Button reset/start Countdown problem

Hello everyone.

I ask you for help because I do not know where I am failing with my code.

I am programming a countdown from a preset time (59min59sec). with an arduino uno and a tft ILI9341 for show the time.

I start my program and the countdown starts when I press the button, but if I press it again the account stops. If I press the countdown again, it continues in the time it stopped.
In addition to this my countdown goes very fast and I don´t know why happen.

I need that when I press the button the countdown will restart again from the 59min 59sec that I have defined.

My code:

#include <Arduino.h>

#include"Wire.h"

#include <TFT_ILI9341.h>





#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <SPI.h>


int buttonPin = 3;
int ledPin = 2;

#define TFT_DC 9
#define TFT_CS 10

#define ONE_SEC     1000
#define ONE_HOUR    (60*60)

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);

int value = LOW;                    // previous value of the LED
int buttonState;                    // variable to store button state
int lastButtonState;                // variable to store last button state
int blinking;                       // condition for blinking - timer is timing


unsigned long timeSec = 3600;



void setup(){

  tft.begin();
  tft.setRotation(3);
  tft.fillScreen (ILI9341_BLACK);
                
  pinMode(ledPin, OUTPUT);         
  pinMode(buttonPin, INPUT);       
  digitalWrite(buttonPin, HIGH);   //  Wire button so that press shorts pin to ground.
  tft.setCursor(0,0);
  tft.print("CountDown");
   
}

void loop(){

  digitalWrite(ledPin, LOW);            // Initiate LED and Step Pin States

  buttonState = digitalRead(buttonPin); // Check for button press, read the button state and store

// check for a high to low transition if true then found a new button press while clock is not running - start the clock    
   
     if (buttonState == LOW && lastButtonState == HIGH  &&  blinking == false){
    
      
      
      
      static unsigned int msecLst = 0;
      unsigned int msec;

      timeSec--;
      
      
      int min = timeSec / 60;
      int sec = timeSec % 60;
      
      char s [20];
      sprintf (s, "%2d:%02d", min, sec);

      tft.setTextSize (9);
      tft.setTextColor (ILI9341_WHITE, ILI9341_BLACK);
      tft.setCursor(30,170);
      tft.println (s);
      
      blinking = true;                                  // turn on blinking while timing
      delay(10);                                         // short delay to debounce switch
      lastButtonState = buttonState;                    // store buttonState in lastButtonState, to compare next time 
   }

// check for a high to low transition if true then found a new button press while clock is running - stop the clock

   else if (buttonState == LOW && lastButtonState == HIGH && blinking == true){

   blinking = false;                                    // turn off blinking, all done timing
   lastButtonState = buttonState;                       // store buttonState in lastButtonState, to compare next time

   }
   
 else{

      lastButtonState = buttonState;                  // store buttonState in lastButtonState, to compare next time

   }


if (blinking == true){
  
  digitalWrite(ledPin, HIGH);
  static unsigned int msecLst = 0;
  unsigned int msec;

  timeSec--;
      
      
      int min = timeSec / 60;
      int sec = timeSec % 60;
      
      char s [20];
      sprintf (s, "%2d:%02d", min, sec);

      tft.setTextSize (9);
      tft.setTextColor (ILI9341_WHITE, ILI9341_BLACK);
      tft.setCursor(30,170);
        
      tft.println (s);
}

      else{

    digitalWrite(ledPin, LOW); 
}

}

Any idea where I have the problem or how to solve it?

Thank you very much to all.

Why do you press the button again? Is there any other purpose for the button?

..Arnav

Hello Arnav

Every time I press the button I need to start the countdown from the beginning.

So when the timer stops, then press the reset button on your Arduino and press the timer button again to start the timer from the starting.

..Arnav

not exactly
I need to restart the countdown from the beginning without resetting the arduino every time I press the button.

Something like this but countdown from 59min.59sec

ArnavPawarAA:
then press the reset button on your Arduino

Did I really just read someone advocating a total reset to restart some functionality?

Surely you jest?

evadne:
Did I really just read someone advocating a total reset to restart some functionality?

Surely you jest?

The timer is the only system. Pressing the reset button will reset the full system, but what else is in the system rather than the timer? Is there any other program the Arduino needs to run excluding the timer? Resetting the Arduino will reset the timer, no problem.

..Arnav

ArnavPawarAA:
Resetting the Arduino will reset the timer, no problem.

Of course it will with "no problem" but it's a cr@p way to do it, even if there is no "other program the Arduino needs to run excluding the timer".

I PM'd OP that I'll try to help later with a code solution rather then restarting the hardware, but I won't have a gap until after 1300 gmt at the earliest.

Edit: And now it's 1500gmt and I've been working since 0300gmt, so probably not today...