programming help for obstacle avoiding robot

hi there people,

so i FINALLY made a obstacle avoiding robot but i wanted to take it to another level so i decided to add another sensor to the robot. but i am having trouble programming it … so could anyone help me

thnks

//#include <AFMotor.h> //import your motor shield library
#define trigPin 12 // define the pins of your sensor
#define echoPin 13
#include <Servo.h>
Servo servoLeft; // and similar for other ones
Servo servoRight; //
int pos = 0; // variable to store the servo position

void setup() {
Serial.begin(9600); // begin serial communitication
Serial.println(“Motor test!”);
pinMode(trigPin, OUTPUT);// set the trig pin to output (Send sound waves)
pinMode(echoPin, INPUT);// set the echo pin to input (recieve sound waves)
servoLeft.attach(10); // Set left servo to digital pin 10
servoRight.attach(9); // Set right servo to digital pin 9
}

void loop() {

long duration, distance; // start the scan
digitalWrite(trigPin, LOW);
delayMicroseconds(2); // delays are required for a succesful sensor operation.
digitalWrite(trigPin, HIGH);

delayMicroseconds(10); //this delay is required as well!
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;// convert the distance to centimeters.
if (distance < 25)/*if there’s an obstacle 25 centimers, ahead, do the following: */ {
Serial.println (“Close Obstacle detected!” );
Serial.println (“Obstacle Details:”);
Serial.print (“Distance From Robot is " );
Serial.print ( distance);
Serial.print ( " CM!”);// print out the distance in centimeters.

Serial.println (" The obstacle is declared a threat due to close distance. “)
;Serial.println (” Turning !");
servoLeft.write(0);
servoRight.write(90);

}
// else {
Serial.println (“No obstacle detected. going forward”);
delay (15);
void turnRight()
;servoLeft.write(180);
servoRight.write(0);
} //

this is my current program and it works fine but the problem is that the robot can only turn in one direction that is the reason i wanted to add another senor so it can turn in both directions

Hey bud, please use code tags for the code. They’re the </> symbol in the reply box.

Can you run through your hardware setup including what type of sensors you’re using? Looks like it might be ultrasonic?

So you just need to check both sensors.
If neither detect an object go forward.
If the left sensor detects an object but not the right, go right.
If the right sensor detects an object but not the left, go left.

Hi, HERE is an example of using one sensor to "look around" and then turn right, left or turn around.