int redPin = 13;
int ylwPin = 9;
int btnPin = 2;
int state = 1; // 1, 2, 3, 4
int old_state;
bool isButtonPressed;
bool btn_val, old_btn_val;
unsigned long currentTime;
unsigned long lastTime;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(ylwPin, OUTPUT);
pinMode(btnPin, INPUT_PULLUP);
}
void loop() {
currentTime = millis();
readButton();
switch(state) {
case 1:
if (isButtonPressed) {
state = 2;
}
if (currentTime - lastTime > 3000) {
state = 3;
}
ledControl(0, 0);
break;
case 2:
if (isButtonPressed) {
state = 3;
}
if (currentTime - lastTime > 1000) {
state = 1;
}
ledControl(1, 0);
break;
case 3:
if (isButtonPressed) {
state = 4;
}
if (currentTime - lastTime > 1000) {
state = 2;
}
ledControl(0, 1);
break;
case 4:
if (isButtonPressed) {
state = 1;
}
if (currentTime - lastTime > 1000) {
state = 3;
}
ledControl(1, 1);
break;
}
if (state != old_state) {
lastTime = currentTime;
}
old_state = state;
}
void ledControl(bool redState, bool ylwState) {
digitalWrite(redPin, redState);
digitalWrite(ylwPin, ylwState);
}
void readButton() {
btn_val = !digitalRead(btnPin);
if (btn_val && !old_btn_val) {
isButtonPressed = true;
}
else {
isButtonPressed = false;
delay(50);
}
old_btn_val = btn_val;
}