Arduino robot won't drive in this code.

Hello everyone,

I am making an Arduino robot that follows a line. I made this code:

#include <AFMotor.h>

int buttonpinL = 4;
int buttonpinR = 2;
int valL;
int valR;

AF_DCMotor motorLinks(1, MOTOR12_1KHZ);
AF_DCMotor motorRechts(2, MOTOR12_1KHZ);

void setup() {
  pinMode(buttonpinL,INPUT);
  pinMode(buttonpinR,INPUT);
  Serial.begin(9600);

}

void loop() {
  motorLinks.setSpeed(150);
  motorRechts.setSpeed(150);
  
  valL = digitalRead(buttonpinL);
  valR = digitalRead(buttonpinR);
  
  if(valL==HIGH && valR==LOW){ //right
    motorLinks.setSpeed(150);
    motorRechts.setSpeed(150);
    motorLinks.run(BACKWARD);
    motorRechts.run(FORWARD);
  }
  else if(valR==HIGH && valL==LOW) { //left
    motorLinks.setSpeed(150);
    motorRechts.setSpeed(150);
    motorLinks.run(FORWARD);
    motorRechts.run(BACKWARD);
  }

  else if (valR==HIGH && valL==HIGH) //forward
  {
    motorLinks.setSpeed(150);
    motorRechts.setSpeed(150);
    motorLinks.run(FORWARD);
    motorRechts.run(FORWARD);
  }
  else if(valR==LOW && valR==LOW){
    Serial.write("BOTH ARE LOW ");
    delay(3000);
  }
  else{
    motorLinks.setSpeed(150);
    motorRechts.setSpeed(150);
    motorLinks.run(FORWARD);
    motorRechts.run(FORWARD);
  }
  
}

The robot will not drive, I do not know why because if I just do:

#include <AFMotor.h>

AF_DCMotor motorLinks(1, MOTOR12_1KHZ);
AF_DCMotor motorRechts(2, MOTOR12_1KHZ);

void setup() {
  Serial.begin(9600);

}

void loop() {
    motorLinks.setSpeed(150);
    motorRechts.setSpeed(150);
    motorLinks.run(FORWARD);
    motorRechts.run(FORWARD);
  
}

The robot will just go Forward. So how can I implement this in my code? This is the first time I made a code myself, I love it!

Thanks for the help everyone.

Get your code to tell you what it is doing by using serial prints.

Hi, thanks for replying!
I already did it, and it keeps spamming "FORWARD". But the robot just doesn't want it haha!

We can’t see that code.

TheMemberFormerlyKnownAsAWOL:
Get your code to tell you what it is doing by using serial prints.

Made a screenshot too :slight_smile: