const byte ledPins[4] = {4, 5, 6, 7};
const byte btnPins[2] = {3, 2};
byte stateMode = 0;
unsigned long debounceTimes[2];
unsigned long intervalDebounce = 50ul;
unsigned long lastSwitch;
int intervalSwitch = 1000;
byte lastLed = 3;
byte lastButtons[2] = {1, 1};
byte direction = 0;
void setup() {
for (int i = 0; i < sizeof(ledPins); i++) {
pinMode(ledPins[i], OUTPUT);
}
for (int i = 0; i < sizeof(btnPins); i++) {
pinMode(btnPins[i], INPUT_PULLUP);
}
}
void loop() {
unsigned long now = millis();
for (int i = 0; i < sizeof(btnPins); i++) {
if (now - debounceTimes[i] >= intervalDebounce) {
byte btn = digitalRead(btnPins[i]);
if (btn != lastButtons[i]) {
debounceTimes[i] = now;
lastButtons[i] = btn;
if (btn == LOW) {
//button pushed..
if (i == 0) {
stateMode = 1;
direction = 0;
} else if (i == 1 && stateMode == 1) {
//button 2 pressed in mode 1
stateMode = 2;
direction = 1;
}
}
}
}
}
if (stateMode > 0) {
if (now - lastSwitch >= intervalSwitch) {
lastSwitch = now;
digitalWrite(ledPins[lastLed], LOW);
if (direction == 0) {
lastLed++;
if (lastLed > 3) lastLed = 0;
}
else {
if (lastLed > 0) lastLed--; else lastLed = 3;
}
digitalWrite(ledPins[lastLed], HIGH);
}
}
}