资源描述:
《循迹小车Arduino程序.doc》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、//循迹小车Arduino程序://R是右(right),L是左(left)小车对着自己看时分的左右intMotorRight1=14;//A0IN1intMotorRight2=15;//A1IN2intMotorLeft1=16;//A2IN3intMotorLeft2=17;//A3IN4intMotorRPWM=5;//PWM5intMotorLPWM=3;//PWM3constintSensorLeft=2;//左感測器輸入腳constintSensorRight=6;//右感測器輸入腳intSL;//左感測器狀態intSR;//右感測器狀態voidsetup(){Seria
2、l.begin(9600);pinMode(MotorRight1,OUTPUT);//腳位14(PWM)pinMode(MotorRight2,OUTPUT);//腳位15(PWM)pinMode(MotorLeft1,OUTPUT);//腳位16(PWM)pinMode(MotorLeft2,OUTPUT);//腳位17(PWM)pinMode(MotorLPWM,OUTPUT);//腳位3(PWM)pinMode(MotorRPWM,OUTPUT);//腳位5(PWM)pinMode(SensorLeft,INPUT);//定義左感測器pinMode(SensorRight,IN
3、PUT);//定義右感測器}voidloop(){SL=digitalRead(SensorLeft);SR=digitalRead(SensorRight);if(SL==LOW&&SR==LOW)//前進{digitalWrite(MotorRight1,LOW);//IN1右电机高电平反转digitalWrite(MotorRight2,HIGH);//IN2右电机高电平正转analogWrite(MotorRPWM,130);//0---100--250digitalWrite(MotorLeft1,LOW);//IN3左电机高电平正转digitalWrite(MotorLef
4、t2,HIGH);//IN4左电机高电平反转analogWrite(MotorLPWM,130);}else//R是右(right),L是左(left)小车对着自己看时分的左右{if(SL==HIGH&SR==LOW)//左白右黑,快速左轉{delay(1);digitalWrite(MotorRight1,HIGH);//IN1右电机高电平反转digitalWrite(MotorRight2,LOW);//IN2右电机高电平正转analogWrite(MotorRPWM,130);digitalWrite(MotorLeft1,LOW);//IN3左电机高电平正转digitalWri
5、te(MotorLeft2,HIGH);//IN4左电机高电平反转analogWrite(MotorLPWM,130);}elseif(SR==HIGH&SL==LOW)//右白左黑,快速右轉{delay(1);digitalWrite(MotorRight1,LOW);//IN1右电机高电平反转digitalWrite(MotorRight2,HIGH);//IN2右电机高电平正转analogWrite(MotorRPWM,130);digitalWrite(MotorLeft1,HIGH);//IN3左电机高电平正转digitalWrite(MotorLeft2,LOW);//IN
6、4左电机高电平反转analogWrite(MotorLPWM,130);}else//都是白色,停止{digitalWrite(MotorRight1,LOW);digitalWrite(MotorRight2,LOW);analogWrite(MotorRPWM,0);digitalWrite(MotorLeft1,LOW);digitalWrite(MotorLeft2,LOW);;analogWrite(MotorLPWM,0);}}}