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