Using a button input to reset a countdown timer mid-count

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++;
        }
    }

Sure, why not? Just check the button periodically. Doubtless, you have some timer loop that just sits there and counts time. Inside that loop, you need to check the button.

Or, be told how to "do more than one thing at a time", using millis() instead of delay().

Hello
Post your current sketch, well formated, with comments and in so called code tags <"/"> to see how we can help.
Have nice day and enjoy coding in C++.

btw Rummikub is a nice game with the family

As you are just thinking about this, my guess is you have no code.

Let's start with this and then decide if you want a different display. Essentually this should give you 'nuff info to ponder-upon for a while:

https://create.arduino.cc/projecthub/paulsb/game-turn-duration-timer-4a0f9a

I had a couple iterations. Nothing very fancy. Did a quick translation (from Portuguese) of what I was last working with.

Thanks for the code, next time please post in a new message, though...

Non-blocking timing tutorials:
Blink without delay().
Beginner's guide to millis().
Several things at a time.

Thanks all of you. Exploring the millis() function I think I got it to work. I'll work on completing the project now (tested just the part of the code that I was having trouble with, when I complete the full program I'll update).

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.