0

I have a problem with my line follower:

My robot has 4 wheels with DC motors connected to L293D shield (Arduino Uno).

The robot is a line follower controlled by IR pilot with an IR receiver, it has 4 IR sensors(two in the back and two in the front). The robot goes forward if none of sensors sees the black line.

When I click > button, the robot should use only the sensors in the front and follow the line until the end of the road made of black tape, and when I click < it should use only the sensors on the back and run CarBackward() (go backwards).

The robot should only stop working if I click 0 button on the pilot (CarStop() function). So without the IR remote code the robot drives normally, but when I add the code for the pilot only two right motors work, they spin forward and nothing else happens no matter if one, two or none sensors see the line.

Everything is powered by two 18650 batteries.

The IR pilot receiver is connected to A0 pin on the shield - pin 14 (which can be used as digital).

#include <IRremote.h>
#include <AFMotor.h>

int IRsensorPin = 14;
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);
    
IRrecv irrecv(IRsensorPin);
unsigned long key_value = 0;
decode_results results;

const int back_left_sensor_pin = A0;
const int left_sensor_pin = A1;
const int back_right_sensor_pin = A2;
const int right_sensor_pin = A3;
int left_sensor_state;
int back_left_sensor_state;
int right_sensor_state;
int back_right_sensor_state;
    
void CarBackward()
{
  if (back_left_sensor_state > 500 && back_right_sensor_state < 500) 
  {
    Serial.println("turn right");
    motor1.run(BACKWARD); // LP
    motor2.run(RELEASE); // LT
    motor3.run(BACKWARD); // PT
    motor4.run(FORWARD); // PP
              
    motor1.setSpeed(160);
    motor2.setSpeed(130);
    motor4.setSpeed(180);
    delay(20);
  }
              
  if (back_left_sensor_state < 500 && back_right_sensor_state > 500)
  {
    Serial.println("turn left");
    motor1.run(FORWARD); // LP
    motor2.run(BACKWARD); // LT
    motor3.run(RELEASE); // PT
    motor4.run(BACKWARD); // PP
          
    motor1.setSpeed(180);
    motor4.setSpeed(160);
    motor3.setSpeed(130);
    delay(20);
  }
          
  if (back_left_sensor_state < 500 && back_right_sensor_state < 500)
  {
    Serial.println("forward");
    motor1.run(BACKWARD); // LP
    motor2.run(BACKWARD); // LT
    motor3.run(BACKWARD); // PT
    motor4.run(BACKWARD); // PP
              
    motor1.setSpeed(110);
    motor2.setSpeed(110);
    motor3.setSpeed(110);
    motor4.setSpeed(110);
    delay(20);
  }
              
  if (back_left_sensor_state > 500 && back_right_sensor_state > 500)
  {
    Serial.println("stop");
    motor1.setSpeed(0);
    motor2.setSpeed(0);
    motor3.setSpeed(0);
    motor4.setSpeed(0);
    delay(20);
  }
}
    
void CarForward()
{
  if (right_sensor_state > 500 && left_sensor_state < 500) 
  {
    Serial.println("turn right");
    motor1.run(FORWARD); // LP
    motor2.run(FORWARD); // LT
    motor3.run(RELEASE); // PT
    motor4.run(BACKWARD); // PP
            
    motor1.setSpeed(160);
    motor2.setSpeed(130);
    motor4.setSpeed(180);
    delay(20);
  }
            
  if (right_sensor_state < 500 && left_sensor_state > 500)
  {
    Serial.println("turn left");
    motor1.run(BACKWARD); // LP
    motor2.run(RELEASE); // LT
    motor3.run(FORWARD); // PT
    motor4.run(FORWARD); // PP
            
    motor1.setSpeed(180);
    motor4.setSpeed(160);
    motor3.setSpeed(130);
    delay(20);   
  }
              
  if (right_sensor_state < 500 && left_sensor_state < 500)
  {
    Serial.println("forward");
    motor1.run(FORWARD); // LP
    motor2.run(FORWARD); // LT
    motor3.run(FORWARD); // PT
    motor4.run(FORWARD); // PP
            
    motor1.setSpeed(110);
    motor2.setSpeed(110);
    motor3.setSpeed(110);
    motor4.setSpeed(110);
    delay(20);
  }
            
  if (right_sensor_state > 500 && left_sensor_state > 500) 
  {
    Serial.println("stop");
    motor1.setSpeed(0);
    motor2.setSpeed(0);
    motor3.setSpeed(0);
    motor4.setSpeed(0);
    delay(20);
  }
}
    
void CarStop()
{
  Serial.println("stop");
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
  delay(200);
}
    
void setup()
{
  // put your setup code here, to run once:
  Serial.begin(9600);
  irrecv.enableIRIn();
  irrecv.blink13(true);
  motor1.setSpeed(130);
  motor2.setSpeed(130);
  motor3.setSpeed(130);
  motor4.setSpeed(130);  
}
    
void loop()
{
  left_sensor_state = analogRead(left_sensor_pin);
  right_sensor_state = analogRead(right_sensor_pin);
  back_left_sensor_state = analogRead(back_left_sensor_pin);
  back_right_sensor_state = analogRead(back_right_sensor_pin);

  static int wariat_na_lini = 1;  // if = 1 follow the line and go forward, if -1 go backwards
    
  if (irrecv.decode(&results)) // have we received an IR signal?
  {
    if (results.value == 0xFFFFFFFF) // if the value is equal to 0xFFFFFFFF
    {
      results.value = key_value; // set the value to the key value
    }
    switch (results.value) 
    {
      case 0xCBA816E1:
      case 0xC078:
        Serial.println("right button");
        wariat_na_lini = 1;
        break;
      case 0x43D69629:
      case 0xC0F8:
        Serial.println("left button");
        wariat_na_lini = -1;
        break;
      case 0xA6F5461D:
      case 0xC004:
        Serial.println("stop button");
        wariat_na_lini = 0;
        CarStop();
        break;
      default:
        Serial.println("different code");
        break;
    }
    key_value = results.value; // store the value as key_value
    irrecv.resume(); // reset the receiver for the next code
  }
    
  if (wariat_na_lini > 0) CarForward();
  else if (wariat_na_lini < 0) CarBackward();
}

So my question is - why are there only right motors working when the ir receiver is plugged?

This is my motor driver: this is my motor driver

3
  • You may want to edit your question to add in a link for your exact shield. – timemage Nov 16 '20 at 19:16
  • edited, i thought that it was obvious that i want to know why doesnt it work with the sensor in? i've added an image of this l293d motor driver – Wojtek Hawryluk Nov 16 '20 at 20:06
  • @WojtekHawryluk, it is possible that it wasn't needed. Then again, it may be that it factored into the answer you just received. – timemage Nov 16 '20 at 20:30
0

Your problem is most likely this line:

  irrecv.blink13(true);

That is intended to flash an LED on pin 13 when you use your IR remote. However pin 13 is also used by the motor shield as part of the serial protocol to communicate with the control chips.

By using that you are confusing the motor shield.

Just remove that line should allow the motors to work again.

1
  • 1
    didn't help, deleted this and still only right motor worked for a while, then turned off after a few seconds and i had to turn it off and on again. – Wojtek Hawryluk Nov 17 '20 at 12:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.