// PinMode
int _PinMode_LED[] = {2, 3, 4, 5, 6};
int _PinMode_Buttons[] = {7, 8};
// Integer used in Loops
int i;
// Length of PinMode Arrays
int Length_PinMode_LED;
int Length_PinMode_Buttons;
// Button states
int ButtonState1 = LOW;
int ButtonState2 = LOW;
bool Pattern1 = false;
bool Pattern2 = false;
// Managing the debounce
int LastButtonState1 = LOW;
int LastButtonState2 = LOW;
unsigned long LastDebounceTime1 = 0;
unsigned long LastDebounceTime2 = 0;
unsigned long DebounceDelay = 50;
int reading1;
int reading2;
void setup() {
Serial.begin(9600);
Length_PinMode_LED = sizeof(_PinMode_LED) / sizeof(_PinMode_LED[0]);
Serial.println(Length_PinMode_LED);
Length_PinMode_Buttons = sizeof(_PinMode_Buttons) / sizeof(_PinMode_Buttons[0]);
Serial.println(Length_PinMode_Buttons);
for (int i = 0; i < Length_PinMode_LED; i++) {
pinMode(_PinMode_LED[i], OUTPUT);
}
for (int i = 0; i < Length_PinMode_Buttons; i++) {
pinMode(_PinMode_Buttons[i], INPUT);
}
}
void loop() {
// checking the state of the buttons
reading1 = digitalRead(_PinMode_Buttons[0]);
reading2 = digitalRead(_PinMode_Buttons[1]);
// Debouncing button 1
if (reading1 != LastButtonState1) {
LastDebounceTime1 = millis();
}
if ((millis() - LastDebounceTime1) > DebounceDelay) {
if (reading1 != ButtonState1) {
ButtonState1 = reading1;
if (ButtonState1 == HIGH) {
Pattern1 = true;
Pattern2 = false;
}
}
}
// Debouncing button 2
if (reading2 != LastButtonState2) {
LastDebounceTime2 = millis();
}
if ((millis() - LastDebounceTime2) > DebounceDelay) {
if (reading2 != ButtonState2) {
ButtonState2 = reading2;
if (ButtonState2 == HIGH) {
Pattern2 = true;
Pattern1 = false;
}
}
}
LastButtonState1 = reading1;
LastButtonState2 = reading2;
if (Pattern1) {
CloseLights();
Example1();
} else if (Pattern2) {
CloseLights();
delay(500);
Example2();
} else {
CloseLights(); }
}
void Example1() {
for (int i = 0; i < Length_PinMode_LED; i++) {
digitalWrite(_PinMode_LED[i], HIGH);
delay(500);
if (i == Length_PinMode_LED - 1) {
for (int x = 0; x <= Length_PinMode_LED; x++) {
digitalWrite(_PinMode_LED[abs(x - Length_PinMode_LED)], LOW);
delay(500);
}
}
}
}
void Example2() {
for (int i = 0; i < Length_PinMode_LED; i++) {
digitalWrite(_PinMode_LED[i], HIGH);
}
delay(500);
for (int i = 0; i < Length_PinMode_LED; i++) {
digitalWrite(_PinMode_LED[i], LOW);
}
}
void CloseLights() {
for (int i = 0; i < Length_PinMode_LED; i++) {
digitalWrite(_PinMode_LED[i], LOW);
}
}