constexpr unsigned long overallBlinkTime = 10000;
constexpr unsigned long blinkDelay = 500;
constexpr unsigned long lightDelay = 500;
constexpr byte buttonPin = 5;
constexpr int noOfLeds = 3;
byte ledPin[noOfLeds] = {2, 3, 4};
int currentLed = 1;
int lastLed = 0;
byte button;
byte step = 0;
unsigned long lastBlinkTime = 0;
unsigned long startOfThisStepTime;
void setup() {
Serial.begin(115200);
Serial.println("Start");
for (int i = 0; i < noOfLeds; i++) {
pinMode(ledPin[i], OUTPUT);
}
pinMode(buttonPin, INPUT_PULLUP);
randomSeed(analogRead(0));
}
void loop() {
button = buttonState();
if (step == 0) {
if (button == LOW) {
for (int i = 0; i < noOfLeds; i++) {
digitalWrite(ledPin[i], LOW);
}
while (currentLed == lastLed) {
currentLed = random(0, noOfLeds);
}
lastLed = currentLed;
digitalWrite(ledPin[currentLed], HIGH);
step = 1;
startOfThisStepTime = millis();
}
}
if (step == 1) {
if (millis() - startOfThisStepTime >= lightDelay) {
if (button == LOW) {
step = 2;
startOfThisStepTime = millis();
} else {
digitalWrite(ledPin[currentLed], LOW);
step = 0;
}
}
}
if (step == 2) {
if (millis() - lastBlinkTime >= blinkDelay) {
lastBlinkTime = millis();
byte actState = digitalRead(ledPin[currentLed]);
digitalWrite(ledPin[currentLed], !actState);
}
if (millis() - startOfThisStepTime >= overallBlinkTime) {
digitalWrite(ledPin[currentLed], LOW);
step = 0;
}
}
}
byte buttonState() {
static unsigned long lastChangeTime = 0;
static byte lastState = HIGH;
static byte state = HIGH;
byte actState = digitalRead(buttonPin);
if (actState != lastState) {
lastState = actState;
lastChangeTime = millis();
}
if (state != lastState && millis() - lastChangeTime > 30) {
state = lastState;
}
return state;
}