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();
}
}