Hodzilla:
Hi everyone,I'm working on an Redbot Line follower with three line sensors. My task at hand is to program it to follow a track that goes from black, red and then blue. I have already made my measurements using the sensors and set the values.
The problem that I am having is getting it to follow the black line, it will follow the red and blue, but when it gets to black it stops.
I am using If else statements. The code in bold, when added, allows the Redbot to follow the black tape, but won't allow for the Redbot to follow the colored tape. But when I remove that section of code, the Redbot will follow the colored tape but not the black tape.
Any suggestions on what I could do?
#include <RedBot.h>
RedBotSensor left = RedBotSensor(A3); // initialize a left sensor object on A3
RedBotSensor center = RedBotSensor(A6); // initialize a center sensor object on A6
RedBotSensor right = RedBotSensor(A7); // initialize a right sensor object on A7
// constants that are used in the code. COLORLINETHRESHOLD is the level to detect
// if the sensor is on the line or not. If the sensor value is greater than this
// the sensor is above a DARK line.
//
// SPEED sets the nominal speed
#define COLORLINETHRESHOLD 690
#define BLACKLINETHRESHOLD 820
#define SPEED 60 // sets the nominal speed. Set to any number from 0 - 255.
RedBotMotors motors;void setup(){
Serial.begin(9600);
Serial.println("Welcome to experiment 6.2 -Line Following");
Serial.println("------------------------------------------");
delay(2000);
Serial.println("IR Sensor Readings: ");
delay(500);
}void loop() {
Serial.print(left.read());
Serial.print("\t"); // tab character
Serial.print(center.read());
Serial.print("\t"); // tab character
Serial.print(right.read());
Serial.println();
// if on the line drive left and right at the same speed (left is CCW / right is CW)
if((left.read() > COLORLINETHRESHOLD) && (center.read() > COLORLINETHRESHOLD) && (right.read() > COLORLINETHRESHOLD)) {
motors.stop();
}
else if((left.read() < COLORLINETHRESHOLD) && (center.read() < COLORLINETHRESHOLD) && (right.read() < COLORLINETHRESHOLD) ) {
motors.leftMotor(130);
motors.rightMotor(0);
}
else if (center.read() > COLORLINETHRESHOLD){
motors.leftMotor(100);
motors.rightMotor(100);
}
// if the line is under the right sensor, adjust relative speeds to turn to the right
else if (right.read() > COLORLINETHRESHOLD){
motors.leftMotor(130);
motors.rightMotor(0);
}
// if the line is under the left sensor, adjust relative speeds to turn to the left
else if (left.read() > COLORLINETHRESHOLD){
motors.leftMotor(0);
motors.rightMotor(130);
}
else if((left.read() < BLACKLINETHRESHOLD) && (center.read() < BLACKLINETHRESHOLD) && (right.read() < BLACKLINETHRESHOLD) ) {
** motors.leftMotor(130);**
** motors.rightMotor(0);**
** }**
** else if (center.read() > BLACKLINETHRESHOLD){**
** motors.leftMotor(100);**
** motors.rightMotor(100);**
** }**
// if the line is under the right sensor, adjust relative speeds to turn to the right
** else if (right.read() > BLACKLINETHRESHOLD){**
** motors.leftMotor(130);**
** motors.rightMotor(0);**
** }**
// if the line is under the left sensor, adjust relative speeds to turn to the left
** else if (left.read() > BLACKLINETHRESHOLD){**
** motors.leftMotor(0);**
** motors.rightMotor(130);**
** }**else {
motors.stop();
}
}
what happened if instead of
"else if((left.read() < BLACKLINETHRESHOLD) && (center.read() < BLACKLINETHRESHOLD) && (right.read() < BLACKLINETHRESHOLD) )",
you used
"if((left.read() < BLACKLINETHRESHOLD) && (center.read() < BLACKLINETHRESHOLD) && (right.read() < BLACKLINETHRESHOLD) ) "
basically create a second if/elseif statement for BLACKLINETHRESHOLD case