I want to make a countdown timer to use while playing Rummikub with my family. Each player has 60s to his turn, and press the button on the timer to signal he's finished and pass the turn.
Before buying any parts I'm trying to simulate the project.
The timer should be set at 60s and start to countdown when the button is pressed. If the count reach 0, the timer should be reset (at 60s waiting for the button to be pressed). If the button is pressed with the countdown running, the time should reset to 60s and keep running.
This last part is where I am having problems. Once I start the loop to do the countdown, I only know how to read the button input after the time reaches 0s.
My "solution" for now is to program the timer to start to countdown by default and use the button as a power hard reset (power reset) outside the logic part.
I want to know if it is possible and/or viable to have the button as a Countdown starter and dynamic time "re-seter".
EDIT: Code;
// Countdown Timer
#define button 13
int counter = 60; // Time
bool berg = 0; // Start/stop state
// matrix
byte binSinal [10][4] {
{0,0,0,0}, //Dig 0
{0,0,0,1}, //Dig 1
{0,0,1,0}, //Dig 2
{0,0,1,1}, //Dig 3
{0,1,0,0}, //Dig 4
{0,1,0,1}, //Dig 5
{0,1,1,0}, //Dig 6
{0,1,1,1}, //Dig 7
{1,0,0,0}, //Dig 8
{1,0,0,1} //Dig 9
};
void setup()
{
// IC CD4511
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
// Seting button as input
pinMode(13, INPUT);
// LOW
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
}
void loop()
{
//
if (berg == 0){ // Start or stay
if (digitalRead(13) == 1){ //Checking for button reset
counter = 60;
}
// Write counter number
escreverNum(counter);
// wait
delay(500);
// I'm going back to the staaart (8)
if(counter == 0){
berg = 1;
counter = 61;
}
counter--;
} else{
//Write 60
escreverNum(60);
}
}
//Sending binary signals to CD4511
void escreverNum(byte numero) {
byte dezena = numero/10; // scuffed maths
byte unidade = numero - 10*dezena; //scuffed maths for units
byte pino = 2; //Pin select
//iteration
for (byte i = 0; i < 4; i++) { // Binary signal Loop
digitalWrite(pino, binSinal[unidade][i]); // Unit digit
digitalWrite((pino + 4), binSinal[dezena][i]); // tens digit
pino++;
}
}