本文是基于Ardino的两轮循迹小车源码。
由于学校的要求于是进行了相应的小车开发,由于结构过于简单,在此就不再赘述,直接附上当时比赛的源代码。
intLeft_motor_go=8; //左电机前进(IN1)intLeft_motor_back=9; //左电机后退(IN2)intRight_motor_go=10; // 右电机前进(IN3)intRight_motor_back=11; // 右电机后退(IN4)constintSensorRight1=6; //右循迹红外传感器(P3.2 OUT1)constintSensorRight2=3; //右循迹红外传感器constintSensorLeft1=4; //左循迹红外传感器(P3.3 OUT2)constintSensorLeft2=5; //左循迹红外传感器(P3.3 OUT2)intSL1; //左循迹红外传感器状态 intSL2; //左循迹红外传感器状态intSR1; //右循迹红外传感器状态intSR2; //右循迹红外传感器状态voidsetup() { //初始化电机驱动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)// pinMode(key,INPUT);//定义按键接口为输入接口// pinMode(beep,OUTPUT);pinMode(SensorRight1, INPUT); //定义右循迹红外传感器为输入pinMode(SensorRight2, INPUT); pinMode(SensorLeft1, INPUT); //定义左循迹红外传感器为输入pinMode(SensorLeft2, INPUT); } //=======================智能小车的基本动作=========================voidruns() { digitalWrite(Right_motor_go,HIGH); // 右电机前进digitalWrite(Right_motor_back,LOW); analogWrite(Right_motor_go,100);//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,100); //delay(time * 100); //执行时间,可以调整 } voidrun() { digitalWrite(Right_motor_go,HIGH); // 右电机前进digitalWrite(Right_motor_back,LOW); analogWrite(Right_motor_go,100);//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,100); //delay(time * 100); //执行时间,可以调整 } //void left(int time) //左转(左轮不动,右轮前进)voidspin_left() { digitalWrite(Right_motor_go,HIGH); // 右电机前进digitalWrite(Right_motor_back,LOW); analogWrite(Right_motor_go,90); analogWrite(Right_motor_back,0);//PWM比例0~255调速digitalWrite(Left_motor_go,HIGH); //左轮后退digitalWrite(Left_motor_back,LOW); analogWrite(Left_motor_go,75); analogWrite(Left_motor_back,0);//PWM比例0~255调速//delay(time * 100); //执行时间,可以调整 } voidspin_lefts() { digitalWrite(Right_motor_go,HIGH); // 右电机前进digitalWrite(Right_motor_back,LOW); analogWrite(Right_motor_go,140.3); analogWrite(Right_motor_back,0);//PWM比例0~255调速digitalWrite(Left_motor_go,HIGH); //左轮后退digitalWrite(Left_motor_back,LOW); analogWrite(Left_motor_go,0); analogWrite(Left_motor_back,140);//PWM比例0~255调速///delay(time * 100); //执行时间,可以调整 } //void right(int time) //右转(右轮不动,左轮前进)voidspin_right() { digitalWrite(Right_motor_go,LOW); //右电机后退digitalWrite(Right_motor_back,HIGH); analogWrite(Right_motor_go,0); analogWrite(Right_motor_back,75);//PWM比例0~255调速digitalWrite(Left_motor_go,LOW);//左电机前进digitalWrite(Left_motor_back,HIGH); analogWrite(Left_motor_go,0); analogWrite(Left_motor_back,90);//PWM比例0~255调速//delay(time * 100); //执行时间,可以调整 } voidspin_rights() { digitalWrite(Right_motor_go,LOW); //右电机后退digitalWrite(Right_motor_back,HIGH); analogWrite(Right_motor_go,140); 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,140.3);//PWM比例0~255调速//delay(time * 100); //执行时间,可以调整 } voidloop() { // keysacn();//调用按键扫描函数 while(1) { //有信号为LOW 没有信号为HIGH 检测到黑线 输出高 检测到白色区域输出低SR1=digitalRead(SensorRight1);//有信号表明在白色区域,车子底板上L1亮;没信号表明压在黑线上,车子底板上L1灭SR2=digitalRead(SensorRight2); SL1=digitalRead(SensorLeft1);//有信号表明在白色区域,车子底板上L2亮;没信号表明压在黑线上,车子底板上L2灭SL2=digitalRead(SensorLeft2); if (SL1==LOW&&SL2==LOW&&SR1==LOW&&SR2==LOWorSL2==HIGH&&SL1==HIGH&&SR2==HIGH&&SR1==LOWorSL2==LOW&&SL1==HIGH&&SR2==HIGH&&SR1==HIGHorSL2==LOW&&SL1==HIGH&&SR2==HIGH&&SR1==LOW ) run(); //调用前进函数elseif (SR1==LOW&&SL2==LOW&&SL1==HIGH&&SR2==LOW) spin_right(); elseif (SL1==LOW&&SR2==LOW&&SL2==HIGH&&SR1==LOW) spin_rights(); elseif (SL1==HIGH&&SL2==HIGH&&SR1==LOW&&SR2==LOW) spin_rights(); elseif (SL1==LOW&&SR1==HIGH&&SR2==LOW&&SL2==LOW) spin_lefts(); elseif (SR1==LOW&&SR2==HIGH&&SL2==LOW&&SL1==LOW) spin_left(); elseif (SL1==LOW&&SL2==LOW&&SR1==HIGH&&SR2==HIGH) spin_lefts(); elseif (SL1==HIGH&&SL2==HIGH&&SR1==HIGH&&SR2==HIGHorSR1==HIGH&&SR2==LOW&&SL1==HIGH&&SL2==LOWorSL1==LOW&&SL2==HIGH&&SR1==LOW&&SR2==HIGH ) runs(); } }