#include <Arduino.h>
#include <Bounce2.h> // https://github.com/thomasfredericks/Bounce2/archive/refs/heads/master.zip
Bounce2::Button button = Bounce2::Button();
const int buttonPin = 4;
const int ledPin = LED_BUILTIN;
byte step = 0;
int counter;
int counterLimit = 10;
bool start = false;
unsigned long previousMillis = 0;
void setup()
{
Serial.begin(9600);
Serial.println("Please enter your name:");
button.attach(buttonPin, INPUT);
button.interval(5); // interval im ms
button.setPressedState(LOW);
}
void loop()
{
button.update();
if (button.pressed())
{
if(step == 1)
{
counter = 0;
step = 2;
counterLimit = 20;
start = true;
previousMillis = millis();
Serial.println("Starting countdown 2");
Serial.println(counterLimit - counter);
}
else
{
counter = 0;
step = 1;
counterLimit = 10;
start = true;
previousMillis = millis();
Serial.println("Starting countdown 1");
Serial.println(counterLimit - counter);
}
}
switch (step)
{
case 0:
break;
case 1:
if ((millis() - previousMillis >= 1000) && (start == true))
{
counter++;
previousMillis = millis();
Serial.println(counterLimit - counter);
}
if((counter == counterLimit) && (start == true))
{
start = false;
Serial.println("Done 1!");
}
break;
case 2:
if ((millis() - previousMillis >= 1000) && (start == true))
{
counter++;
previousMillis = millis();
Serial.println(counterLimit - counter);
}
if((counter == counterLimit) && (start == true))
{
start = false;
Serial.println("Done 2!");
}
break;
default:
break;
}
}