Countdown Timer with Button and Buzzer

Hello,

So, I’m quite new to Arduino, I’m taking a class to create interactive interfaces. The final project is to make a game, which I started, and had code that was generally working except for one problem. I then tried to fix the problem, destroyed the code so that it doesn’t work AT ALL, and forgot to save the (mostly working) original.

I feel like this shouldn’t be that difficult - I want a countdown timer of 10-15 seconds (I have it at 5 just for testing) that beeps at the end, and when you press the button restarts/adds more time.

The original issue was that I had coded in the button to add time, but the debounce would make it add anywhere from 3-100 times what was in the code. I was trying to work in a debounce and the button stopped working.

This is the code, from what I’ve tried to piece back together.

If I block the button-related code in the loop the countdown will work okay. But then, how to restart it?

const int buzzer = 9; //Buzzer Pin
const int button = 7; // Push-button pin
// include the library code:
#include <LiquidCrystal.h>

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

unsigned long prevMillis;
int count = 5;
int buttonState = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("Starting Countdown");
  prevMillis = millis();  // get the time at the start
  Serial.println(count);

  pinMode(buzzer, OUTPUT); //Defines pinBuz as an Output
  pinMode(button, INPUT); // Defines pinSwi as an input
  lcd.begin(16, 2);
}

void loop() {

buttonState = (digitalRead, button);
Serial.println(buttonState);
  if (buttonState == HIGH) {
    tone(buzzer, 294);
    lcd.clear ();
    lcd.print ("Go!");
    count = count + 5;

    unsigned long curMillis = millis();
    //  if one second has passed
    if (curMillis - prevMillis >= 1000) {
      count -= 1;
      Serial.println(count);
      prevMillis += 1000;
      lcd.clear ();
      lcd.print(count);
      // normally prevMillis = curMillis
      // but by adding 1000 insead we prevent
      // accumulation of any mis-timing.
    }
    if (count == 0) {
      tone(buzzer, 294);
      //delay(1000);
      lcd.clear();
      lcd.print("Turn completed");
      //while (1); // lock up the board until reset
    }

    if (count < -1) {
      lcd.clear ();
      noTone(buzzer);
      lcd.print ("Next Player");
    }
  }

  else if (buttonState == LOW) {
    noTone(buzzer);
    unsigned long curMillis = millis();
    //  if one second has passed
    //if (curMillis - prevMillis >= 1000) {
    //count -= 1;
    //Serial.println(count);
    //prevMillis += 1000;
    //lcd.clear ();
    //lcd.print(count);

  }
}

Thanks for any help you can give!

I’ve attached schematics

In your sketches, suggest you scan switches every 50ms or so.
This can filter out bounces.

Handle switch readings with change of state/position detection.
If a switch has changed position, from the last time it was read, do some action based on the level the switch is at. Then note the new position of the switch.
If the switch changes to the opposite state/position, note the new state then do some action etc.

There is an example that comes in the IDE you can refer to.

Also, see the SwitchManager example:

Thanks! I'm always struggling with the logic of things (not the way my brain works) but that makes sense.

Now let's see if I can get it working....

narmstrong:
Hello,

So, I’m quite new to Arduino, I’m taking a class to create interactive interfaces. The final project is to make a game, which I started, and had code that was generally working except for one problem. I then tried to fix the problem, destroyed the code so that it doesn’t work AT ALL, and forgot to save the (mostly working) original.

What did you do to “destroy” the code?.. :o :o :o
OPs circuit.
Screenshot 2018-05-22 18.45.41.png
Tom… :slight_smile: