Arduino小车-指定花式动作实验

简介注意:由于左右电机出厂时的齿轮磨损不一样,所以未调整前,直线行走可能会发生偏转

简介

  • 使小车做指定花式动作

注意:由于左右电机出厂时的齿轮磨损不一样,所以未调整前,直线行走可能会发生偏转

样例代码

  • 小车指定花式动作代码
//===============================================================
//  智能小车指定花式动作
//===============================================================
//#include <Servo.h> 
int Left_motor_back=8;     //左电机后退(IN1)
int Left_motor_go=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 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,HIGH);  // 左电机前进
  digitalWrite(Left_motor_back,LOW);
  analogWrite(Left_motor_go,200);//PWM比例0~255调速,左右轮差异略增减
  analogWrite(Left_motor_back,0);
  delay(time * 100);   //执行时间,可以调整  
}

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 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,LOW);   //左轮后退
  digitalWrite(Left_motor_back,HIGH);
  analogWrite(Left_motor_go,0); 
  analogWrite(Left_motor_back,200);//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,HIGH);//左电机前进
  digitalWrite(Left_motor_back,LOW);
  analogWrite(Left_motor_go,200); 
  analogWrite(Left_motor_back,0);//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,HIGH);//左电机前进
  digitalWrite(Left_motor_back,LOW);
  analogWrite(Left_motor_go,200); 
  analogWrite(Left_motor_back,0);//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,LOW);  //左轮后退
  digitalWrite(Left_motor_back,HIGH);
  analogWrite(Left_motor_go,0);
  analogWrite(Left_motor_back,150);//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);
  }

}

测试结果

  • 小车做指定花式动作
「点点赞赏,手留余香」

    还没有人赞赏,快来当第一个赞赏的人吧!
0 条回复 A 作者 M 管理员
    所有的伟大,都源于一个勇敢的开始!
欢迎您,新朋友,感谢参与互动!欢迎您 {{author}},您在本站有{{commentsCount}}条评论