It is an obstacle avoidance program for the arduio bot.
#include <Servo.h>
#define irSensorPin 2 // IR sensor on this pin
#define irsensorPin 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 2);// 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(irSensorPin 3); // Reads the value
running total += distRead;// loads it into a tally
delay(50);// delay needed for the next reading
}
disAvg2 = (runningTotal / avgFactor);// once reads are taken then avg them out
}
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)
{
Foward();
)
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 if((distAvg > 520 && distAvg < 600)&&(distAvg2 > 565 && distAvg2 < 595)){
Left();
}
else;