# Code does IF statement even when IF statement isnt true?

**URL:** <https://forum.arduino.cc/t/code-does-if-statement-even-when-if-statement-isnt-true/892131>\
**Category:** Programming\
**Created:** [August 3, 2021, 10:06pm UTC](https://forum.arduino.cc/t/code-does-if-statement-even-when-if-statement-isnt-true/892131 "2021-08-03T22:06:19Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jj0932](https://avatars.discourse-cdn.com/v4/letter/j/fbc32d/32.png) [@jj0932](https://forum.arduino.cc/u/jj0932)\
**Post date:** [August 3, 2021, 10:06pm UTC](https://forum.arduino.cc/t/code-does-if-statement-even-when-if-statement-isnt-true/892131/1 "2021-08-03T22:06:19Z")

</div>

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

```cpp
enum DIR_ENUM {
  DIR_UP = 0, // Black
  DIR_DN, // Blue
  DIR_LT, // Yellow
  DIR_RT, // Green
  DIR_CNT
};
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:
  Serial.begin(9600);
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);

                 
                  //UP
                  Serial.println("UP");
                  digitalWrite(DirectionPin[Steps[0][0]], HIGH); // Go UP for 2 seconds
                  delay(Steps[0][1]);
                  digitalWrite(DirectionPin[Steps[0][0]], LOW);
                 
                  //DOWN
                  Serial.println("DOWN");
                  digitalWrite(DirectionPin[Steps[1][0]], HIGH); // Go DOWN for 2 seconds
                  delay(Steps[1][1]);
                  digitalWrite(DirectionPin[Steps[1][0]], LOW);
                  
                  //LEFT
                  Serial.println("LEFT");
                  digitalWrite(DirectionPin[Steps[2][0]], HIGH); // Go LEFT for 4 seconds
                  delay(Steps[2][1]);
                  digitalWrite(DirectionPin[Steps[2][0]], LOW);
                  
                  //RIGHT
                  Serial.println("RIGHT");
                  digitalWrite(DirectionPin[Steps[3][0]], HIGH); // Go RIGHT for 4 seconds
                  delay(Steps[3][1]);
                  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) 
  
  {
    Serial.println("abort");
    digitalWrite(ledPin, HIGH);
    while(1);

  } 

  
   }

```

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [August 3, 2021, 10:09pm UTC](https://forum.arduino.cc/t/code-does-if-statement-even-when-if-statement-isnt-true/892131/2 "2021-08-03T22:09:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jj0932](https://avatars.discourse-cdn.com/v4/letter/j/fbc32d/32.png) [@jj0932](https://forum.arduino.cc/u/jj0932)\
**Post date:** [August 3, 2021, 10:11pm UTC](https://forum.arduino.cc/t/code-does-if-statement-even-when-if-statement-isnt-true/892131/3 "2021-08-03T22:11:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [August 3, 2021, 10:13pm UTC](https://forum.arduino.cc/t/code-does-if-statement-even-when-if-statement-isnt-true/892131/4 "2021-08-03T22:13:42Z")

</div>

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

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

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

```

The pin will read LOW when the button is pressed.

---

<div class="post-metadata">

**Author:** ![jj0932](https://avatars.discourse-cdn.com/v4/letter/j/fbc32d/32.png) [@jj0932](https://forum.arduino.cc/u/jj0932)\
**Post date:** [August 3, 2021, 10:17pm UTC](https://forum.arduino.cc/t/code-does-if-statement-even-when-if-statement-isnt-true/892131/6 "2021-08-03T22:17:40Z")

</div>

Okay so you are saying no resistor is needed?

---

<div class="post-metadata">

**Author:** ![SteveMann](https://avatars.discourse-cdn.com/v4/letter/s/5e9695/32.png) [@SteveMann](https://forum.arduino.cc/u/SteveMann)\
**Post date:** [August 3, 2021, 10:34pm UTC](https://forum.arduino.cc/t/code-does-if-statement-even-when-if-statement-isnt-true/892131/7 "2021-08-03T22:34:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jj0932](https://avatars.discourse-cdn.com/v4/letter/j/fbc32d/32.png) [@jj0932](https://forum.arduino.cc/u/jj0932)\
**Post date:** [August 3, 2021, 10:44pm UTC](https://forum.arduino.cc/t/code-does-if-statement-even-when-if-statement-isnt-true/892131/8 "2021-08-03T22:44:25Z")

</div>

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.

 ![Screen Shot 2021-08-03 at 5.42.47 PM](https://europe1.discourse-cdn.com/arduino/original/4X/2/9/2/29223fd75bccac84e0f5450ddc72e50a87e0e14a.png)

---

<div class="post-metadata">

**Author:** ![jj0932](https://avatars.discourse-cdn.com/v4/letter/j/fbc32d/32.png) [@jj0932](https://forum.arduino.cc/u/jj0932)\
**Post date:** [August 3, 2021, 10:46pm UTC](https://forum.arduino.cc/t/code-does-if-statement-even-when-if-statement-isnt-true/892131/9 "2021-08-03T22:46:28Z")

</div>

with 10kohm resistor

---

<div class="post-metadata">

**Author:** ![chrisknightley](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/chrisknightley/32/637257_2.png) [@chrisknightley](https://forum.arduino.cc/u/chrisknightley)\
**Post date:** [August 3, 2021, 10:51pm UTC](https://forum.arduino.cc/t/code-does-if-statement-even-when-if-statement-isnt-true/892131/10 "2021-08-03T22:51:54Z")

</div>

Your code said...

```cpp
const int buttonPin = 3;

```

But you connected button to pin 2.

![image](https://europe1.discourse-cdn.com/arduino/original/4X/7/1/2/712f5e08ae0014832ff21733b933002bbe721a65.png)  
🙄

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [August 3, 2021, 10:52pm UTC](https://forum.arduino.cc/t/code-does-if-statement-even-when-if-statement-isnt-true/892131/11 "2021-08-03T22:52:40Z")

</div>

> [@jj0932](#):
>
> Okay so you are saying no resistor is needed?

No. INPUT\_PULLUP supplies the required resistor, internally. Wire the button from PIN 3 to GROUND.

Here are the options:

 ![](https://europe1.discourse-cdn.com/arduino/original/4X/f/8/c/f8c299c8c8e63f1fbce2ced640499757b4159ed8.jpeg)

---

<div class="post-metadata">

**Author:** ![jj0932](https://avatars.discourse-cdn.com/v4/letter/j/fbc32d/32.png) [@jj0932](https://forum.arduino.cc/u/jj0932)\
**Post date:** [August 3, 2021, 10:53pm UTC](https://forum.arduino.cc/t/code-does-if-statement-even-when-if-statement-isnt-true/892131/12 "2021-08-03T22:53:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![chrisknightley](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/chrisknightley/32/637257_2.png) [@chrisknightley](https://forum.arduino.cc/u/chrisknightley)\
**Post date:** [August 3, 2021, 10:54pm UTC](https://forum.arduino.cc/t/code-does-if-statement-even-when-if-statement-isnt-true/892131/13 "2021-08-03T22:54:55Z")

</div>

> [@anon94801460](#):
>
> No. INPUT\_PULLUP supplies the required resistor, internally. Wire the button from PIN 3 to GROUND.

And rewrite the IF conditional expression to LOW.

---

<div class="post-metadata">

**Author:** ![jj0932](https://avatars.discourse-cdn.com/v4/letter/j/fbc32d/32.png) [@jj0932](https://forum.arduino.cc/u/jj0932)\
**Post date:** [August 3, 2021, 11:02pm UTC](https://forum.arduino.cc/t/code-does-if-statement-even-when-if-statement-isnt-true/892131/14 "2021-08-03T23:02:17Z")

</div>

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.

 ![IMG_8394](https://europe1.discourse-cdn.com/arduino/original/4X/2/9/5/2958a78b3222e56a68dec508a12a72d7a2b43146.jpeg)

---

<div class="post-metadata">

**Author:** ![cattledog](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cattledog/32/217816_2.png) [@cattledog](https://forum.arduino.cc/u/cattledog)\
**Post date:** [August 3, 2021, 11:03pm UTC](https://forum.arduino.cc/t/code-does-if-statement-even-when-if-statement-isnt-true/892131/15 "2021-08-03T23:03:37Z")

</div>

> [@jj0932](#):
>
> Not sure if this setup is correct.

No.

You need to connect diagonally across the button.

---

<div class="post-metadata">

**Author:** ![jj0932](https://avatars.discourse-cdn.com/v4/letter/j/fbc32d/32.png) [@jj0932](https://forum.arduino.cc/u/jj0932)\
**Post date:** [August 3, 2021, 11:06pm UTC](https://forum.arduino.cc/t/code-does-if-statement-even-when-if-statement-isnt-true/892131/16 "2021-08-03T23:06:37Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![cattledog](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cattledog/32/217816_2.png) [@cattledog](https://forum.arduino.cc/u/cattledog)\
**Post date:** [August 3, 2021, 11:19pm UTC](https://forum.arduino.cc/t/code-does-if-statement-even-when-if-statement-isnt-true/892131/17 "2021-08-03T23:19:00Z")

</div>

![pushbutton](https://europe1.discourse-cdn.com/arduino/original/4X/7/9/c/79c705b7ea2941870d0908a5f6166f687f6d8e8c.jpeg)

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.

---

<div class="post-metadata">

**Author:** ![jj0932](https://avatars.discourse-cdn.com/v4/letter/j/fbc32d/32.png) [@jj0932](https://forum.arduino.cc/u/jj0932)\
**Post date:** [August 3, 2021, 11:41pm UTC](https://forum.arduino.cc/t/code-does-if-statement-even-when-if-statement-isnt-true/892131/18 "2021-08-03T23:41:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chrisknightley](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/chrisknightley/32/637257_2.png) [@chrisknightley](https://forum.arduino.cc/u/chrisknightley)\
**Post date:** [August 3, 2021, 11:46pm UTC](https://forum.arduino.cc/t/code-does-if-statement-even-when-if-statement-isnt-true/892131/19 "2021-08-03T23:46:12Z")

</div>

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

You said...

> [@jj0932](#):
>
> changed the code to pin 2.

But you connected button to pin 3.

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/6/0/b/60b4c9e03f276b77c08bd44dae5802633a1cd123.jpeg)

Please clarify which one use... 😔

---

<div class="post-metadata">

**Author:** ![jj0932](https://avatars.discourse-cdn.com/v4/letter/j/fbc32d/32.png) [@jj0932](https://forum.arduino.cc/u/jj0932)\
**Post date:** [August 3, 2021, 11:49pm UTC](https://forum.arduino.cc/t/code-does-if-statement-even-when-if-statement-isnt-true/892131/20 "2021-08-03T23:49:06Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![jj0932](https://avatars.discourse-cdn.com/v4/letter/j/fbc32d/32.png) [@jj0932](https://forum.arduino.cc/u/jj0932)\
**Post date:** [August 3, 2021, 11:49pm UTC](https://forum.arduino.cc/t/code-does-if-statement-even-when-if-statement-isnt-true/892131/21 "2021-08-03T23:49:40Z")

</div>

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

[Next page](https://forum.arduino.cc/t/code-does-if-statement-even-when-if-statement-isnt-true/892131.md?page=2)
