Arduino bot with 2 servos, 2 sharp ir, and flame s

The code compiles but the bot only turns left. I tried changing the commands in the void loop so that the bot can also go right and straight depending on if there is an object in front of it. How can i change the code so the bot can roam around and take IR readings the whole time, not just after it turns 90 degrees.

#include <Servo.h>

#define irSensorPin 2 // IR sensor on this pin

#define irSensor2Pin 3 // IR sensor2 on this pin

//Main Variables needed in program

Servo leftservo; // Creates servo object for left side

Servo rightservo; // Creates Servo object for right side

int pos = 0; // variable to store the servo position

int avgFactor = 4; // amount of times you read the IR

int i; // counter for IR

int distRead = 0; // Var for the reading

int runningTotal = 0;// Total of readings

int distAvg = 0; // avg gdist readin

int distAvg2 = 0; // avg gdist readin

void setup()

{

Serial.begin(9600);

leftservo.attach(9); // attaches the servo on pin 9 to the servo object

rightservo.attach(10);

}

void IRREAD(){

i = 0; //counter

distAvg = 0;

runningTotal = 0;

for (i=0; i < avgFactor; i++) //sets up to read as many times as you want then average out readings

{

distRead = analogRead(irSensorPin);// Reads the value

runningTotal += distRead;// loads it into a tally

delay(50);// delay needed for nxt reading

}

distAvg = (runningTotal / avgFactor);// once reads are taken then avg them out

}

void IRREAD2()

{

i = 0; //counter

distAvg2 = 0;

runningTotal = 0;

for (i=0; i < avgFactor; i++) //sets up to read as many times as you want then average out readings

{

distRead = analogRead(irSensor2Pin); // Reads the value

runningTotal += distRead;// loads it into a tally

delay(50);// delay needed for the next reading

}

distAvg2 = (runningTotal / avgFactor);// once reads are taken then avg them out

}

void Stop(){

leftservo.write(94);

rightservo.write(93);

delay(5000);

}

void Forward(){

leftservo.write(255);

rightservo.write(0);

delay(1000);

//6inches = 1000 delay

}

void Right(){

leftservo.write(255);

rightservo.write(255);

delay(700);

//90Deg right = 700 delay

}

void Right45(){

leftservo.write(255);

rightservo.write(255);

delay(350);

//45Deg right = 350 Delay

}

void Left(){

leftservo.write(0);

rightservo.write(0);

delay(710);

//90Deg Left = 710 Delay

}

void Left45(){

leftservo.write(0);

rightservo.write(0);

delay(355);

//45Deg Left = 355 Delay

}

void loop()

{

Stop();

IRREAD();// calls the sensor

IRREAD2();// calls sensor2

//Foward();//calls function for bot movement foward

//Right();//calls function for bot movement 90deg right

//Left();

//Left45();

//Right45();

Serial.println(distAvg); //display the results

Serial.print(" ");

Serial.println(distAvg2);//displays the results

if (distAvg < 214){

Forward();

}

else if ((distAvg > 214 && distAvg < 230)&&(distAvg2 > 190 && distAvg2 < 204))

{

Left();

}

else if((distAvg > 282 && distAvg < 298)&&(distAvg2 > 267 && distAvg2 < 283)){

Left();

}

else if((distAvg > 421 && distAvg < 436)&&(distAvg2 > 396 && distAvg2 < 416)){

Left();

}

else ((distAvg > 520 && distAvg < 600)&&(distAvg2 > 565 && distAvg2 < 595));{
Left();

}

}

There are things you could to to shorten this code. IRREAD and IRREAD2 are identical except for the pin number to read from and where the value is stored. Create one function that takes a pin number as input, and returns the value.

Call that function once for each sensor, storing the returned value in the correct place.

The functions that move the robots contain delay statements. Nothing else can happen during a delay(). So, unless you get rid of them, you won't be able to make the robot move and scan at the same time.

In loop, there needs to be continuity in the if/else statements involving distAvg and distAvg. There are huge gaps in which the robot is to do nothing. This can't be right.

I know Thats why I need Help.

Thats why I need Help.

First, why are you doing this project? Is it a school assignment?

Second, what do you need help with? I've suggested some things to change. Can you make those changes?

There's a lot of information in the debug prints that you aren't sharing with us.

Yes its a school project. My group and I are begginners when it comes to code and were trying to work on it.

can i change the code so the bot can roam around and take IR readings the whole time, not just after it turns 90 degrees

Yes, but you need to get rid of all the "delay"s.
You need to think very carefully about how you want your bot to behave.

but the bot only turns left

Maybe because you don't have any "turn right" commands?

at this point were up for anything. were stuck. the whole project is that we put up a wall and we are using the UV Tron sensor. the robot goes around a wall the flame sensor senses a candle and goes to it and stops a few inches away.

The delay function is like an alarm clock. Set a time, and go to sleep until the alarm goes off.

You can't be checking the sensors while you're sleeping.

So, what can you do? Think about how you would get to school on time without an alarm clock (pretend that you do get to school on time). You'd check your watch, and see if it was time to go to school. If it was, you'd go. Otherwise, you'd do other stuff (like check the sensors). After a while (like when you got done reading the sensors, you'd check the time again.

There's no reason the turn the motors on, wait, and turn them off, is there? The only time to change the motor direction, speed, or status (on or off) is when one of the sensors says that there is something of interest in the way.

The functions (Right(), Left(), etc.) you have are good, except for turning things off after a period of time. Once you have decided to go in some direction, keep going that way until a sensor says not to. Or, turn in the best way, as determined by the various sensors, for a period of time (delay is OK here; no real need to look as you turn, as long as you don't spend an enormous amount of time turning) and then go straight (unlimited) until you encounter an obstacle.

Imagine how you avoid obstacles. Your eyes and ears are open all the time. You don't open your eyes, see that there is nothing in front of you, close your eyes and take three steps, then look again. But, this is what you are telling the robot to do.

How would u change the code, what would u suggest? since I am a beginner at programing.

I suggest you start using the serial monitor to verify the appropriate variables are being generated by the sensors for use in controlling the bot in each piece of the code. You can use dummy variables to see if the servos are being controlled as expected. Put the bot up on blocks and start testing to see where something is not as expected.

Cool Thanks, Will keep u posted!