Arduino-寻迹小车
Arduino-寻迹小车
作者:Atopos
这是完整的代码,下面有各部分的详解
#define AIN1 7 //定义两个电机 左
#define AIN2 8
#define BIN1 3 //右
#define BIN2 4
#define l A1 //从车尾方向从左到右排序
#define z A2
#define r A3
#define STBY 10
int PWMA=5; //左PWM调速
int PWMB=6; //右
int LED0; //定义三个用来存储传感器返回值
int LED1;
int LED2;
void setup() {
// 电机驱动模块引脚初始化
pinMode(AIN1,OUTPUT);
pinMode(AIN2,OUTPUT);
pinMode(BIN1,OUTPUT);
pinMode(BIN2,OUTPUT);
//循迹模块引脚初始化
pinMode(A1,INPUT);
pinMode(A2,INPUT);
pinMode(A3,INPUT);
pinMode(PWMA,OUTPUT);
pinMode(PWMB,OUTPUT);
pinMode(STBY,OUTPUT);
}
void loop() {
xunji();
}
void xunji(){
LED0=digitalRead(A1);//这里用之前定义的三个变量来读取传感器的返回值,用与接下来的判断
LED1=digitalRead(A2);
LED2=digitalRead(A3);
digitalWrite(STBY,HIGH);
if(LED0==HIGH&&LED1==LOW&&LED2==HIGH) //正常情况小车正常直行
{
digitalWrite(AIN1,HIGH);
digitalWrite(AIN2,LOW);
analogWrite(PWMA,80);
digitalWrite(BIN1,HIGH);
digitalWrite(BIN2,LOW);
analogWrite(PWMB,110);
delay(30) ;
}
else if(LED0==HIGH&&LED1==HIGH&&LED2==HIGH) //三个传感器都接受到信号小车正常行走
{
digitalWrite(AIN1,HIGH);
digitalWrite(AIN2,LOW);
analogWrite(PWMA,80);
digitalWrite(BIN1,HIGH);
digitalWrite(BIN2,LOW);
analogWrite(PWMB,110);
delay(30);
}
else if(LED0==LOW&&LED1==HIGH&&LED2==HIGH) //小车左转
{
digitalWrite(AIN1,LOW);
digitalWrite(AIN2,HIGH);
analogWrite(PWMA,100);
digitalWrite(BIN1,HIGH);
digitalWrite(BIN2,LOW);
analogWrite(PWMB,100);
delay(50);
}
else if(LED0==HIGH&&LED1==HIGH&&LED2==LOW) //小车右转
{
digitalWrite(AIN1,HIGH);
digitalWrite(AIN2,LOW);
analogWrite(PWMA,115);
digitalWrite(BIN1,LOW);
digitalWrite(BIN2,HIGH);
analogWrite(PWMB,90);
delay(30);
}
else if(LED0==LOW&&LED1==LOW&&LED2==HIGH) //小车进内圈左转
{
digitalWrite(AIN1,LOW);
digitalWrite(AIN2,HIGH);
analogWrite(PWMA,80);
digitalWrite(BIN1,HIGH);
digitalWrite(BIN2,LOW);
analogWrite(PWMB,120);
delay(600);
}
else if(LED0==LOW&&LED1==LOW&&LED2==LOW) //遇到十字路口,小车3个感应器都感应到黑线,小车停止
{
digitalWrite(STBY,LOW);
delay(1000);
}
}
详解
第一部分:线路
放上小车的线路连接图
第二部分:代码
UNO板子,电机以及传感器接口的预处理
对每个接口都进行定义
#define AIN1 7 //定义两个电机 左
#define AIN2 8
#define BIN1 3 //右
#define BIN2 4
#define l A1 //从车尾方向从左到右排序
#define z A2
#define r A3
#define STBY 10
int PWMA=5; //左PWM调速
int PWMB=6; //右
int LED0; //定义三个用来存储传感器返回值
int LED1;
int LED2;
宣告接口状态,也就是初始化各个引脚的状态
void setup() {
// 电机驱动模块引脚初始化
pinMode(AIN1,OUTPUT);
pinMode(AIN2,OUTPUT);
pinMode(BIN1,OUTPUT);
pinMode(BIN2,OUTPUT);
//循迹模块引脚初始化
pinMode(A1,INPUT);
pinMode(A2,INPUT);
pinMode(A3,INPUT);
pinMode(PWMA,OUTPUT);
pinMode(PWMB,OUTPUT);
pinMode(STBY,OUTPUT);
}
下面是主程序——loop程序
这里用一个函数代替,方便后续修改,至于后续要修改什么,文章最后会提到
void loop() {
xunji();
}
最后就是我们的重点——寻迹模块
这也是整个程序的核心
首先,先定义LED0,LED1,LED2,三个变量来接收三个传感器返回过来的高低电频
LED0=digitalRead(A1);//这里用之前定义的三个变量来读取传感器的返回值,用与接下来的判断
LED1=digitalRead(A2);
LED2=digitalRead(A3);
digitalWrite(STBY,HIGH);
这里用if -else语句,根据传感器返回的值来判断小车的状态,从而做出相应的操作
void xunji(){
if(LED0==HIGH&&LED1==LOW&&LED2==HIGH) //正常情况小车正常直行
{
digitalWrite(AIN1,HIGH);
digitalWrite(AIN2,LOW);
analogWrite(PWMA,80);
digitalWrite(BIN1,HIGH);
digitalWrite(BIN2,LOW);
analogWrite(PWMB,110);
delay(30) ;
}
else if(LED0==HIGH&&LED1==HIGH&&LED2==HIGH) //三个传感器都接受到信号小车正常行走
{
digitalWrite(AIN1,HIGH);
digitalWrite(AIN2,LOW);
analogWrite(PWMA,80);
digitalWrite(BIN1,HIGH);
digitalWrite(BIN2,LOW);
analogWrite(PWMB,110);
delay(30);
}
else if(LED0==LOW&&LED1==HIGH&&LED2==HIGH) //小车左转
{
digitalWrite(AIN1,LOW);
digitalWrite(AIN2,HIGH);
analogWrite(PWMA,100);
digitalWrite(BIN1,HIGH);
digitalWrite(BIN2,LOW);
analogWrite(PWMB,100);
delay(50);
}
else if(LED0==HIGH&&LED1==HIGH&&LED2==LOW) //小车右转
{
digitalWrite(AIN1,HIGH);
digitalWrite(AIN2,LOW);
analogWrite(PWMA,115);
digitalWrite(BIN1,LOW);
digitalWrite(BIN2,HIGH);
analogWrite(PWMB,90);
delay(30);
}
else if(LED0==LOW&&LED1==LOW&&LED2==HIGH) //小车进内圈左转
{
digitalWrite(AIN1,LOW);
digitalWrite(AIN2,HIGH);
analogWrite(PWMA,80);
digitalWrite(BIN1,HIGH);
digitalWrite(BIN2,LOW);
analogWrite(PWMB,120);
delay(600);
}
else if(LED0==LOW&&LED1==LOW&&LED2==LOW) //遇到十字路口,小车3个感应器都感应到黑线,小车停止
{
digitalWrite(STBY,LOW);
delay(1000);
}
}
存在问题
- 上述程序用的是数字传感器,如果是灰度传感器,不可以用上述程序
- 还有,程序采用了正反转,这个根据实际情况而定,如果不要求时间,可以让他慢一点
- 因为测试用的小车电机比较廉价,所以左右电机会有一定的物理误差,也就是给它一样的PWM值
两个电机的转速是不一样的,这个对小车的寻迹也有一定的影响,所以要经过反复实验,调整合适的PWM值 - 还有传感器的位置,这个也根据赛道的情况自己调整
更新:四个传感器代码
哥哥接口跟引脚和上述三个传感器的一样,第四个传感器接在A4引脚。从左往右依次为1,2,3,4传感器,pwm速度需要自己调试
#define AIN1 7 //定义两个电机 左
#define AIN2 8
#define BIN1 3 //右
#define BIN2 4
#define l A1 //从车尾方向从左到右排序
#define z A2
#define r A3
#define f A4
#define STBY 10
int PWMA=5; //左PWM调速
int PWMB=6; //右
int LED0; //定义三个用来存储传感器返回值
int LED1;
int LED2;
int LED3;
void setup() {
// 电机驱动模块引脚初始化
pinMode(AIN1,OUTPUT);
pinMode(AIN2,OUTPUT);
pinMode(BIN1,OUTPUT);
pinMode(BIN2,OUTPUT);
//循迹模块引脚初始化
pinMode(A1,INPUT);
pinMode(A2,INPUT);
pinMode(A3,INPUT);
pinMode(A4,INPUT);
pinMode(PWMA,OUTPUT);
pinMode(PWMB,OUTPUT);
pinMode(STBY,OUTPUT);
}
void loop() {
xunji();
}
void xunji(){
LED0=digitalRead(A1);//这里用之前定义的四个个变量来读取传感器的返回值,用与接下来的判断
LED1=digitalRead(A2);
LED2=digitalRead(A3);
LED3=digitalRead(A4);
digitalWrite(STBY,HIGH);
if(LED0==HIGH&&LED1==0&&LED2==0&&LED3==HIGH) //正常情况小车正常直行
{
digitalWrite(AIN1,HIGH);
digitalWrite(AIN2,LOW);
analogWrite(PWMA,90);
digitalWrite(BIN1,HIGH);
digitalWrite(BIN2,LOW);
analogWrite(PWMB,95);
delay(30) ;
}
else if(LED0==HIGH&&LED1==HIGH&&LED2==HIGH&&LED3==HIGH) //四个个传感器都接受到信号小车正常行走
{
digitalWrite(AIN1,HIGH);
digitalWrite(AIN2,LOW);
analogWrite(PWMA,90);
digitalWrite(BIN1,HIGH);
digitalWrite(BIN2,LOW);
analogWrite(PWMB,95);
delay(30);
}
else if(LED0==HIGH&&LED1==LOW&&LED2==HIGH&&LED3==HIGH) //小车左小调整
{
digitalWrite(AIN1,LOW);
digitalWrite(AIN2,HIGH);
analogWrite(PWMA,100);
digitalWrite(BIN1,HIGH);
digitalWrite(BIN2,LOW);
analogWrite(PWMB,100);
delay(80);
}
else if(LED0==LOW&&LED1==HIGH&&LED2==HIGH&&LED3==HIGH) //小车左大调整
{
digitalWrite(AIN1,LOW);
digitalWrite(AIN2,HIGH);
analogWrite(PWMA,100);
digitalWrite(BIN1,HIGH);
digitalWrite(BIN2,LOW);
analogWrite(PWMB,100);
delay(30);
}
else if(LED0==HIGH&&LED1==HIGH&&LED2==LOW&&LED3==HIGH) //小车右小调整
{
digitalWrite(AIN1,HIGH);
digitalWrite(AIN2,LOW);
analogWrite(PWMA,130);
digitalWrite(BIN1,LOW);
digitalWrite(BIN2,HIGH);
analogWrite(PWMB,100);
delay(80);
}
else if(LED0==HIGH&&LED1==HIGH&&LED2==HIGH&&LED3==LOW) //小车右大调整
{
digitalWrite(AIN1,HIGH);
digitalWrite(AIN2,LOW);
analogWrite(PWMA,130);
digitalWrite(BIN1,LOW);
digitalWrite(BIN2,HIGH);
analogWrite(PWMB,100);
delay(50);
}
else if(LED0==HIGH&&LED1==LOW&&LED2==LOW&&LED3==LOW) //小车右直角
{
digitalWrite(AIN1,HIGH);
digitalWrite(AIN2,LOW);
analogWrite(PWMA,125);
digitalWrite(BIN1,LOW);
digitalWrite(BIN2,HIGH);
analogWrite(PWMB,90);
delay(30);
}
else if(LED0==LOW&&LED1==LOW&&LED2==LOW&&LED3==HIGH) //小车进内圈左转
{
digitalWrite(AIN1,LOW);
digitalWrite(AIN2,HIGH);
analogWrite(PWMA,90);
digitalWrite(BIN1,HIGH);
digitalWrite(BIN2,LOW);
analogWrite(PWMB,120);
delay(30);
}
else if(LED0==LOW&&LED1==HIGH&&LED2==HIGH&&LED3==LOW) //小车进内圈左转
{
digitalWrite(AIN1,LOW);
digitalWrite(AIN2,HIGH);
analogWrite(PWMA,90);
digitalWrite(BIN1,HIGH);
digitalWrite(BIN2,LOW);
analogWrite(PWMB,120);
delay(30);
}
else if(LED0==LOW&&LED1==HIGH&&LED2==LOW&&LED3==LOW) //小车进内圈左转
{
digitalWrite(AIN1,LOW);
digitalWrite(AIN2,HIGH);
analogWrite(PWMA,90);
digitalWrite(BIN1,HIGH);
digitalWrite(BIN2,LOW);
analogWrite(PWMB,120);
delay(30);
}
else if(LED0==LOW&&LED1==LOW&&LED2==LOW&&LED3==LOW) //遇到十字路口,小车3个感应器都感应到黑线,小车停止
{
digitalWrite(STBY,LOW);
delay(1000);
}
}