// constants won't change. They're used here to
// set pin numbers:
const int sensorPinL = 2; //
const int sensorPinR = 3; //
int pwmR = 0;
int pwmL = 0;
int IRpin = 0; // analog pin for reading the IR sensor
float sensorValue, distance;
const int motorLin1 = 10; // 左馬達 電機控制
const int motorLin2= 11; // 左馬達 電機控制
const int motorRin1 = 12; // 右馬達控制
const int motorRin2= 13; // 左馬達控制
// variables will change:
int sensorStateL = 0; // 左偵測器
int sensorStateR = 0; // 右偵測器
void loop(){
// read the state of the pushbutton value:
analogWrite(5,200); //PWM 左馬達
analogWrite(6,200); //PWM 右馬達
sensorStateL = digitalRead(sensorPinL);
sensorStateR = digitalRead(sensorPinR);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (sensorStateL == HIGH) {
// turn LED on:
digitalWrite(motorLin1, HIGH);
digitalWrite(motorLin2, LOW);
}
else {
// turn LED off:
digitalWrite(motorLin2, HIGH);
digitalWrite(motorLin1, LOW);
}
if (sensorStateR == HIGH) {
// turn LED on:
digitalWrite(motorRin1, HIGH);
digitalWrite(motorRin2, LOW);
}
else {
// turn LED off:
digitalWrite(motorRin2, HIGH);
digitalWrite(motorRin1, LOW);
}