DC motor spins when it shouldn't

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:



void loop() {
  // put your main code here, to run repeatedly:

  int left_light = analogRead(LEFT_LIGHT);
  int right_light = analogRead(RIGHT_LIGHT);


  Serial.print("Right: ");
  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);
    forward("left", left_speed);
  } else {

  Serial.print("\tLeft: ");
  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);
    forward("right", right_speed);
  } else {

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!

Besides the sketch a schematic of YOUR actual wiring would be helpful to those wanting to assist.

Could you also take a few moments to Learn How To Use The Forum.
It will help you get the best out of the forum in the future.
Other general help and troubleshooting advice can be found here.

  if (motor == "left") {

This is not how to compare strings - in general “left” != “left”. For C strings the == operator
compares the address in memory the string is stored at, not the strings themselves. For that you
need to use strcmp() function - check the docs.

You’d be better off using #define’d constants or enum’s to represent such tags, not strings, as equality
is well defined for these.