Maze solver / Simple IR and Black Lines

Hi Guys

I have a nasty bug I can't get rid of.

Scenario: A Maze solving Robot (Parallax BoeBot) with 4 Infrared Sender / Receiver.

IR-Modules are called:

leftir (for left-infrared)/ midleftir (...Middle-left-Infrared)/ midrightir / rightir

Maze is black lines on white paper.

The Phase is the mapping Phase where it roams the maze with the "Left hand on the wall rule"

Navigation IS working, except for one special case:

If the two Middle-IR-Modules AND the RIGHT one go high, it turns left. There is NO section in the code where I told it to do so.
And it happens only on the way BACK. (But the bot does not know that yet, as I'm not yet saving the data.)

In fact, there is a section where i explicitly told it NOT to turn left but go straight:

   if(rightir == 1 && midrightir == 1 && midleftir == 1 && leftir == 0){
     forward();       
     delay (25);     
     
   }

But still, it sometimes goes left! And it goes the exact amount of left it would on a regular turn.
Btw. the above section is not needed, I just tried to fix it that way.

Summary:
Left-IR-Sensor = 0 / Midleft-IR-Sensor = 1 / Midright-IR-Sensor = 1 / Right-IR-Sensor = 1
---> Bot still goes left sometimes, around 90 Degrees (Not the small steps when it would correct it's path on the line, more like 1/4 of a circle). I believe it "thinks" it hit a left turn.

It's really puzzling me, I'd be very grateful if someone has some advice for me.

The whole Code:

#include <Servo.h>                           // Include servo library


//Servos  
Servo servoleft;                             
Servo servoright;

//IR Variables
int leftir;
int midleftir;
int midrightir;
int rightir;
 
void setup()                                
{
  servoleft.attach(13);                      
  servoright.attach(12);   
                
  //IR_Readers
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(4, INPUT);
  pinMode(5, INPUT);

  
  //Serial Communication
  Serial.begin(9600);
  
}  
 
void loop()                                  
{
  
  //Read IR Values
  leftir = digitalRead(5);
  delay(1);
  midleftir = digitalRead(4);
  delay(1);
  midrightir = digitalRead(3);
  delay(1);
  rightir = digitalRead(2);
  delay(1);
 
  
  //Line Follower Part (I have a wide Line, so only if BOTH Middle Sensors are High, it goes forward)
  if (midrightir == 1 && midleftir == 1){
    forward();       
    delay (25);  
  }
  
  if (midrightir == 1 && midleftir == 0){
    turnright();       
    delay (25);  
  }


  if (midleftir == 1 && midrightir == 0){
    turnleft();       
    delay (25);  
  }
  


  // turn left at left turn
  if (leftir == 1 && rightir == 0 && midleftir == 1 && midrightir == 1){
     forward();
     delay(330);
     turnleft();       
     delay (520); 
     stopbot();    
     delay (2000);     
  }

/*  // Turn right at right turn  ########### MAY ONLY BE USED _AFTER_ SOLVING MAZE!!!! Left-Hand-On-Wall-Rule!! ###########
  if ((rightir == 1 && leftir == 0) && (midleftir == 1 && midrightir == 1)){
     forward();
     delay(330);
     turnright();       
     delay (520); 
     stopbot();    
     delay (2000);  
  }
*/

  // turn left at intersection
  if (leftir == 1 && rightir == 1 && midleftir == 1 && midrightir == 1){
     forward();
     delay(330);
     turnleft();       
     delay (520); 
     stopbot();    
     delay (2000);     
  }  


  //Turn at the end of a line
  if (rightir == 0 && midrightir == 0 && midleftir == 0 && leftir == 0){
   stopbot();
   delay(15);
   turnleft();       
   delay (80);
   }  

   //SHOULD NOT turn left when hitting a path to the right - But still does.
   if(rightir == 1 && midrightir == 1 && midleftir == 1 && leftir == 0){
     forward();       
     delay (25);     
     
   }

  
}



void forward(){
  servoleft.writeMicroseconds(1800);       
  servoright.writeMicroseconds(1200); 
}

void backward(){
  servoleft.writeMicroseconds(1200);       
  servoright.writeMicroseconds(1800);  
}

void turnright(){
  servoleft.writeMicroseconds(1800);         
  servoright.writeMicroseconds(1800);        
}

void turnleft(){
  servoleft.writeMicroseconds(1200);         
  servoright.writeMicroseconds(1200);      
}

void stopbot(){
  servoleft.writeMicroseconds(1500);         
  servoright.writeMicroseconds(1500);        
  
}

void stopservo(){
  servoleft.detach();                   
  servoright.detach();  
}

Some telemetry would be nice. Either serial.print, assuming you can run the bot with a usb cable attached, or record the data to an SD card.

Scratch this one. It was a Hardware Error. And by the "Hardware" i mean the MAZE.

I did the same maze again on a different surface, and the code runs flawlessly. I suppose it had a reflection or blind spot or something that threw the sensors off.

So anyone using Parallax Boebot and Linefollower-Kit: This Code is good.

Obviously not complete, but maze-solving with left-hand-on-wall rule is working as it should.

Thank you anyways wildbill. We could have spent a long time looking at telemetry. ^^

   if(rightir == 1 && midrightir == 1 && midleftir == 1 && leftir == 0){

Boolean variables would make this some much simpler to understand:

   if(rightir && midrightir  && midleftir && !leftir)
   {

Camel case names would make it easier to read, too.

Vote against camelCase and in favour of multi_part_var_names... Much easier to read :slight_smile: