Escaping robot

In this exercise, we add two light-dependent resistors on each side of the breadboard to simulate the eyes of a bug. Whenever the illumination intensity is beyond one certain level, it will begin to move forward. In terms of turning the direction, we measure the absolute value of the difference between 2 resistors. And base on this value, we can easily determine the right way to go.

The following is the flow chart of the code.

We add a circular platform on top of it to hold the breadboard.

 
And this is what it looks like with the add-on.

This is the demo video me leading the way of it.


 

And here are the codes.

int E1 = 5;     //M1 Speed Control
int E2 = 6;     //M2 Speed Control
int M1 = 4;    //M1 Direction Control
int M2 = 7;    //M1 Direction Control

void stop(void)                    //Stop
{
  digitalWrite(E1, LOW);
  digitalWrite(E2, LOW);
}
void advance(char a, char b)         //Move forward
{
  analogWrite (E1, a);     //PWM Speed Control
  digitalWrite(M1, HIGH);
  analogWrite (E2, b);
  digitalWrite(M2, HIGH);
}
void back_off (char a, char b)         //Move backward
{
  analogWrite (E1, a);
  digitalWrite(M1, LOW);
  analogWrite (E2, b);
  digitalWrite(M2, LOW);
}
void turn_L (char a, char b)            //Turn Left
{
  analogWrite (E1, a);
  digitalWrite(M1, LOW);
  analogWrite (E2, b);
  digitalWrite(M2, HIGH);
}
void turn_R (char a, char b)            //Turn Right
{
  analogWrite (E1, a);
  digitalWrite(M1, HIGH);
  analogWrite (E2, b);
  digitalWrite(M2, LOW);
}
void setup(void)
{
  int i;
  
  for (i = 4; i <= 7; i++)
    pinMode(i, OUTPUT);
  Serial.begin(19200);      //Set Baud Rate
  //Serial.println("Run keyboard control");
}
void loop(void)
{
  //100 is the min
  int sensorValue1 = analogRead(A0);
  int sensorValue2=analogRead(A1);
  Serial.println(sensorValue1);
  Serial.println(sensorValue2);
  delay(1);
  if(sensorValue1<300 && sensorValue2 <300 && abs(sensorValue2-sensorValue1)<35 ){
    advance(255, 255);
    delay(1);
  }else if(sensorValue2-sensorValue1 > 35 ){
    turn_R(100,100);
    delay(50);
  }else if(sensorValue1-sensorValue2 > 35){
    turn_L(100,100); 
    delay(50);
  }else{
    stop();
  }
}

This is the final mission and we successfully escape the labyrinth.

 

Leave a Reply

Your email address will not be published. Required fields are marked *