I wrote the code for a line following robot recently and it's weird because the right wheel of the robot moves backwards and the left wheel moves forward when it's supposed to go forward. However the robot moves right and left properly(the wheels move how it's programmed to. I've used two infared receivers/transmitter modules. Please tell what's wrong!
The same thing happens!! I changed the code also. When a black line is detected(when the sensor doesn't detect anything) true/1 is returned whereas 0/false is returned when the sensor is above a lighter surface. Please tell me what's wrong
int const RS = A0; // Right sensor connected to pin A0
int const LS= A3; // Left sensor connected to pin A3
#include <AFMotor.h>
AF_DCMotor r_motor(3); // Right motor connected to port 3
AF_DCMotor l_motor(4); // Left motor connected to port 4
void setup(){
Serial.begin(9600);
pinMode(RS, INPUT);
pinMode(LS, INPUT);
//Intitializing pin A0 and A3 as inputs
}
void loop(){
bool r_state = digitalRead(A0);
bool l_state = digitalRead(A3);
if((r_state == false) && (l_state == false)){
Straight();
}
if((r_state == true) && (l_state == false)){
Turn_Right();
}
if((l_state == true) && (r_state == false)){
Turn_Left();
}
if(l_state && r_state ){
Stop();
}
}
void Straight(){
l_motor.setSpeed(200);
r_motor.setSpeed(200);
r_motor.run(FORWARD);
l_motor.run(FORWARD);
}
void Turn_Right(){
r_motor.setSpeed(200);
l_motor.setSpeed(200);
r_motor.run(FORWARD);
l_motor.run(BACKWARD);
}
void Turn_Left(){
l_motor.setSpeed(200);
r_motor.setSpeed(200);
l_motor.run(FORWARD);
r_motor.run(BACKWARD);
}
void Stop(){
l_motor.setSpeed(0);
r_motor.setSpeed(0);
r_motor.run(RELEASE);
l_motor.run(RELEASE);
}
I'm using the Adafruit motor shield the only way for me to connect the sensors is to connect it to the Analog pins. The sensors still work though. You think the problem is with the way the motors are connected?