Code does IF statement even when IF statement isnt true?

After about 2-3 times of the loop running, the code goes to "abort" despite not pressing the button. Any idea why?

enum DIR_ENUM {
  DIR_UP = 0, // Black
  DIR_DN,     // Blue
  DIR_LT,     // Yellow
  DIR_RT,     // Green
const int buttonPin = 3;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

int buttonState = 0;         // variable for reading the pushbutton status

int DirectionPin[DIR_CNT] = {10, 8, 6, 4};
int OppositesPin[DIR_CNT] = {8, 10, 4, 6};
int Steps [100][2];
int Accumulation [DIR_CNT][2];

int Purple = 12;
boolean running = HIGH;

void setup() {
  // put your setup code here, to run once:
Steps[0][0] = DIR_UP;
Steps[0][1] = 2000; //delay for 2 seconds
Steps[1][0] = DIR_DN;
Steps[1][1] = 2000;
Steps[2][0] = DIR_LT;
Steps[2][1] = 4000;
Steps[3][0] = DIR_RT;
Steps[3][1] = 4000;

// initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);


void loop() {

  // put your main code here, to run repeatedly:
                 if (running) 
                  digitalWrite(ledPin, LOW);

                  digitalWrite(DirectionPin[Steps[0][0]], HIGH);    // Go UP for 2 seconds
                  digitalWrite(DirectionPin[Steps[0][0]], LOW);
                  digitalWrite(DirectionPin[Steps[1][0]], HIGH);    // Go DOWN for 2 seconds
                  digitalWrite(DirectionPin[Steps[1][0]], LOW);
                  digitalWrite(DirectionPin[Steps[2][0]], HIGH);    // Go LEFT for 4 seconds
                  digitalWrite(DirectionPin[Steps[2][0]], LOW);
                  digitalWrite(DirectionPin[Steps[3][0]], HIGH);    // Go RIGHT for 4 seconds
                  digitalWrite(DirectionPin[Steps[3][0]], LOW);


  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) 
    digitalWrite(ledPin, HIGH);



Depends on how the button is wired, which you did not mention. Do you have the required pullup or pulldown resistor?

I have a wire going to pin 3 and another wire connecting the button to ground through a resistor.

The input pin is floating (value undefined) when the button is not pressed.

Connect the button directly from the pin to ground, and use:

  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP);

The pin will read LOW when the button is pressed.

1 Like

Okay so you are saying no resistor is needed?

SCHEMATIC! It will take you a couple of minutes with pencil and paper and save us a lot of time not playing 20-questions.

Sorry. I am new to this. Please bare with me. I really appreciate it.
I've just now set my breadboard up just like this. It's still doing the same thing.

with 10kohm resistor

Your code said...

const int buttonPin = 3;

But you connected button to pin 2.


No. INPUT_PULLUP supplies the required resistor, internally. Wire the button from PIN 3 to GROUND.

Here are the options:

Yes I recently just changed my whole layout and changed the code to pin 2. That's not the issue sorry I didnt mention that.

And rewrite the IF conditional expression to LOW.

Ok changed the IF conditional expression to LOW.
Not sure if this setup is correct. I'm sorry I am a VERY beginner at this.


You need to connect diagonally across the button.

Does someone mind drawing it out for me? I'm just not understanding, and I'm not understanding how to come up with the right setup. Thank you!


Wire diagonally across the button leads at A and D or leads at B and C. You can't get it wrong and wire to two connected legs.

Got it. Yeah, I did that, and same result :frowning: not sure what im doing wrong

I might be saying the same thing again, but...

You said...

But you connected button to pin 3.

Please clarify which one use... :pensive:

Once I saw that wasn't the issue, I switched it back. The pin isnt the issue. That's the one thing I do know :slight_smile:

I have noticed the code works when I hold the button down.. ?? I'm not sure exactly what that means