I have a problem with my Arduino project and I tried everything to fix it but nothing helps.

I am trying to rotate my DC motor with two buttons:

  • one button is to stop/start rotating on left side, and;
  • the second one is for stop/start rotating on the right side.

I can't switch the states and if the motor is rotating I can't stop it. The speed is controlled by the potentiometer and works fine, just the buttons are the problem. I hope somebody can help me.

int enablePin = 11;
int in1Pin = 10;
int in2Pin = 9;
int switchPin = 7;
int switchPin1 = 6;
int potPin = 0;
int statusPin= 13;
boolean status;

void setup()
  pinMode(in1Pin, OUTPUT);
  pinMode(in2Pin, OUTPUT);
  pinMode(enablePin, OUTPUT);

  pinMode(switchPin, INPUT_PULLUP);
  pinMode(switchPin1, INPUT_PULLUP);


void loop()

  int speed = analogRead(potPin) / 4;

  boolean reverse = digitalRead(switchPin);
  boolean reverse1 = digitalRead(switchPin1);

  if(reverse == false){
    status = true;
  if(reverse1 == false){
    status == false;

  setMotor(speed, status);

void setMotor(int speed, boolean reverse)
  analogWrite(enablePin, speed);

  digitalWrite(in1Pin, ! reverse);
  digitalWrite(in2Pin, reverse);

I just added the second button with paint, so don't get confused.

Fritzing diagram

  • Did you try a simple sketch that rotates right and an other one that rotates left to make sure that at least that was working ? Oct 20 '18 at 16:41
  • Also format and comment your code it will be a lot easier to read. Oct 20 '18 at 16:44

In your code the buttons only control the direction, but are not turning the motor on or off. This is only be done with the potentiometer, which can be set to zero speed.

To really enable the motor with the buttons, you have add some logic for the enable pin. For example change the setMotor() function and give it also a boolean as parameter, which indicates the enabling of the motor:

void setMotor(int speed, boolean enabled, boolean reverse)
    if(enabled) analogWrite(enablePin, speed);
    else        analogWrite(enablePin, 0);
    digitalWrite(in1Pin, ! reverse);
    digitalWrite(in2Pin, reverse);

You can set a corresponding boolean variable in the loop() function like you did with the status variable. Set this new variable to true in both if-statement of the buttons. Alternatively you can do this inline with the setMotor() call by writing:

setMotor(speed, reverse || reverse1, status);

The || is a logical OR operation. It returns true if any of both operands are true. In a similar way you can ditch the other if-statement with the status variable:

setMotor(speed, reverse || reverse1, !reverse && reverse1);

! negates the value of reverse and && does a logical AND. You can decide yourself, if you want to use if-statements (which are a bit easier to read or these logical statements (which are more compact).

Also you should use better variable names. reverse and reverse1 are not really good to distinguish. Better would be reverse and forward.

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.