資源描述:
《循跡小車Arduino程序.doc》由會員上傳分享,免費在線閱讀,更多相關內(nèi)容在工程資料-天天文庫。
1、//循跡小車Arduino程序://R是右(right),L是左(left)小車對著自己看時分的左右intMotorRight1=14;//A0IN1intMotorRight2=15;//A1IN2intMotorLeft1=16;//A2IN3intMotorLeft2=17;//A3IN4intMotorRPWM=5;//PWM5intMotorLPWM=3;//PWM3constintSensorLeft=2;//左感測器輸入腳constintSensorRight=6;//右感測器輸入腳intSL;//左感測器狀態(tài)intSR;//右感測器狀態(tài)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);}}}