/*
  More info:
  https://home.et.utwente.nl/slootenvanf/2020/06/08/countdown-timers-tasks-in-parallel/ (bottom)
  https://wokwi.com/projects/400289660957329409

  When the first button is pressed the LEDs light up red for two seconds and
  turn off for one second,
  immediately after they start to blink, continuously showing
  blue, yellow and red, each color for one second.
  While this blinking is happening the buttons should be checked as
  when button one is pressed while the blinking is happening the LEDs will stop
  blinking and randomly choose a color out of the 3 and a beeper will start.
*/

// Include the Bounce2 library:
// https://github.com/thomasfredericks/Bounce2
#include <Bounce2.h>

// pins for the LEDs:
const int redPin = 3;
const int greenPin = 5;
const int bluePin = 6;

// WE WILL attach() THE BUTTON TO THE FOLLOWING PIN IN setup()
#define BUTTON1_PIN 8 

// INSTANTIATE A Button OBJECT
Bounce2::Button button1 = Bounce2::Button();

// variables for time management:
unsigned long previousMillis = 0; // will store last time timer was updated

const unsigned long interval = 1000; // 1 second

int state = 0; // blue (0), yellow (1) and red (2), finished (3)

void setup() {
  // initialize serial:
  Serial.begin(9600);
  Serial.println("Start");
  
  // button setup:
  button1.attach( BUTTON1_PIN, INPUT_PULLUP ); // USE INTERNAL PULL-UP
  button1.interval(5); // DEBOUNCE INTERVAL IN MILLISECONDS
  button1.setPressedState(LOW); // INDICATE THAT THE LOW STATE CORRESPONDS TO PHYSICALLY PRESSING THE BUTTON
  
  // make the pins outputs:
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);

  // https://www.arduino.cc/reference/en/language/functions/random-numbers/random/
  randomSeed(analogRead(0));

  reset();
  Serial.println("Loop");
} // end setup

void loop() {
  // check for button updates
  button1.update();

  if ( button1.pressed() && state<3 ) {
    Serial.print("Press state="); Serial.println(state);
    state=3; // finish
    // random color:
    change_color(random(3));
    // beep
  }

  // blinking task (every second):
  unsigned long currentMillis = millis();

  // task 1 - blink with 3 colors:
  if ((currentMillis - previousMillis > interval) && state<3 ) { // interval passed?
    previousMillis = currentMillis; // save the last time
    //state: blue (0), yellow (1) and red (2), finished (3)
    change_color(state);
    // next state:
    state++;
    if (state==3) state=0;
  }
} // end loop


void change_color(int color_number) {
  if (color_number==0) { // blue
    analogWrite(redPin, 0);
    analogWrite(greenPin, 0);
    analogWrite(bluePin, 255);
  } else if (color_number==1) { // yellow
    analogWrite(redPin, 255);
    analogWrite(greenPin, 255);
    analogWrite(bluePin, 0);
  } else if (color_number==2) { // red
    analogWrite(redPin, 255);
    analogWrite(greenPin, 0);
    analogWrite(bluePin, 0);
  }
}

void reset() {
  Serial.println("reset()");
  
  while ( !button1.pressed() ) button1.update(); // wait until button is pressed...

  // red for 2 seconds:
  analogWrite(redPin, 255);
  analogWrite(greenPin, 0);
  analogWrite(bluePin, 0);
  delay(2000);
  // off for 1 second:
  analogWrite(redPin, 0);
  analogWrite(greenPin, 0);
  analogWrite(bluePin, 0);
  delay(1000);

  state = 0;
}
$abcdeabcde151015202530354045505560fghijfghij