Only return button state once in X amount of time? I want to detect if a button has been pressed, but what I don't want is for it to keep returning the "pressed" state more than once within a second or two.

For example, with this code:

void loop() {
  passButtonState = digitalRead(passButton);

  if (passButtonState == LOW) {
    Serial.println("PASSED!");
  }
}


Would spit out something like this with a single quick press of the button:

PASSED!
PASSED!
PASSED!
PASSED!
PASSED!
PASSED!
PASSED!
PASSED!
PASSED!


But what I want is for a single button press to just return PASSED! once for that single button press.
To avoid this problem you can use a boolean variable as a flag that permit to enter inside the if only once.

bool flag = true;

void loop() {
  passButtonState = digitalRead(passButton);

  if (passButtonState == LOW &amp;&amp; flag) {
    Serial.println("PASSED!");
    flag = false;
  }
}