Code no longer waits when button is not pressed

So here is the code

const int buttonPin = 2;     
const int ledPin =  13;      
int wait_time = 10; 
bool wait_finished = false;

// variables will change:
int buttonState = 0;         

void setup() {
  Serial.begin(9600);
  Serial.print("wait_finished set to true.");
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH || wait_finished == false) {
    digitalWrite(ledPin, LOW);
    wait_finished == false; 
  } 

  if (buttonState == LOW && wait_finished == true) {
    digitalWrite(ledPin, HIGH);
  } 
  else if (buttonState == LOW) {
    wait();
  }
}

void wait() {
  if (wait_finished == false) {
    delay(wait_time * 1000); 
    wait_finished = true;
    Serial.println("wait_finished set to true.");
  } 
}

My goal is to wait for 10 seconds when the button is not pressed, then turn the LED on. However, after it waits for the first time, the LED would turn immediately on whenever I lift the button without waiting. What did I miss here?

Hi
this is a comparison and not an equality. double "=".

Try wait_finished = false;

2 Likes

Without a schematic I will assume the button input is floating when open. You need to pull it either UP or Down, your choice.

Hello
Do you aware about using the delay() function the sketch will stop?
Have a nice day and enjoy coding in C++.

What he said...

void loop() {
buttonState = digitalRead(buttonPin);

if (buttonState == HIGH || wait_finished == false) {
digitalWrite(ledPin, LOW);
wait_finished == false; < < < ===============
}

if you have connected an external pullup resistor to the pin 2 this might work without a dirty, blocking delay():

const int buttonPin = 2;  // was 2
const int ledPin =  13;
const byte wait_time = 10; // in seconds
//bool wait_finished = false;

// variables will change:
//int buttonState = 0;

void setup() {
  Serial.begin(115200);  // speed up for the year 2021
  Serial.println(F("wait_finished set to true."));  // use F-Makro for fix text
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT); // INPUT or INPUT_PULLUP
}

void loop() {
  static uint32_t buttonReleaseMillis = 0;
  static int previousButtonState = HIGH;
  int buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH && previousButtonState == LOW) {
    Serial.println(F("button was pressed"));
    digitalWrite(ledPin, LOW);
  }
  if (buttonState == LOW && previousButtonState == HIGH) {
    Serial.println(F("button was released"));
    buttonReleaseMillis = millis(); // remember the "time" when the button was released
  }
  if (buttonState == LOW && millis() - buttonReleaseMillis > wait_time * 1000UL && digitalRead(ledPin) == LOW) {
    Serial.println(F("Alarm"));
    digitalWrite(ledPin, HIGH);
  }
  previousButtonState = buttonState;
}