I've been learning Arduino through Jeremy Blum's book and got stuck at the Roving Robot project, where two photoresistors control the movement of two DC motors. The Right DC motor spins even when it shouldn't, but the Left DC motor works as expected. The Right DC motor spins when the analog values from the photoresistors are lower than the lower-bound threshold I set and the serial monitor shows that the speed should be 0, as well as when I tried to troubleshoot and only run the setup section where both motors should brake. The only way I've got it to stop spinning is if in the brake function I leave the ENABLE pin at LOW after turning the two INPUT pins (I named them SWITCH in the code) LOW. I've tripled checked my wiring and code and am at a lost here.
** I've also swapped the Left and Right DC motors. The problem persists with the Right DC motor, so the issue doesn't seem to be with the physical motor itself.
Some specs: I'm using Arduino UNO, a L293D H-Bridge, a L7805CV Linear Regulator to output 5V from a 9V battery input, and two 10uF Electrolytic Capacitors.
const int RIGHT_LIGHT = 0;
const int LEFT_LIGHT = 1;
const int RIGHT_ENABLE = 9;
const int LEFT_ENABLE = 10;
const int LEFT_SWITCH1 = 4;
const int LEFT_SWITCH2 = 5;
const int RIGHT_SWITCH1 = 2;
const int RIGHT_SWITCH2 = 3;
const int LIGHT_THRESHOLD_MIN = 1000;
const int LIGHT_THRESHOLD_MAX = 1500;
const int SPEED_MIN = 150;
const int SPEED_MAX = 255;
void setup() {
// put your setup code here, to run once:
pinMode(LEFT_ENABLE, OUTPUT);
pinMode(RIGHT_ENABLE, OUTPUT);
pinMode(LEFT_SWITCH1, OUTPUT);
pinMode(LEFT_SWITCH2, OUTPUT);
pinMode(RIGHT_SWITCH1, OUTPUT);
pinMode(RIGHT_SWITCH2, OUTPUT);
brake("left");
brake("right");
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
int left_light = analogRead(LEFT_LIGHT);
int right_light = analogRead(RIGHT_LIGHT);
delay(500);
Serial.print("Right: ");
Serial.print(right_light);
Serial.print(" ");
if (right_light >= LIGHT_THRESHOLD_MIN) {
int left_speed = map(right_light, LIGHT_THRESHOLD_MIN, LIGHT_THRESHOLD_MAX, SPEED_MIN, SPEED_MAX);
left_speed = constrain(left_speed, SPEED_MIN, SPEED_MAX);
Serial.print(left_speed);
forward("left", left_speed);
} else {
Serial.print("0");
brake("left");
}
Serial.print("\tLeft: ");
Serial.print(left_light);
Serial.print(" ");
if (left_light >= LIGHT_THRESHOLD_MIN) {
int right_speed = map(left_light, LIGHT_THRESHOLD_MIN, LIGHT_THRESHOLD_MAX, SPEED_MIN, SPEED_MAX);
right_speed = constrain(right_speed, SPEED_MIN, SPEED_MAX);
Serial.print(right_speed);
forward("right", right_speed);
} else {
Serial.println("0");
brake("right");
}
}
void forward(String motor, int rate) {
if (motor == "left") {
digitalWrite(LEFT_ENABLE, LOW);
digitalWrite(LEFT_SWITCH1, HIGH);
digitalWrite(LEFT_SWITCH2, LOW);
analogWrite(LEFT_ENABLE, rate);
} else if (motor == "right") {
digitalWrite(RIGHT_ENABLE, LOW);
digitalWrite(RIGHT_SWITCH1, HIGH);
digitalWrite(RIGHT_SWITCH2, LOW);
analogWrite(RIGHT_ENABLE, rate);
}
}
void brake(String motor) {
if (motor == "left") {
digitalWrite(LEFT_ENABLE, LOW);
digitalWrite(LEFT_SWITCH1, LOW);
digitalWrite(LEFT_SWITCH2, LOW);
digitalWrite(LEFT_ENABLE, HIGH);
} else if (motor == "right") {
digitalWrite(RIGHT_ENABLE, LOW);
digitalWrite(RIGHT_SWITCH1, LOW);
digitalWrite(RIGHT_SWITCH2, LOW);
digitalWrite(RIGHT_ENABLE, HIGH);
}
}
This is something I'd see in my serial monitor. The first number is the analog light value and the second is the speed. Even though the speed is 0 as it should be, the right motor spins anyways.
Right: 754 0 Left: 568 0
Right: 755 0 Left: 569 0
Right: 753 0 Left: 567 0
Right: 753 0 Left: 564 0
I'd appreciate any help. Thanks!