int distanceCm = 0;
/* using variables / identifiers instead of hard-coded values
makes the code more readable and easier to modify */
int quarterSpeed = 64;
int fullSpeed = 255;
/* the following names assume a scenario where
left motor is controlled*/
int leftMotorFwd = 6;
int leftMotorBwd = 5;
long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
/ |