Beginner - Works in simulation (tinkercad) not in real

I made a code to make a dc motor turn forward or reverse. When making it in tinkercad it works (motor is a led).

When uploding it to the uno it only goes forward. It only reacts to input 9. When pressing them both the motor doesn't do anything, as it should.
Where do i go wrong ?

Motor is connect to arduino by a motorshield.

int buttonState1 = 0;
int buttonState2 = 0;
int motor1pin1 = 10;
int motor1pin2 = 11;



void setup()
{
  pinMode(8, INPUT);
  pinMode(9, INPUT);
  pinMode(motor1pin1, OUTPUT);
  pinMode(motor1pin2, OUTPUT);
}

void loop()
{
  // read the state of the pushbutton value
  buttonState1 = digitalRead(8);
  buttonState2 = digitalRead(9);
  // check if pushbutton is pressed.  if it is, the
  // buttonState is HIGH
 if (buttonState1 == HIGH && buttonState2 == LOW) { 
    digitalWrite(motor1pin2, LOW);
    digitalWrite(motor1pin1, HIGH);
    }
 if (buttonState2 == HIGH && buttonState1 == LOW) {
    
    digitalWrite(motor1pin1, LOW);
    digitalWrite(motor1pin2, HIGH);
  } else {
   
   digitalWrite(motor1pin1, LOW);
   digitalWrite(motor1pin2, LOW);
  }
  delay(10); // Delay a little bit to improve simulation performance
}

How are your button switches wired? Have you a pullup or pulldown resistor? Without the resistors the pin can float at an unpredictable value

The usual way to use button switches is with pinMode(pin, INPUT_PULLUP); and then connect the switch so it pulls the pin to GND when pressed. Then digitalRead(switchPin); returns LOW when the switch is pressed.

...R

No idea what you mean,

Both buttons work when changed,, connected to the UNO with a resistor.

Can you give me an example what the correct way is to program this ?

petervanhanegem:
No idea what you mean,

Please make a simple pencil drawing showing how you have you have everything connected and post a photo of the drawing. See this Simple Image Posting Guide

...R

I think that your 'else' should not be an 'else' but an 'if' where you test if both buttons are not pressed.

sterretje:
I think that your 'else' should not be an 'else' but an 'if' where you test if both buttons are not pressed.

This did it. Thanks

Why would it have worked properly in a simulation?

fionning_macara:
Why would it have worked properly in a simulation?

I had been assuming the Tinkercad was not emulating the hardware accurately - which would not surprise me. Hardware can be difficult to emulate.

But it's very poor if it is not even emulating the software properly.

...R

petervanhanegem:
No idea what you mean,

Both buttons work when changed,, connected to the UNO with a resistor.

Can you give me an example what the correct way is to program this ?

There are several ways you could have connected your buttons. As it's now working correctly with the code correction, I assume you had both of them connected like this?

No idea what you mean,

Both buttons work when changed,, connected to the UNO with a resistor.

The Digital Read Serial Example shows you how to wire a switch with a pull-down resistor. The pull-down resistor (one for each switch/button) insures that the input is low when the button is not pushed. When the button is pushed the resistor is "overpowered" and the input goes high.

Or, there are built-in pull-up resistors that you can optionally enable, and then you don't have to add any external components. But, you have to wire the switch to ground instead of +5V, and you have to reverse the logic in software. In the "real world" pull-ups are more common than pull-downs but either will work, and since the Arduino bas built-in pull-ups that's an easier solution.


A couple of troubleshooting tips -

Add some Serial.prntln() messages so you can "see" what your program is doing. For example you can display a message about which buttons are pushed, and/or which direction the motor is running.

And, try running the motor both directions under software control without requiring a button push.

It's always a good idea to test the input (buttons) and output (motor) separately before putting everything together.

And, it's usually best to start as simple as possible (maybe by just reading/testing one button) and then add one or two lines of code at a time to "develop" your code, instead of trying to write the whole program at once.