|
unsigned char analogPin = 0;
float val = 0;
int pwm_a = 3;
int pwm_b = 11;
int dir_a = 12;
int dir_b = 13;
void setup() {
Serial.begin(9600);
pinMode(pwm_a, OUTPUT);
pinMode(pwm_b, OUTPUT);
pinMode(dir_a, OUTPUT);
pinMode(dir_b, OUTPUT);
}
void loop()
{
val = analogRead(analogPin)*1.38; // read the value from the
Serial.print("EZ1 distance = ");
Serial.print(val);
Serial.println(" cm");
delay(100);
digitalWrite(dir_a, HIGH);
digitalWrite(dir_b, HIGH);
delay(100);
analogWrite(pwm_a,250);
analogWrite(pwm_b,250);
delay(100);
if (val < 100) // obstacle detected
{
//Stop both motors...
digitalWrite(dir_a, HIGH);
digitalWrite(dir_b, HIGH);
analogWrite(pwm_a, 100);
analogWrite(pwm_b, 100);
delay(1000); // ...and pause for a second
}
else if ( val < 50 );
{
digitalWrite(dir_a, LOW);
digitalWrite(dir_b, HIGH);
analogWrite(pwm_a, 150);
analogWrite(pwm_b, 50);
delay (1000);
}
}
無法照程式計畫走 |
|