If statement conditions always true

Hi All,

I am new to this whole Arduino thing and have an if statement that I can't seem to find an answer for. When I run the code below what I am expecting is:

1.) The Serial display to print the number of times that a button has been pressed on every button press and that works.

2.) If LoopCheck is equal to my buttonPushCounter it should print the LoopCheck variable. I am not updating the loop check variable to troubleshoot, but in my mind when I press the button once the two variables will not be equal and nothing should print. This is the problem, LoopCheck is printed continuously in my serial display, it is as though the if statement is being ignored.

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 3;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
int LoopCheck = 0;           // See if loop runs



void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
   
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      //Serial.println(buttonPushCounter);
      Serial.print("BPC ");
      Serial.println(buttonPushCounter);
      }
    }

  // save the current state as the last state, for next time through the loop
  
  delay(50); // Delay a little bit to avoid bouncing
  lastButtonState = buttonState;

  if (buttonPushCounter == LoopCheck); 
  {
    Serial.println(LoopCheck);
  }

Thanks

BTW most of the code was written by Tom Igoe.

  if (buttonPushCounter == LoopCheck);

Oops

(I bet Tom didn’t write that bit!)

But what is the oops? Am I not just checking an integer against and integer?

Am I not just checking an integer against and integer?

Yes, but what code runs if they are equal and what code always runs whether they are equal or not ?

Hint: Your sketch has three "if"s.

Play “spot-the-difference”

If it is true it will blink is supposed to run a for loop, but I have taken that out for now and put the Serial.print statement in it place. If they are not equal the it should run back to the top of the program.

See reply #1 and #4

TheMemberFormerlyKnownAsAWOL:
Hint: Your sketch has three "if"s.

Play "spot-the-difference"

Well I am an idiot...

; = End of statement and I will never EVER forget that again. THANKS!

scorin:
Well I am an idiot…

; = End of statement and I will never EVER forget that again. THANKS!

Don’t count on it - BT;DT… :smiley:

scorin:
Well I am an idiot...

; = End of statement and I will never EVER forget that again. THANKS!

No, just human! There will be more. We all have them. After while you will learn to spot them. Hint: next time print the source code and read it. May be easier to spot.

Paul