I am trying to build a robot to navigate a maze. Right now I have three Sharp IR sensors one on the front and one on each side. RIght now the code will allow it to drive forward and when an opening on the right occurs it turns right. It will repeat this. but we are having a problem with reading another sensor. We went the front sensor to register when a wall is in front of it and than back up and turn right, but nothing happens when the front sensor is triggered. Val3 is the right sensor and Val1 is the front sensor names. Val 3 is connected to analog 2 and val1 is connected to analog 0. Can anyone explain why the front sensor isn't working? I know its something within my void loop but I cant figure out what.
void loop()
{
AllForward(); // All wheels forward
val1 = analogRead(sensorpin1); // reads the value of the sharp sensor
val2 = analogRead(sensorpin2); // reads the value of the sharp sensor
val3 = analogRead(sensorpin3); // reads the value of the sharp sensor
Serial.println(val1); // prints the value of the sensor to the serial monitor
Serial.println(val2); // prints the value of the sensor to the serial monitor
Serial.println(val3); // prints the value of the sensor to the serial monitor
if(val3>200) {// If the value is greater than 200 turn right
{
AllForward(); // All wheels forward
//delay(700); // Wait 0.11 seconds
}
{
if(val3 < 200) // If the right distance is greater than the left distance , turn right
{
delay(500);
turnRight();
if(turnRight)
AllForward1();
if(AllForward1)
AllForward;
if(val1>400) //if the front sensor triggers go back and turn right
GoBack();
if(GoBack);
turnRight;
}
}
}
Sorry still learning how to go about all of this. Here is my full code.
#include <AFMotor.h> // Enables the Motor library #include <Servo.h> // Enables the Servo library
int sensorpin1 = 0 ; // analog pin used to connect the sharp sensor
int sensorpin2 = 2;
int sensorpin3 = 3;
int val1 = 0; // variable to store the values from sensor(initially zero)
int val2 = 0; // variable to store the values from sensor(initially zero)
int val3 = 0; // variable to store the values from sensor(initially zero)
Servo PingServo;
AF_DCMotor motor1(1); // Motor 1 is connected to the port 1 on the motor shield
AF_DCMotor motor2(2); // Motor 2 is connected to the port 2 on the motor shield
AF_DCMotor motor3(3); // Motor 3 is connected to the port 1 on the motor shield
AF_DCMotor motor4(4); // Motor 4 is connected to the port 2 on the motor shield
void setup() {
motor1.setSpeed(215); // Sets the speed of the first motor (At 0, the motors are turned off. 255 is the fastest setting that you are able to use, I used 215 to not push the motors too hard.)
motor2.setSpeed(215); // Sets the speed of the second motor (At 0, the motors are turned off. 255 is the fastest setting that you are able to use, I used 215 to not push the motors too hard.)
motor3.setSpeed(215); // Sets the speed of the first motor (At 0, the motors are turned off. 255 is the fastest setting that you are able to use, I used 215 to not push the motors too hard.)
motor4.setSpeed(215); // Sets the speed of the second motor (At 0, the motors are turned off. 255 is the fastest setting that you are able to use, I used 215 to not push the motors too hard.)
Serial.begin(9600); // Enables Serial monitor for debugging purposes Serial.println("Serial test!"); // Test the Serial communication
}
void AllStop() {
motor1.run(RELEASE); // Turns off motor 1
motor2.run(RELEASE); // Turns off motor 2
motor3.run(RELEASE); // Turns off motor 3
motor4.run(RELEASE); // Turns off motor 4
}
void AllForward() { // Makes the robot go forward
motor1.run(FORWARD); // Motor 1 goes forward
motor2.run(BACKWARD); // Motor 2 goes forward
motor3.run(BACKWARD); // Motor 1 goes forward
motor4.run(FORWARD); // Motor 2 goes forward
}
void AllForward1() { // Makes the robot go forward
motor1.run(FORWARD); // Motor 1 goes forward
motor2.run(BACKWARD); // Motor 2 goes forward
motor3.run(BACKWARD); // Motor 1 goes forward
motor4.run(FORWARD); // Motor 2 goes forward
delay(700);
}
//Serial.println("Going forward"); // Prints a line in the serial monitor
void turnRight() { // Makes the robot go right
motor2.run(FORWARD); // Turns off motor 2
motor1.run(FORWARD); // Motor 1 goes forwardmotor2.run(BACKWARD); // Turns off motor 2motor2.run(BACKWARD); // Turns off motor 2
motor3.run(FORWARD); // Motor 1 goes forward
motor4.run(FORWARD); // Turns off motor 2
delay(1000); // Time required to turn right (1.6 seconds) Serial.println("Motors going Right"); // Prints a line in the serial monitor
}
void GoBack(){ // Makes the robot go back
motor2.run(FORWARD); // Motor 2 goes back
motor1.run(BACKWARD); // Motor 1 goes back
motor3.run(FORWARD); // Motor 2 goes back
motor4.run(BACKWARD); // Motor 1 goes back
delay(1000); // Time Required to go back (1.6 seconds) Serial.println("Backward"); // Prints a line in the serial monitor
}
void turnLeft() { // Makes the robot go Left
motor2.run(BACKWARD); // Motor 2 goes forward
motor1.run(BACKWARD); // turns off motor 1
motor4.run(BACKWARD); // Motor 2 goes forward
motor3.run(BACKWARD); // turns off motor 1
delay(1000); //Time Required to turn left (1.6)Seconds Serial.println("Motors going Left");// Prints a line in the serial monitor
}
// Starts the loop to decide what to do
void loop()
{
AllForward(); // All wheels forward
val1 = analogRead(sensorpin1); // reads the value of the sharp sensor
val2 = analogRead(sensorpin2); // reads the value of the sharp sensor
val3 = analogRead(sensorpin3); // reads the value of the sharp sensor Serial.println(val1); // prints the value of the sensor to the serial monitor Serial.println(val2); // prints the value of the sensor to the serial monitor Serial.println(val3); // prints the value of the sensor to the serial monitor
if(val3>200) {// If the value is greater than 200 turn right
{
AllForward(); // All wheels forward
//delay(700); // Wait 0.11 seconds
}
{
if(val3 < 200) // If the right distance is greater than the left distance , turn right
{
delay(500);
turnRight();
if(turnRight)
AllForward1();
if(AllForward1)
AllForward;
if(val1>400) //if the front sensor triggers go back and turn right
GoBack();
if(GoBack);
turnRight;
}
}
}
}