Can anyone help me with this code i am new to programing and i just want this if else statement to work.
#define ping2pin 10 // left sensor
#define pingPin 9 // forward Ping ultrasonic sensor on pin D9.
#define leftDir1 8 // Left motor direction 1, to AIn1.
#define leftDir2 7 // Left motor direction 2, to AIn2.
#define rightDir1 6 // Right motor direction 1, to BIn1.
#define rightDir2 5 // Right motor direction 2, to BIn2.
#define BOUNDARY 50 // (cm) Avoid objects closer than 20cm.
#define INTERVAL 25 // (ms) Interval between distance readings.
#define limit 30
// setup
// Set motor pins as OUTPUTS, initialize Serial
void setup()
{
pinMode(leftDir1, OUTPUT); // Set motor direction pins as outputs.
pinMode(leftDir2, OUTPUT);
pinMode(rightDir1, OUTPUT);
pinMode(rightDir2, OUTPUT);
Serial.begin(9600);
}
// Main program
// Roam around while avoiding objects.
//
// Set motors to move forward,
// Take distance readings over and over,
// as long as no objects are too close (determined by BOUNDARY).
// If object is too close, avoid it -- back up and turn.
// Repeat.
void loop()
{
long xdistance; // Distance reading from front ping.
long ydistance; // Distance reading from left ping
{ // Robot moves forward continuously.
if(((xdistance >= BOUNDARY)&&(ydistance <= BOUNDARY)&&(ydistance >= limit)));
{
forward();
}
else if((xdistance < BOUNDARY)&&(ydistance > BOUNDARY)); // Loop while no objects close-by.
{
leftTurn(500);
}
else((xdistance > BOUNDARY)&&(ydistance <= limit));
{
rightTurn(20);
} // Turn left 500 ms.
} // end Main program
}
// Robot has sensed a nearby object and exited the while loop.
// Take evasive action to avoid object.
// forward
//
// Move robot forward by setting both wheels forward.
// Will persist until something else changes the
// motors' directions.
void forward()
{
digitalWrite(leftDir1, LOW);
digitalWrite(leftDir2, LOW);
digitalWrite(rightDir1, HIGH); // LEFT motor forward.
digitalWrite(rightDir2, HIGH); // RIGHT MOTOR FORWARD.
}
// backward
//
// Move robot backward by setting both wheels backward.
// Will persist until something else changes the
// motors' directions.
void backward()
{
digitalWrite(leftDir1, HIGH); // Left motor backward.
digitalWrite(leftDir2, HIGH);
digitalWrite(rightDir1, LOW); // Right motor backward.
digitalWrite(rightDir2, LOW);
}
// leftTurn
//
// Turn robot to left by moving wheels in opposite directions.
// Amount of turning is determined by duration argument (ms).
void leftTurn(int duration)
{
digitalWrite(leftDir1, HIGH); // Left motor backward.
digitalWrite(leftDir2, LOW);
digitalWrite(rightDir1, HIGH); // Right motor forward.
digitalWrite(rightDir2, HIGH);
delay(duration); // Turning time (ms).
}
void rightTurn(int duration)
{
digitalWrite(leftDir1,LOW);
digitalWrite(leftDir2, HIGH);
digitalWrite(rightDir1,HIGH);
digitalWrite(rightDir2,HIGH);
}
// readDistance
// Take a distance reading from Ping ultrasonic rangefinder.
// from http://arduino.cc/en/Tutorial/Ping?from=Tutorial.UltrasoundSensor
long readxDistance()
{
long duration, inches, cm;
// The Ping is triggered by a HIGH pulse of 2 or more microseconds.
// We give a short LOW pulse beforehand to ensure a clean HIGH pulse.
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the Ping: a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
// Convert the time into a distance.
cm = microsecondsToCentimeters(duration);
return(cm);
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance traveled.
return microseconds / 29 / 2;
}
long readyDistance()
{
long duration, inches, cm; //left sensor variable
// The Ping is triggered by a HIGH pulse of 2 or more microseconds.
// We give a short LOW pulse beforehand to ensure a clean HIGH pulse.
pinMode(ping2pin, OUTPUT);
digitalWrite(ping2pin, LOW);
delayMicroseconds(2);
digitalWrite(ping2pin, HIGH);
delayMicroseconds(5);
digitalWrite(ping2pin, LOW);
// The same pin is used to read the signal from the Ping: a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off an object.
pinMode(ping2pin, INPUT);
duration = pulseIn(ping2pin, HIGH);
// Convert the time into a distance.
cm = microsecondsToCentimeters(duration);
return(cm);
}