ZYAR20A 亚克力2驱 蓝牙 298寻迹避障机器人 —— 小车指定花式动作

在下载到开发板之前要选择好板和端口,具体参见:

ZYAR20A 亚克力2驱 蓝牙 298寻迹避障机器人 —— Arduino相关设置

代码

//  智能小车指定花式动作
//===============================================================
//#include <Servo.h> 
int Left_motor_go=8;     //左电机前进(IN1)
int Left_motor_back=9;     //左电机后退(IN2)

int Right_motor_go=10;    // 右电机前进(IN3)
int Right_motor_back=11;    // 右电机后退(IN4)

void setup()
{
  //初始化电机驱动IO为输出方式
  pinMode(Left_motor_go,OUTPUT); // PIN 8 (PWM)
  pinMode(Left_motor_back,OUTPUT); // PIN 9 (PWM)
  pinMode(Right_motor_go,OUTPUT);// PIN 10 (PWM) 
  pinMode(Right_motor_back,OUTPUT);// PIN 11 (PWM)
}

void brake(int time)         //刹车,停车
{
  digitalWrite(Right_motor_go,LOW);
  digitalWrite(Right_motor_back,LOW);
  digitalWrite(Left_motor_go,LOW);
  digitalWrite(Left_motor_back,LOW);
  delay(time * 100);//执行时间,可以调整  
}
void run(int time)     // 前进
{
  digitalWrite(Right_motor_go,HIGH);  // 右电机前进
  digitalWrite(Right_motor_back,LOW);     
  analogWrite(Right_motor_go,200);//PWM比例0~255调速,左右轮差异略增减
  analogWrite(Right_motor_back,0);
  digitalWrite(Left_motor_go,LOW);  // 左电机前进
  digitalWrite(Left_motor_back,HIGH);
  analogWrite(Left_motor_go,0);//PWM比例0~255调速,左右轮差异略增减
  analogWrite(Left_motor_back,200);
  delay(time * 100);   //执行时间,可以调整  
}
void left(int time)         //左转(左轮不动,右轮前进)
{
  digitalWrite(Right_motor_go,HIGH);	// 右电机前进
  digitalWrite(Right_motor_back,LOW);
  analogWrite(Right_motor_go,200); 
  analogWrite(Right_motor_back,0);//PWM比例0~255调速
  digitalWrite(Left_motor_go,LOW);   //左轮后退
  digitalWrite(Left_motor_back,LOW);
  analogWrite(Left_motor_go,0); 
  analogWrite(Left_motor_back,0);//PWM比例0~255调速
  delay(time * 100);	//执行时间,可以调整  
}
void spin_left(int time)         //左转(左轮后退,右轮前进)
{
  digitalWrite(Right_motor_go,HIGH);	// 右电机前进
  digitalWrite(Right_motor_back,LOW);
  analogWrite(Right_motor_go,200); 
  analogWrite(Right_motor_back,0);//PWM比例0~255调速
  digitalWrite(Left_motor_go,HIGH);   //左轮后退
  digitalWrite(Left_motor_back,LOW);
  analogWrite(Left_motor_go,200); 
  analogWrite(Left_motor_back,0);//PWM比例0~255调速
  delay(time * 100);	//执行时间,可以调整  
}

void right(int time)        //右转(右轮不动,左轮前进)
{
  digitalWrite(Right_motor_go,LOW);   //右电机后退
  digitalWrite(Right_motor_back,LOW);
  analogWrite(Right_motor_go,0); 
  analogWrite(Right_motor_back,0);//PWM比例0~255调速
  digitalWrite(Left_motor_go,LOW);//左电机前进
  digitalWrite(Left_motor_back,HIGH);
  analogWrite(Left_motor_go,0); 
  analogWrite(Left_motor_back,200);//PWM比例0~255调速
  delay(time * 100);	//执行时间,可以调整  
}

void spin_right(int time)        //右转(右轮后退,左轮前进)
{
  digitalWrite(Right_motor_go,LOW);   //右电机后退
  digitalWrite(Right_motor_back,HIGH);
  analogWrite(Right_motor_go,0); 
  analogWrite(Right_motor_back,200);//PWM比例0~255调速
  digitalWrite(Left_motor_go,LOW);//左电机前进
  digitalWrite(Left_motor_back,HIGH);
  analogWrite(Left_motor_go,0); 
  analogWrite(Left_motor_back,200);//PWM比例0~255调速
  delay(time * 100);	//执行时间,可以调整    
}

void back(int time)          //后退
{
  digitalWrite(Right_motor_go,LOW);  //右轮后退
  digitalWrite(Right_motor_back,HIGH);
  analogWrite(Right_motor_go,0);
  analogWrite(Right_motor_back,150);//PWM比例0~255调速
  digitalWrite(Left_motor_go,HIGH);  //左轮后退
  digitalWrite(Left_motor_back,LOW);
  analogWrite(Left_motor_go,150);
  analogWrite(Left_motor_back,0);//PWM比例0~255调速
  delay(time * 100);     //执行时间,可以调整  
}

void loop()
{    
  int i;
  delay(2000); //延时2s后启动
  run(10);
  back(10);//全速前进急停后退
  brake(5);

  for(i=0;i<5;i++)
  {
    run(10);//小车间断性前进5步
    brake(1);
  }

  for(i=0;i<5;i++)
  {
    back(10);//小车间断性后退5步
    brake(1);
  }

  for(i=0;i<5;i++)
  {
    left(10);//大弯套小弯连续左旋转
    spin_left(5);
  }

  for(i=0;i<5;i++)
  {
    right(10);//大弯套小弯连续右旋转
    spin_right(5);
  }

  for(i=0;i<10;i++)
  {
    right(1);//间断性原地右转弯
    brake(1);
  }

  for(i=0;i<10;i++)
  {
    left(1);//间断性原地左转弯
    brake(1);
  }

  for(i=0;i<10;i++)
  {
    left(3);//走S形前进
    right(3);
  }

  for(i=0;i<10;i++)
  {
    spin_left(3);//间断性原地左打转
    brake(3);
  }

  for(i=0;i<10;i++)
  {
    spin_right(3);//间断性原地右打转
    brake(3);
  }

}





posted @ 2019-02-20 21:36  AlexKing007  阅读(83)  评论(0编辑  收藏  举报