IR Sensors with Robot control

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

Hi,
Welcome to the forum.

It would be good if we can see your code as far as you have got so we can suggest edits to solve your problem.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

A list of the hardware you are using will help too. Arduino model, motor driver/shield , power supply etc.

Thanks.. Tom... :slight_smile:

These sensors are not very reliable, their readings are affected by many factors. I'd suggest that you use kind of a filter, to detect and handle the situations that make the robot turn unexpectedly. As a first step you could log the sensor readings, e.g. plot them in Serial Plotter, to find out more about the inputs.

Hi,

Please don’t edit previous posts to answer later posts.
It disturbs the flow of the the thread.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks for info… Tom… :slight_smile: