Let's Make Robots!

Maze solving robot

i have a few errors in turnings.

the bot should make a turnAround only after going straight for a while and then meeting a dead end. But, it turns anytime, anywhere as soon as all the sensors read white.

here's my code.

 

 

 

#define rightFarSensor A0

#define rightNearSensor A1

#define centerSensor A2

#define leftNearSensor A3

#define leftFarSensor A4

 

int s1;

int s2;

int s3;

int s4;

int s5;

 

#define rightMotor1 6

#define rightMotor2 5

 

#define leftMotor1 10

#define leftMotor2 9

 

#define leapTime 100

 

void setup() {

  Serial.begin(115500);

 

  pinMode(rightMotor1, OUTPUT);

  pinMode(rightMotor2, OUTPUT);

  pinMode(leftMotor1, OUTPUT);

  pinMode(leftMotor2, OUTPUT);

}

 

void loop(){

 

 readSensors();                                                                                     

 

 if(s5<500 && s1<500 && (s2>500 || s3>500 || s4>500)){ 

    straight();                                                                                      

  }

  else{                                                                                              

    leftHandWall();                                                                                   

  }

 

}

 

  void readSensors() {

   s1 = analogRead(A0);

   s2 = analogRead(A1);

   s3 = analogRead(A2);

   s4 = analogRead(A3);

   s5 = analogRead(A4);

  // print out the value you read:

   Serial.print("s1: ");

 Serial.println(s1);

 Serial.print("s2: ");

 Serial.println(s2);

 

 Serial.print("s3: ");

 Serial.println(s3);

 

 Serial.print("s4: ");

 Serial.println(s4);

 Serial.print("s5: ");

 Serial.println(s5);

}

 

void leftHandWall(){

if( s5>500 && s1>500){

    analogWrite(leftMotor1, 180);

    analogWrite(leftMotor2, 0);

    analogWrite(rightMotor1, 180);

    analogWrite(rightMotor2, 0);

    delay(leapTime);

    readSensors();

 

    if(s5>500 || s1>500){

      done();

      delay(100000);

  }

  else {

    turnLeft();

  }

}

     else if(s5>500)

  {

  turnLeft();

  }

  else if(s1>500){

    analogWrite(leftMotor1, 180);

    analogWrite(leftMotor2, 0);

    analogWrite(rightMotor1, 180);

    analogWrite(rightMotor2, 0);

    delay(leapTime);

    readSensors();

    if(s3>500) {

      straight();

    }

    if(s1<500 && s5<500 && s3<500){

    turnRight();

  }

  straight();

  if(s1<500 && s2<500 && s3<500 && s4<500 && s5<500){

    turnAround();

  }

}

}

 

 

 

 

 

void done() {

analogWrite(leftMotor1, 0); 

analogWrite(leftMotor2, 0); 

 

analogWrite(rightMotor1, 0); 

analogWrite(rightMotor2, 0);

}

 

void straight(){

    analogWrite(leftMotor1, 180);

    analogWrite(leftMotor2, 0);

    analogWrite(rightMotor1, 180);

    analogWrite(rightMotor2, 0);

}

 

 

void turnAround(){

    analogWrite(leftMotor1, 180);

    analogWrite(leftMotor2, 0);

    analogWrite(rightMotor1, 180);

    analogWrite(rightMotor2, 0);

    delay(180);

   while(analogRead(A3)<500){

     analogWrite(leftMotor1, 0);

    analogWrite(leftMotor2, 180);

    analogWrite(rightMotor1, 180);

    analogWrite(rightMotor2, 0);

    delay(2);

     analogWrite(leftMotor1, 0);

    analogWrite(leftMotor2, 0);

    analogWrite(rightMotor1, 0);

    analogWrite(rightMotor2, 0);

    delay(1);

  }

}

 

 

void turnRight(){

     analogWrite(leftMotor1, 180);

    analogWrite(leftMotor2, 0);

    analogWrite(rightMotor1, 0);

    analogWrite(rightMotor2, 180);

}

 

 

void turnLeft(){

    analogWrite(leftMotor1, 0);

    analogWrite(leftMotor2, 180);

    analogWrite(rightMotor1, 180);

    analogWrite(rightMotor2, 0);

  }

 

 

 

 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Its the baud rate, dude.

You have your serial monitor set to 115,200 baud but in your code, you have specified, "115500". This is the issue.

If you have 5 spare digital I/O, add resistors and LEDs, then use those to see how your sensors are responding. That way you aren't guessing what is going on.

this one inclides stopping at the end....but again, bot prefers st. to left...and wont go right

 

 

#define rightFarSensor A0
#define rightNearSensor A1
#define centerSensor A2
#define leftNearSensor A3
#define leftFarSensor A4

const int RIGHTMOTOR1 = 6;
const int RIGHTMOTOR2 = 5;
const int LEFTMOTOR1 = 10;
const int LEFTMOTOR2 = 9;
const int LEAPTIME = 100;
const boolean BLACK = 0;
const boolean WHITE = 1;
const boolean DEBUG = false; // set to true to get serial read output

boolean rightFar;
boolean rightNear;
boolean center;
boolean leftNear;
boolean leftFar;

void setup() {
    Serial.begin(115200);
    pinMode(RIGHTMOTOR1, OUTPUT);
    pinMode(RIGHTMOTOR2, OUTPUT);
    pinMode(LEFTMOTOR1, OUTPUT);
    pinMode(LEFTMOTOR2, OUTPUT);
}

void loop() {
    readSensors();                                                                                     

    if (leftFar == WHITE && rightFar == WHITE && (rightNear == BLACK || 
        center == BLACK || leftNear == BLACK)) { 
        move(250, 0, 250, 0); // forward
    }

    else {                                                                                              
        leftHandWall();                                                                                   
    }
}

void readSensors() {
    int s1 = analogRead(rightFarSensor);
    int s2 = analogRead(rightNearSensor);
    int s3 = analogRead(centerSensor);
    int s4 = analogRead(leftNearSensor);
    int s5 = analogRead(leftFarSensor);
    
    // print out the value you read:
    if (DEBUG == true) {
        Serial.print("s1: ");
        Serial.println(s1);
        Serial.print("s2: ");
        Serial.println(s2);
        Serial.print("s3: ");
        Serial.println(s3);
        Serial.print("s4: ");
        Serial.println(s4);
        Serial.print("s5: ");
        Serial.println(s5);
    }
    
    if (s1 > 500) {
        rightFar = BLACK;
    }
    else {
        rightFar = WHITE;
    }
    
    if (s2 > 500) {
        rightNear = BLACK;
    }
    else {
        rightNear = WHITE;
    }
    
    if (s3 > 500) {
        center = BLACK;
    }
    else {
        center = WHITE;
    }
    
    if (s4 > 500) {
        leftNear = BLACK;
    }
    else {
        leftNear = WHITE;
    }
    
    if (s5 > 500) {
        leftFar = BLACK;
    }
    else {
        leftFar = WHITE;
    }
}

void leftHandWall() {
       if (leftFar == BLACK) {
         move(250, 0, 250, 0); //forward
         delay(LEAPTIME);
         readSensors();
         if (leftFar == BLACK && leftNear  == BLACK && center == BLACK && rightNear == BLACK && rightFar == BLACK) {
           move(0, 0, 0, 0); // motorstop
           delay(100000);
         }
         else {
		move(0, 250, 250, 0); // left
	}
       }
	else if (leftNear == BLACK || center == BLACK || rightNear == BLACK) {
   move(250, 0, 250, 0); //forward
         delay(LEAPTIME);
         readSensors();
         if (leftFar == BLACK && leftNear  == BLACK && center == BLACK && rightNear == BLACK && rightFar == BLACK) {
           move(0, 0, 0, 0); // motorstop
           delay(100000);
         }
         else {
		move(250, 0, 250, 0); // forward
}	
}
	else if (rightFar == BLACK) {
   move(250, 0, 250, 0); //forward
         delay(LEAPTIME);
         readSensors();
         if (leftFar == BLACK && leftNear  == BLACK && center == BLACK && rightNear == BLACK && rightFar == BLACK) {
           move(0, 0, 0, 0); // motorstop
           delay(100000);
         }
         else {
		move(250, 0, 0, 250); // right
	}
}
	else if (leftFar == WHITE && leftNear == WHITE && center == WHITE &&
			 rightNear == WHITE && rightFar == WHITE) {
		turnAround();
	}
}

void turnAround() {

    while(analogRead(center) == WHITE){
        move(0, 250, 250, 0); // left
    }
}
 
void move(int LM1, int LM2, int RM1, int RM2) {
    analogWrite(LEFTMOTOR1, LM1);
    analogWrite(LEFTMOTOR2, LM2);
    analogWrite(RIGHTMOTOR1, RM1);
    analogWrite(RIGHTMOTOR2, RM2);
}


void leftHandWall() {
    if (leftFar == BLACK) {
        stayOrGo('L');
    }
    else if (leftNear == BLACK || center == BLACK || rightNear == BLACK) {
        stayOrGo('F');
    }
    else if (rightFar == BLACK) {
        stayOrGo('R');
    }
    else if (leftFar == WHITE && leftNear == WHITE && center == WHITE &&
        rightNear == WHITE && rightFar == WHITE) {
        turnAround();
    }
}

void stayOrGo(char dir) {
    move(250, 0, 250, 0); //forward
    delay(LEAPTIME);
    readSensors();
    if (leftFar == BLACK && leftNear  == BLACK && center == BLACK &&
        rightNear == BLACK && rightFar == BLACK) {
        move(0, 0, 0, 0); // motorstop
        delay(100000);
    }
    else {
        switch (dir) {
            case 'L':
                move(0, 250, 250, 0); // left
                break;
            case 'F':
                move(250, 0, 250, 0); //forward
                break;
            case 'R':
                move(250, 0, 0, 250); // right
            default:
                move(0, 0, 0, 0); // stop
        }
    }
}

I can only say that it compiles.

here's a code that finally works better......however, the 180 turns are too slow.....

 

 

 

#define rightFarSensor A0
#define rightNearSensor A1
#define centerSensor A2
#define leftNearSensor A3
#define leftFarSensor A4

const int RIGHTMOTOR1 = 6;
const int RIGHTMOTOR2 = 5;
const int LEFTMOTOR1 = 10;
const int LEFTMOTOR2 = 9;
const int LEAPTIME = 100;
const boolean BLACK = 0;
const boolean WHITE = 1;
const boolean DEBUG = false; // set to true to get serial read output

boolean rightFar;
boolean rightNear;
boolean center;
boolean leftNear;
boolean leftFar;

void setup() {
    Serial.begin(115200);
    pinMode(RIGHTMOTOR1, OUTPUT);
    pinMode(RIGHTMOTOR2, OUTPUT);
    pinMode(LEFTMOTOR1, OUTPUT);
    pinMode(LEFTMOTOR2, OUTPUT);
}

void loop() {
    readSensors();                                                                                     

    if (leftFar == WHITE && rightFar == WHITE && (rightNear == BLACK || 
        center == BLACK || leftNear == BLACK)) { 
        move(250, 0, 250, 0); // forward
    }

    else {                                                                                              
        leftHandWall();                                                                                   
    }
}

void readSensors() {
    int s1 = analogRead(rightFarSensor);
    int s2 = analogRead(rightNearSensor);
    int s3 = analogRead(centerSensor);
    int s4 = analogRead(leftNearSensor);
    int s5 = analogRead(leftFarSensor);
    
    // print out the value you read:
    if (DEBUG == true) {
        Serial.print("s1: ");
        Serial.println(s1);
        Serial.print("s2: ");
        Serial.println(s2);
        Serial.print("s3: ");
        Serial.println(s3);
        Serial.print("s4: ");
        Serial.println(s4);
        Serial.print("s5: ");
        Serial.println(s5);
    }
    
    if (s1 > 500) {
        rightFar = BLACK;
    }
    else {
        rightFar = WHITE;
    }
    
    if (s2 > 500) {
        rightNear = BLACK;
    }
    else {
        rightNear = WHITE;
    }
    
    if (s3 > 500) {
        center = BLACK;
    }
    else {
        center = WHITE;
    }
    
    if (s4 > 500) {
        leftNear = BLACK;
    }
    else {
        leftNear = WHITE;
    }
    
    if (s5 > 500) {
        leftFar = BLACK;
    }
    else {
        leftFar = WHITE;
    }
}

void leftHandWall() {
       if (leftFar == BLACK) {
		move(0, 250, 250, 0); // left
	}
	else if (leftNear == BLACK || center == BLACK || rightNear == BLACK) {
		move(250, 0, 250, 0); // forward
	}
	else if (rightFar == BLACK) {
		move(250, 0, 0, 250); // right
	}
	else if (leftFar == WHITE && leftNear == WHITE && center == WHITE &&
			 rightNear == WHITE && rightFar == WHITE) {
		turnAround();
	}
}

void turnAround() {

    while(analogRead(center) == WHITE){
        move(0, 250, 250, 0); // left
    }
}
 
void move(int LM1, int LM2, int RM1, int RM2) {
    analogWrite(LEFTMOTOR1, LM1);
    analogWrite(LEFTMOTOR2, LM2);
    analogWrite(RIGHTMOTOR1, RM1);
    analogWrite(RIGHTMOTOR2, RM2);
}

here's a re-written code....but still not working...it makes a 180 turn....and has started going right as well.....still goes striaght over left...

 

#define rightFarSensor A0

#define rightNearSensor A1

#define centerSensor A2

#define leftNearSensor A3

#define leftFarSensor A4

 

const int RIGHTMOTOR1 = 6;

const int RIGHTMOTOR2 = 5;

const int LEFTMOTOR1 = 10;

const int LEFTMOTOR2 = 9;

const int LEAPTIME = 100;

const boolean BLACK = 0;

const boolean WHITE = 1;

const boolean DEBUG = false; // set to true to get serial read output

 

boolean rightFar;

boolean rightNear;

boolean center;

boolean leftNear;

boolean leftFar;

 

void setup() {

    Serial.begin(115200);

    pinMode(RIGHTMOTOR1, OUTPUT);

    pinMode(RIGHTMOTOR2, OUTPUT);

    pinMode(LEFTMOTOR1, OUTPUT);

    pinMode(LEFTMOTOR2, OUTPUT);

}

 

void loop() {

    readSensors();                                                                                     

 

    if (leftFar == WHITE && rightFar == WHITE && (rightNear == BLACK || 

        center == BLACK || leftNear == BLACK)) { 

        move(180, 0, 180, 0); // forward

    }

 

    else {                                                                                              

        leftHandWall();                                                                                   

    }

}

 

void readSensors() {

    int s1 = analogRead(rightFarSensor);

    int s2 = analogRead(rightNearSensor);

    int s3 = analogRead(centerSensor);

    int s4 = analogRead(leftNearSensor);

    int s5 = analogRead(leftFarSensor);

 

    // print out the value you read:

    if (DEBUG == true) {

        Serial.print("s1: ");

        Serial.println(s1);

        Serial.print("s2: ");

        Serial.println(s2);

        Serial.print("s3: ");

        Serial.println(s3);

        Serial.print("s4: ");

        Serial.println(s4);

        Serial.print("s5: ");

        Serial.println(s5);

    }

 

    if (s1 > 500) {

        rightFar = BLACK;

    }

    else {

        rightFar = WHITE;

    }

 

    if (s2 > 500) {

        rightNear = BLACK;

    }

    else {

        rightNear = WHITE;

    }

 

    if (s3 > 500) {

        center = BLACK;

    }

    else {

        center = WHITE;

    }

 

    if (s4 > 500) {

        leftNear = BLACK;

    }

    else {

        leftNear = WHITE;

    }

 

    if (s5 > 500) {

        leftFar = BLACK;

    }

    else {

        leftFar = WHITE;

    }

}

 

void leftHandWall() {

      if (leftFar == BLACK) {

        move(180, 0, 180, 0); // forward

        delay(LEAPTIME);

 

        readSensors();

 

        if (leftFar == BLACK && rightFar == BLACK) {

          move(0, 0, 0, 0);//stop

          delay(1000000);

        }

        else {

         move(0, 180, 180, 0); // left

    }

      }

    if (leftFar == WHITE && rightFar == BLACK) {

        move(180, 0, 180, 0); // inch

        delay(LEAPTIME);

 

        readSensors();

         if (rightFar == BLACK && leftFar == BLACK ) {

          move(0, 0, 0, 0);

          delay(1000000);

        }

        else if (center == BLACK) {

            move(180, 0, 180, 0); // forward

        }

 

    else {

        move(180, 0, 0, 180); // right

    }

    }//end of if(leftFar == BLACK && rightFar == BLACK)

 

else if (rightFar == WHITE && rightNear == WHITE && 

            center == WHITE && leftNear == WHITE && leftFar == WHITE) {

            turnAround();

            }

}

 

void turnAround() {

 

    while(analogRead(leftNearSensor) == WHITE){

        move(0, 180, 180, 0); // left

    }

}

 

void move(int LM1, int LM2, int RM1, int RM2) {

    analogWrite(LEFTMOTOR1, LM1);

    analogWrite(LEFTMOTOR2, LM2);

    analogWrite(RIGHTMOTOR1, RM1);

    analogWrite(RIGHTMOTOR2, RM2);

}

    if (leftFar == BLACK && leftNear == BLACK) {
		move(0, 180, 180, 0); // left
	}
	else if (leftNear == BLACK || center == BLACK || rightNear == BLACK) {
		move(180, 0, 180, 0); // forward
	}
	else if (rightNear == BLACK && rightFar == BLACK) {
		move(180, 0, 0, 180); // right
	}
	else if (leftFar == WHITE && leftNear == WHITE && center == WHITE &&
			 rightNear == WHITE && rightFar == WHITE) {
		turnAround();
	}
	else {
		move(0, 0, 0, 0); // stop
		delay(1000000);
	}

It compiles. I can't say any more than that. I don't specifically like the layout of the code, but, it follows what you wrote as closely as I can manage.

I hope it will help you locate it.

#define rightFarSensor A0
#define rightNearSensor A1
#define centerSensor A2
#define leftNearSensor A3
#define leftFarSensor A4

const int RIGHTMOTOR1 = 6;
const int RIGHTMOTOR2 = 5;
const int LEFTMOTOR1 = 10;
const int LEFTMOTOR2 = 9;
const int LEAPTIME = 100;
const boolean BLACK = 0;
const boolean WHITE = 1;
const boolean DEBUG = false; // set to true to get serial read output

boolean rightFar;
boolean rightNear;
boolean center;
boolean leftNear;
boolean leftFar;

void setup() {
    Serial.begin(115200);
    pinMode(RIGHTMOTOR1, OUTPUT);
    pinMode(RIGHTMOTOR2, OUTPUT);
    pinMode(LEFTMOTOR1, OUTPUT);
    pinMode(LEFTMOTOR2, OUTPUT);
}

void loop() {
    readSensors();                                                                                     

    if (leftFar == WHITE && rightFar == WHITE && (rightNear == BLACK || 
        center == BLACK || leftNear == BLACK)) { 
        move(180, 0, 180, 0); // forward
    }

    else {                                                                                              
        leftHandWall();                                                                                   
    }
}

void readSensors() {
    int s1 = analogRead(rightFarSensor);
    int s2 = analogRead(rightNearSensor);
    int s3 = analogRead(centerSensor);
    int s4 = analogRead(leftNearSensor);
    int s5 = analogRead(leftFarSensor);
    
    // print out the value you read:
    if (DEBUG == true) {
        Serial.print("s1: ");
        Serial.println(s1);
        Serial.print("s2: ");
        Serial.println(s2);
        Serial.print("s3: ");
        Serial.println(s3);
        Serial.print("s4: ");
        Serial.println(s4);
        Serial.print("s5: ");
        Serial.println(s5);
    }
    
    if (s1 > 500) {
        rightFar = BLACK;
    }
    else {
        rightFar = WHITE;
    }
    
    if (s2 > 500) {
        rightNear = BLACK;
    }
    else {
        rightNear = WHITE;
    }
    
    if (s3 > 500) {
        center = BLACK;
    }
    else {
        center = WHITE;
    }
    
    if (s4 > 500) {
        leftNear = BLACK;
    }
    else {
        leftNear = WHITE;
    }
    
    if (s5 > 500) {
        leftFar = BLACK;
    }
    else {
        leftFar = WHITE;
    }
}

void leftHandWall() {
    if (leftFar == BLACK && rightFar == BLACK) {
        move(180, 0, 180, 0); // forward
        delay(LEAPTIME);
        readSensors();         if (leftFar == BLACK || rightFar == BLACK) {             move(0, 0, 0, 0); // stop             delay(100000);         }     else {         move(0, 180, 180, 0); // left     }     }//end of if(leftFar == BLACK && rightFar == BLACK)     else if (leftFar == BLACK) {          move(0, 180, 180, 0); // left     }     else if (rightFar == BLACK) {         move(180, 0, 180, 0); // forward         delay(LEAPTIME);         readSensors();         if (center == BLACK) {             move(180, 0, 180, 0); // forward         }         if (rightFar == WHITE && leftFar == WHITE && center == WHITE) {             move(180, 0, 0, 180); // right         }                  move(180, 0, 180, 0); // forward            if (rightFar == WHITE && rightNear == WHITE &&             center == WHITE && leftNear == WHITE && leftFar == WHITE) {             turnAround();         }     }//end of else if(rightFar == BLACK) } void turnAround() {     move(180, 0, 180, 0); // forward     delay(180);     while(analogRead(leftNearSensor) == WHITE){         move(0, 180, 180, 0); // left         delay(2);         move(0, 0, 0, 0); // stop         delay(1);     } }   void move(int LM1, int LM2, int RM1, int RM2) {     analogWrite(LEFTMOTOR1, LM1);     analogWrite(LEFTMOTOR2, LM2);     analogWrite(RIGHTMOTOR1, RM1);     analogWrite(RIGHTMOTOR2, RM2); }

I am not sure if this will work. It does compile. It should react the same way your current code does. mogul commented on a better way to check the sensors, but, it would require more code rewriting.