//define all the arrays and variables for the code
int leds[] = {10, 9, 8, 7, 6, 5, 4, 3, 2};
const byte ledGreen = 20;
const byte button = 15;
const long delayTimeL = 100;
const long delayTimeB = 1;
unsigned long pastTime = 0;
int act = 0;
int direct = 1;
int numOfLeds = sizeof(leds) / sizeof(leds[0]);
int go = 1;
void setup() {
//making all the led's pins OUTPUT
for (int i = 0; i < numOfLeds; i++) {
pinMode(leds[i], OUTPUT);
}
}
void reset() {
//making all the leds LOW
for (int i = 0; i < numOfLeds; i++) {
digitalWrite(leds[i], LOW);
}
}
void ledWave() {
//making the delay between two leds HIGH state and checking if the button is pressed
unsigned long currentTime = millis();
if (go == 1) {
//making HIGH all the leds one by one towards and backwards but the others are in LOW state
if (currentTime - pastTime >= delayTimeL) {
reset();
digitalWrite(leds[act], HIGH);
act += direct;
pastTime = currentTime;
if (act == numOfLeds - 1 || act == 0) {
direct = -direct;
}
}
}
else {
//delay is for better visual effect
reset();
}
}
void loop() {
if (digitalRead(button) == HIGH) {
go++;
}
ledWave();
if (go == 2) {
for (int k = 1; k < 4; k++) {
for (int j : leds) {
digitalWrite(j, HIGH);
}
delay(300);
for (int j : leds) {
digitalWrite(j, LOW);
}
delay(300);
}
go++;
}
if (go == 3) {
digitalWrite(ledGreen, HIGH);
}
}