Robot cleaner

Hi guys

I am new to this forum as well as for the Arduino Programming I wrote a code in order to make a robot with two motor and 3 ultrasound sensors. The code is listed below.

#include <AFMotor.h>
#define trigPinF 12
#define echoPinF 13
#define trigPinR 10
#define echoPinR 11
#define trigPinL 8
#define echoPinL 9
AF_DCMotor motor1(1,MOTOR12_64KHZ);
AF_DCMotor motor2(2, MOTOR12_8KHZ);

void setup() {
pinMode(trigPinF, OUTPUT);
pinMode(echoPinF, INPUT);

pinMode(trigPinR, OUTPUT);
pinMode(echoPinR, INPUT);

pinMode(trigPinL, OUTPUT);
pinMode(echoPinL, INPUT);
}

void loop() {

long durationF, distanceF;
digitalWrite(trigPinF, LOW);
delayMicroseconds(2);
digitalWrite(trigPinF, HIGH);

delayMicroseconds(10);
digitalWrite(trigPinF, LOW);
durationF = pulseIn(echoPinF, HIGH);
distanceF = (durationF/2) / 29.1;

long durationR, distanceR;
digitalWrite(trigPinR, LOW);
delayMicroseconds(2);
digitalWrite(trigPinR, HIGH);

delayMicroseconds(10);
digitalWrite(trigPinR, LOW);
durationR = pulseIn(echoPinR, HIGH);
distanceR = (durationR/2) / 29.1;

long durationL, distanceL;
digitalWrite(trigPinL, LOW);
delayMicroseconds(2);
digitalWrite(trigPinL, HIGH);

delayMicroseconds(10);
digitalWrite(trigPinL, LOW);
durationL = pulseIn(echoPinL, HIGH);
distanceL = (durationL/2) / 29.1;

if (distanceF < 20) {
motor1.setSpeed(100);
motor2.setSpeed(100);
motor1.run(BACKWARD);
motor2.run(FORWARD);
delay(2000); //CHANGE THIS ACCORDING TO HOW THE ROBOT TURNS.
}
else {
motor1.setSpeed(100); //CHANGE THIS ACCORDING TO HOW FAST YOUR ROBOT SHOULD GO.
motor2.setSpeed(100); //CHANGE THIS TO THE SAME VALUE AS YOU PUT IN ABOVE.
motor1.run(FORWARD);
motor2.run(FORWARD);
}

if (distanceR < 20) {
motor1.setSpeed(100);
motor2.setSpeed(100);
motor1.run(BACKWARD);
motor2.run(FORWARD);
delay(2000); //CHANGE THIS ACCORDING TO HOW THE ROBOT TURNS.
}
else {
motor1.setSpeed(100); //CHANGE THIS ACCORDING TO HOW FAST YOUR ROBOT SHOULD GO.
motor2.setSpeed(100); //CHANGE THIS TO THE SAME VALUE AS YOU PUT IN ABOVE.
motor1.run(FORWARD);
motor2.run(FORWARD);
}

if (distanceL < 20) {
motor1.setSpeed(100);
motor2.setSpeed(100);
motor1.run(BACKWARD);
motor2.run(FORWARD);
delay(2000); //CHANGE THIS ACCORDING TO HOW THE ROBOT TURNS.
}
else {
motor1.setSpeed(100); //CHANGE THIS ACCORDING TO HOW FAST YOUR ROBOT SHOULD GO.
motor2.setSpeed(100); //CHANGE THIS TO THE SAME VALUE AS YOU PUT IN ABOVE.
motor1.run(FORWARD);
motor2.run(FORWARD);
}
}

The main problem is that if test each single sensor everything is fine but when I uncomment everything the well do not turn as established by the code.

Is there someone that wants to help me? Please let me know and in case you can correct my code I would appreciate it?

Thanks

Hello and welcome to the forum!

First off, you should probably read the first three posts at the beginning of this section of the forum. Especially read the Read this before posting a programming question ... post. Pay attention to #6 - getting help on the forum and #7 - Debugging.

6 mentions wrapping your code with "code.../code" tags. Users of this forum expect to see code formatted with those tags, it's easier to read your code that way.

7 is about using the serial monitor and print statements to debug your code. You need to do this. Without getting an output from your code, there is no way to know what your code is doing.

In your case, I would start by putting print statements right before the first 'if' statement that would print the values of each sensor. Knowing what your sensor data is will help you trouble shoot the 'if then' parts of your code.

It's doubtful that you will find someone using the same sensors and motors that you are, so you'll have to trouble shoot this yourself. Maybe someone can spot a error in the logic of your code, or maybe someone won't. So you kinda have to narrow it down yourself. If you narrow it down to one sensors data should cause x-part of the code to execute, and it's not executing, then post up a specific question like that.

You will probably get better help that way.

Best of luck, Randy