const int buttonPin = 2;
const long startCount = 5000; // start countdown with 5 seconds
unsigned long previousMillisCounter;
bool counting = false;
unsigned long previousMillisUpdate;
const unsigned long intervalUpdate = 200;
void setup()
{
pinMode( buttonPin, INPUT_PULLUP);
Serial.begin( 9600);
Serial.println( "Press the button");
}
void loop()
{
unsigned long currentMillis = millis();
if( !counting)
{
// As soon as the button is pressed, the counter is
// made active and the value of millis() is remembered.
if( digitalRead( buttonPin) == LOW)
{
previousMillisCounter = currentMillis;
counting = true;
}
}
else
{
unsigned long countUpValue = currentMillis - previousMillisCounter; // must be unsigned long !
long countDownValue = startCount - long( countUpValue); // must be long !
// Check if the end time is reached
if( countDownValue <= 0)
{
countDownValue = 0;
// Also print the value of 0 to the output
Serial.println( countDownValue);
Serial.println( "Press the button");
counting = false;
}
else
{
// Update the output.
if( currentMillis - previousMillisUpdate >= intervalUpdate)
{
previousMillisUpdate = currentMillis;
Serial.println( countDownValue);
}
}
}
}