循跡小車Arduino程序.doc
//循跡小車Arduino 程序:
// R是右(right),L是左(left) 小車對(duì)著自己看時(shí) 分的左右
int MotorRight1=14; //A0 IN1
int MotorRight2=15; //A1 IN2
int MotorLeft1=16; //A2 IN3
int MotorLeft2=17; //A3 IN4
int MotorRPWM=5; //PWM 5
int MotorLPWM=3; //PWM 3
const int SensorLeft = 2; //左感測(cè)器輸入腳
const int SensorRight = 6; //右感測(cè)器輸入腳
int SL; //左感測(cè)器狀態(tài)
int SR; //右感測(cè)器狀態(tài)
void setup()
{
Serial.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); //定義左感測(cè)器
pinMode(SensorRight, INPUT); //定義右感測(cè)器
}
void loop()
{
SL = digitalRead(SensorLeft);
SR = digitalRead(SensorRight);
if (SL == LOW&&SR==LOW)// 前進(jìn)
{
digitalWrite(MotorRight1,LOW);//IN1 右電機(jī) 高電平反轉(zhuǎn)
digitalWrite(MotorRight2,HIGH);//IN2 右電機(jī) 高電平正轉(zhuǎn)
analogWrite(MotorRPWM,130); //0---100--250
digitalWrite(MotorLeft1,LOW);//IN3 左電機(jī) 高電平正轉(zhuǎn)
digitalWrite(MotorLeft2,HIGH);//IN4 左電機(jī) 高電平反轉(zhuǎn)
analogWrite(MotorLPWM,130);
}
else // R是右(right),L是左(left) 小車對(duì)著自己看時(shí) 分的左右
{
if (SL == HIGH & SR == LOW)// 左白右黑, 快速左轉(zhuǎn)
{
delay(1);
digitalWrite(MotorRight1,HIGH);//IN1 右電機(jī) 高電平反轉(zhuǎn)
digitalWrite(MotorRight2,LOW);//IN2 右電機(jī) 高電平正轉(zhuǎn)
analogWrite(MotorRPWM,130);
digitalWrite(MotorLeft1,LOW);//IN3 左電機(jī) 高電平正轉(zhuǎn)
digitalWrite(MotorLeft2,HIGH);//IN4 左電機(jī) 高電平反轉(zhuǎn)
analogWrite(MotorLPWM,130);
}
else if (SR == HIGH & SL == LOW) // 右白左黑, 快速右轉(zhuǎn)
{
delay(1);
digitalWrite(MotorRight1,LOW);//IN1 右電機(jī) 高電平反轉(zhuǎn)
digitalWrite(MotorRight2,HIGH);//IN2 右電機(jī) 高電平正轉(zhuǎn)
analogWrite(MotorRPWM,130);
digitalWrite(MotorLeft1,HIGH);//IN3 左電機(jī) 高電平正轉(zhuǎn)
digitalWrite(MotorLeft2,LOW);//IN4 左電機(jī) 高電平反轉(zhuǎn)
analogWrite(MotorLPWM,130);
}
else // 都是白色, 停止
{
digitalWrite(MotorRight1,LOW);
digitalWrite(MotorRight2,LOW);
analogWrite(MotorRPWM,0);
digitalWrite(MotorLeft1,LOW);
digitalWrite(MotorLeft2,LOW);;
analogWrite(MotorLPWM,0);
}}}
收藏
- 資源描述:
-
//循跡小車Arduino 程序:
// R是右(right),L是左(left) 小車對(duì)著自己看時(shí) 分的左右
int MotorRight1=14; //A0 IN1
int MotorRight2=15; //A1 IN2
int MotorLeft1=16; //A2 IN3
int MotorLeft2=17; //A3 IN4
int MotorRPWM=5; //PWM 5
int MotorLPWM=3; //PWM 3
const int SensorLeft = 2; //左感測(cè)器輸入腳
const int SensorRight = 6; //右感測(cè)器輸入腳
int SL; //左感測(cè)器狀態(tài)
int SR; //右感測(cè)器狀態(tài)
void setup()
{
Serial.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); //定義左感測(cè)器
pinMode(SensorRight, INPUT); //定義右感測(cè)器
}
void loop()
{
SL = digitalRead(SensorLeft);
SR = digitalRead(SensorRight);
if (SL == LOW&&SR==LOW)// 前進(jìn)
{
digitalWrite(MotorRight1,LOW);//IN1 右電機(jī) 高電平反轉(zhuǎn)
digitalWrite(MotorRight2,HIGH);//IN2 右電機(jī) 高電平正轉(zhuǎn)
analogWrite(MotorRPWM,130); //0---100--250
digitalWrite(MotorLeft1,LOW);//IN3 左電機(jī) 高電平正轉(zhuǎn)
digitalWrite(MotorLeft2,HIGH);//IN4 左電機(jī) 高電平反轉(zhuǎn)
analogWrite(MotorLPWM,130);
}
else // R是右(right),L是左(left) 小車對(duì)著自己看時(shí) 分的左右
{
if (SL == HIGH & SR == LOW)// 左白右黑, 快速左轉(zhuǎn)
{
delay(1);
digitalWrite(MotorRight1,HIGH);//IN1 右電機(jī) 高電平反轉(zhuǎn)
digitalWrite(MotorRight2,LOW);//IN2 右電機(jī) 高電平正轉(zhuǎn)
analogWrite(MotorRPWM,130);
digitalWrite(MotorLeft1,LOW);//IN3 左電機(jī) 高電平正轉(zhuǎn)
digitalWrite(MotorLeft2,HIGH);//IN4 左電機(jī) 高電平反轉(zhuǎn)
analogWrite(MotorLPWM,130);
}
else if (SR == HIGH & SL == LOW) // 右白左黑, 快速右轉(zhuǎn)
{
delay(1);
digitalWrite(MotorRight1,LOW);//IN1 右電機(jī) 高電平反轉(zhuǎn)
digitalWrite(MotorRight2,HIGH);//IN2 右電機(jī) 高電平正轉(zhuǎn)
analogWrite(MotorRPWM,130);
digitalWrite(MotorLeft1,HIGH);//IN3 左電機(jī) 高電平正轉(zhuǎn)
digitalWrite(MotorLeft2,LOW);//IN4 左電機(jī) 高電平反轉(zhuǎn)
analogWrite(MotorLPWM,130);
}
else // 都是白色, 停止
{
digitalWrite(MotorRight1,LOW);
digitalWrite(MotorRight2,LOW);
analogWrite(MotorRPWM,0);
digitalWrite(MotorLeft1,LOW);
digitalWrite(MotorLeft2,LOW);;
analogWrite(MotorLPWM,0);
}}}
展開閱讀全文