0

I'm a beginner into Arduino so I have a little problem where I'm trying to do a countdown on LCD using while loop but I'm also trying to get a key input inside that same while loop. Problem is that its hard to get a input in that while because there are delay because of timer. Tried to look around the internet for solution but didn't find one. If anybody got a good solution I would be great full if you post it :) Thanks again!

Here is my code:

while (gameInProgress) {

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Until boom! ");
    lcd.print(timer);
    lcd.setCursor(0, 1);
    lcd.print(passwordInputed);

    char key = keypad.getKey();
    if (key && gameInProgress) {
      passwordInputed += key;
    }

    analogWrite(buzzerPin, 145);
    delay(200);
    analogWrite(buzzerPin, 0);
    delay(800);
    timer--;
    if (timer == 0){
      gameInProgress = false;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Boom happend!");
      lcd.setCursor(0, 1);
      lcd.print("You lost :(");
    }
  }
6
  • 2
    Write non blocking code: docs.arduino.cc/built-in-examples/digital/BlinkWithoutDelay
    – VE7JRO
    Nov 29, 2023 at 20:03
  • 2
    Go learn the Blink Without Delay style of timing. If you want to have an easier time of it, put this code aside for a bit until you really understand what's going on with that style of timing. Then come back and add it here. If you try to just copy that code without understanding how it works then you're going to have a bad time.
    – Delta_G
    Nov 29, 2023 at 20:22
  • 1
    Why did you mark it solved? What is the solution? This isn't a forum, solutions (aka answers) are posted below.
    – Nick Gammon
    Nov 29, 2023 at 22:06
  • 2
    I’m voting to close this question because [SOLVED] is not an answer.
    – VE7JRO
    Nov 30, 2023 at 0:14
  • I removed [SOLVED] from the subject line - it doesn't make any sense.
    – Nick Gammon
    Nov 30, 2023 at 4:19

1 Answer 1

0

In general, when a program needs to perform two or more functions (for example, counting off seconds and reacting to a button press that could happen at any moment) a state machine programming pattern will provide an optimal solution.

Essentially, remove all calls to delay(). Instead, record the value returned from millis() outside the loop. If tracking a, say, 1 second (1000ms) interval, add 1000 to the recorded value. Each time through the loop, check if mills() is greater then the recorded value. When it is, change the state, add 1000 to the recorded value again and do what ever is needed when the state indicating 1 second has elapsed has changed. Now clear the state and wait for the cycle to repeat.

As this loop with out calls to delay() is being executed as fast as possible, a scan for a button press can be added and the necessary reaction to that button press can be very responsive.

Add states as needed for more complex behavior. Remember, in most state machines, outputs normally happen upon state changes. And there are no delays within the loop. Tests to trigger state changes are run as fast as possible each time through the loop.

Not the answer you're looking for? Browse other questions tagged or ask your own question.