Hi people i require some help please, i'm new to the arduino world , im building an autonomous robot and im using 3 ping sensors. 1 sensor connected on either side of the robot and 1 in the front, robot consists of two wheels and a tail wheel.i want to use differential drive enabling the robot to move left,right,foward. All sensors seem to be working well when viewed on the serial monitor,here is the code.
const int pingPin1 = 2;
const int pingPin2 = 4;
const int pingPin3 = 7;
void setup()
{
 Serial.begin(9600);
}
void loop()
{
 ping1();
 delay(250);
 ping2();
 delay(250);
 ping3();
 delay(250);
}
long microsecondsToCentimeters(long microseconds)
{
 return microseconds / 29 / 2;
}
void ping1()
{
 long duration1, cm1;
 pinMode(pingPin1, OUTPUT);
 digitalWrite(pingPin1, LOW);
 delayMicroseconds(2);
 digitalWrite(pingPin1, HIGH);
 delayMicroseconds(5);
 digitalWrite(pingPin1, LOW);
 pinMode(pingPin1, INPUT);
 duration1 = pulseIn(pingPin1, HIGH);
 cm1 = microsecondsToCentimeters(duration1);
 Serial.print("Left = ");
 Serial.print(cm1);
 Serial.print("cm");
 Serial.println();
}
void ping2()
{
 long duration2, cm2;
 pinMode(pingPin2, OUTPUT);
 digitalWrite(pingPin2, LOW);
 delayMicroseconds(2);
 digitalWrite(pingPin2, HIGH);
 delayMicroseconds(5);
 digitalWrite(pingPin2, LOW);
 pinMode(pingPin2, INPUT);
 duration2 = pulseIn(pingPin2, HIGH);
 cm2 = microsecondsToCentimeters(duration2);
 Serial.print("Right = ");
 Serial.print(cm2);
 Serial.print("cm");
 Serial.println();
}
void ping3()
{
 long duration3, cm3;
 pinMode(pingPin3, OUTPUT);
 digitalWrite(pingPin3, LOW);
 delayMicroseconds(2);
 digitalWrite(pingPin3, HIGH);
 delayMicroseconds(5);
 digitalWrite(pingPin3, LOW);
 pinMode(pingPin3, INPUT);
 duration3 = pulseIn(pingPin3, HIGH);
 cm3 = microsecondsToCentimeters(duration3);
 Serial.print("Front = ");
 Serial.print(cm3);
 Serial.print("cm");
 Serial.println();
}
.
Now the problem is that i require the robot to be able to avoid obstacles, once it comes within 20cm of an obstacle:
for example, if sensor1,sensor2,sensor3 are all greater the 20cm then robot moves foward
if sensor1 less than 20cm and sensor 2/3 are greater than 20cm then robot turns right
if sensor1 and 2 greater than 20cm amd sensor 3 less than 20 then robot turns right
can someone please guide me with the code on how to go about this, will be much appreciated, thanks