Hey! I'm new to the Arduino and the forum so I apologize in advanced if I'm in the wrong sub-forum and or missing any question requirements.
I'm currently working on a Robot that has to solve a maze. I'm having difficulty getting the robot to keep a linear path throughout the maze. It likes to make awkward turns. I'm using 4 IR sensors which are just getting the distance from the walls and reading it through the ADC. Does anyone know of any stabilization code that could solve this issue or any tips to alleviate this problem? Thank you!
Hardware:
Romeo V2- an Arduino Robot Board (Arduino Leonardo) with Motor Driver
DFRobot 4WD Arduino Mobile Platform
Infrared Proximity Sensor - Sharp GP2Y0A21YK
void loop(void)
{
GetSensor();
Control();
}
void Control()
{
if(currentmotion == 1)
{
advance(80,80);
if (Sen_Val_R > 400)
{
stop();
turn_L(220,220);
delay(500);
turn_R(220,220);
delay(500);
advance(80,80);
delay (1000);
}
else if (Sen_Val_L > 400)
{
stop();
turn_R(220,220);
delay(500);
advance(80,80);
delay(1000);
}
if(Sen_Val_F > 200)
{
stop();
if(Sen_Val_L < 100)
{
while ((Sen_Val_B < 200)|| (Sen_Val_F > 50))
turn_L(220,220);
delay(750);
stop();
delay(500);
GetSensor();
}
stop();
delay(1000);
advance(100,100);
return;
}
if( Sen_Val_R < 100)
{
while ((Sen_Val_R < 100) || (Sen_Val_F > 90))
{
turn_R(220,220);
delay(750);
stop();
delay(500);
GetSensor();
}
stop();
delay(1000);
advance(80,80);
return;
}
return;
}
}
}
void setup(void)
{
Serial.begin(9600);
advance(80,80);
}void stop(void) //Stop
{
digitalWrite(E1,LOW);
digitalWrite(E2,LOW);
currentmotion = 0;
return;
}
void advance(char a,char b)//Move forward
{
analogWrite(E1,a);//PWM Speed Control
digitalWrite(M1,HIGH);
analogWrite(E2,b);
digitalWrite(M2,HIGH);
currentmotion = 1;
return;
}
void back_off (char a,char b)//Move backward
{
analogWrite(E1,a);
digitalWrite(M1,LOW);
analogWrite(E2,b);
digitalWrite(M2,LOW);
currentmotion = 2;
return;
}
void turn_L (char a,char b)//Turn Left
{
analogWrite (E1,a);
digitalWrite(M1,LOW);
analogWrite (E2,b);
digitalWrite(M2,HIGH);
currentmotion = 3;
return;
}
void turn_R (char a,char b) //Turn Right
{
analogWrite (E1,a);
digitalWrite(M1,HIGH);
analogWrite (E2,b);
digitalWrite(M2,LOW);
currentmotion = 4;
return;
}void GetSensor()
{
Sen_Val_B = SenAvg(Sensor_Back);
Sen_Val_F = SenAvg(Sensor_Front);
Sen_Val_R = SenAvg(Sensor_Right);
Sen_Val_L = SenAvg(Sensor_Left);
return;
}
int SenAvg(int SensorNumber)
{
Arr[100]= 0;
avg = 0;
for(int i = 0; i<100; i++)
{
Arr[i] = analogRead(SensorNumber);
avg = avg + Arr[i];
}
avg = avg/100;
return avg;
}