travel time in microseconds */
return pulseIn(echoPin, HIGH);
}
void setup()
{
pinMode(leftMotorBwd, OUTPUT);
pinMode(leftMotorFwd, OUTPUT);
}
void loop()
{
distanceCm = 0.01723 * readUltrasonicDistance(9, 8);
if (distanceCm > 50) {
analogWrite(leftMotorBwd, 0);
analogWrite(leftMotorFwd, fullSpeed);
} else {
if (distanceCm > 25) {
analogWrite(leftMotorBwd, 0);
analogWrite(leftMotorFwd, quarterSpeed);
} else {
if (distanceCm <= 20) {
analogWrite(leftMotorBwd, quarterSpeed);
analogWrite(leftMotorFwd, 0);
} else {
analogWrite(leftMotorBwd, 0);
analogWrite(leftMotorFwd, 0);
}
}
}
delay(200); // Wait for 200 millisecond(s)
} |