/*
Forum: https://forum.arduino.cc/t/i-dont-really-understand-how-the-millis-work/1229674
Wokwi: https://wokwi.com/projects/391000332028236801
*/
byte leds[] = {2, 3, 4, 5, 6, 7};
const int noOfLeds = sizeof(leds) / sizeof(leds[0]);
const unsigned long ledDelay = 100;
unsigned long lastChange = 0;
int actLed = 0;
int direction = 1;
void setup() {
for (int i = 0; i < noOfLeds; i++) {
pinMode(leds[i], OUTPUT);
}
}
void loop() {
ledWave();
}
void ledWave() {
if (millis() - lastChange > ledDelay) {
lastChange = millis();
resetLeds();
digitalWrite(leds[actLed], HIGH);
actLed += direction;
if (actLed == noOfLeds - 1 || actLed == 0) {
direction = -direction;
}
}
}
void resetLeds() {
for (int i = 0; i < noOfLeds; i++) {
digitalWrite(leds[i], LOW);
}
}