malteasy
malteasy

Reputation: 87

The right motor on my line sensor Arduino robot runs even when the requirements to run it are not met

So in this Arduino code for a line sensor robot using RedBot line sensors, the right motor seems to be running even when both sensors are detecting black (a line). This does not happen on the right motor so I suspect it has something to do with the first "if" statement.

The motor does stop however when only the right sensor detects black and this makes only the left motor run. I'm not sure what the problem is that is making the right motor run even when its condition isn't met and this is not a problem with the sensor btw. I am using an L293D motor driver to control the motors but I doubt it has anything to do with that.

#include <RedBot.h>

RedBotSensor right_sen = RedBotSensor(A5);  
RedBotSensor left_sen = RedBotSensor(A4); 
int lineStandard = 650;
int motorPinRight = 12;
int motorPinLeft = 10;

void setup()  
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(motorPinRight, OUTPUT);
  pinMode(motorPinLeft, OUTPUT);
  Serial.begin(9600);
  Serial.println("IR Sensor Readings:: ");
  delay(1000);
}

void loop() {
  if(left_sen.read() > lineStandard) {
   left();
   digitalWrite(LED_BUILTIN, HIGH);
  } else if(right_sen.read() > lineStandard) {
    right();
    digitalWrite(LED_BUILTIN, HIGH);
  } else if((left_sen.read() < lineStandard) && (right_sen.read() < lineStandard)) {
    forward();
    digitalWrite(LED_BUILTIN, LOW);
  } else if((left_sen.read() > lineStandard) && (right_sen.read() > lineStandard)) {
    halt();
    digitalWrite(LED_BUILTIN, HIGH);
  } else {
    halt();
  }
  delay(10);
}

void forward() {
  digitalWrite(motorPinRight, HIGH);
  digitalWrite(motorPinLeft, HIGH);
}

void left() {
  digitalWrite(motorPinRight, HIGH);
  digitalWrite(motorPinLeft, LOW);
}

void right() {
  digitalWrite(motorPinRight, LOW);
  digitalWrite(motorPinLeft, HIGH);
}

void halt() {
  digitalWrite(motorPinRight, LOW);
  digitalWrite(motorPinLeft, LOW);
}

Upvotes: 1

Views: 105

Answers (1)

Micha&#235;l Roy
Micha&#235;l Roy

Reputation: 6481

There is something wrong with the order of your tests:

 if(left_sen.read() > lineStandard){ /* ...*/ }
 else if(right_sen.read() > lineStandard){ /* ...*/ }
 else if((left_sen.read() < lineStandard) && (right_sen.read() < lineStandard)) { /* ...*/ }
 else if((left_sen.read() > lineStandard) && (right_sen.read() > lineStandard)) 
     { /* this block cannot be reached, because of first test  */ 
       /* should be moved to first position, because it's more */
       /* restrictive than both tests 1 and 2                  */ }
 else { /* ...*/ }

To avoid this problem, you should reorder your tests starting with the most restrictive conditions first, or rewrite your code using a switch statement, which will make the code clearer.

#define LEFT_DETECT  (1 << 0)
#define RIGHT_DETECT (1 << 1)
// ...


void movementControl()
{
    char detect;
    detect = (left_sen.read() > lineStandard) ? LEFT_DETECT : 0;
    detect += (right_sen.read() > lineStandard) ? RIGHT_DETECT : 0;

    switch(detect)
    {
    case LEFT_DETECT:
        left(); 
        digitalWrite(LED_BUILTIN, HIGH);  
        break;

    case RIGHT_DETECT:
        right();
        digitalWrite(LED_BUILTIN, HIGH);  
        break;

    default:
    case LEFT_DETECT + RIGHT_DETECT:
         halt();
         break;

    case 0:
        forward();
        digitalWrite(LED_BUILTIN, LOW);  
        break;
    }
}

Upvotes: 1

Related Questions