How do you plan to solve it?
// Define the pins for the LEDs and the push button switch
const int led1 = 8;
const int led2 = 9;
const int led3 = 10;
const int led4 = 11;
const int buttonPin = 2;
// Variables to manage the pattern state
int currentPattern = 1;
// Variables to handle button press detection
int buttonState;
int lastButtonState = HIGH;
unsigned long lastDebounceTime = 0;
const long debounceDelay = 10;
// Variables for non-blocking LED timing
unsigned long lastBlinkTime = 0;
int ledState = LOW;
void setup() {
// Set LED pins as outputs
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
// Set button pin as input with an internal pull-up resistor
pinMode(buttonPin, INPUT_PULLUP);
// Initialize Serial Monitor
Serial.begin(9600);
}
void loop() {
// Read the state of the button
int reading = digitalRead(buttonPin);
// Check for button state change
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
// If enough time has passed, update the button state
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
// If the state is LOW, a button press has occurred
if (buttonState == LOW) {
currentPattern++;
if (currentPattern > 5) {
currentPattern = 1;
}
}
}
}
// Update the last button state
lastButtonState = reading;
// Execute the pattern based on the currentPattern
switch (currentPattern) {
case 1:
// Pattern 1: All 4 LEDs blink together
if (millis() - lastBlinkTime > 500) {
lastBlinkTime = millis();
ledState = !ledState;
digitalWrite(led1, ledState);
digitalWrite(led2, ledState);
digitalWrite(led3, ledState);
digitalWrite(led4, ledState);
}
break;
case 2:
// Pattern 2: LED 1 & 3 blink, then 2 & 4
if (millis() - lastBlinkTime > 500) {
lastBlinkTime = millis();
ledState = !ledState;
digitalWrite(led1, ledState);
digitalWrite(led3, ledState);
digitalWrite(led2, !ledState);
digitalWrite(led4, !ledState);
}
break;
case 3:
// Pattern 3: Sequential blink
if (millis() - lastBlinkTime > 250) {
lastBlinkTime = millis();
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
switch ((millis() / 250) % 4) {
case 0:
digitalWrite(led1, HIGH);
break;
case 1:
digitalWrite(led2, HIGH);
break;
case 2:
digitalWrite(led3, HIGH);
break;
case 3:
digitalWrite(led4, HIGH);
break;
}
}
break;
case 4:
// Pattern 4: LED 1 & 2 blink, then 3 & 4
if (millis() - lastBlinkTime > 500) {
lastBlinkTime = millis();
ledState = !ledState;
digitalWrite(led1, ledState);
digitalWrite(led2, ledState);
digitalWrite(led3, !ledState);
digitalWrite(led4, !ledState);
}
break;
case 5:
// Pattern 5: LED 1 & 4 blink, then 2 & 3
if (millis() - lastBlinkTime > 500) {
lastBlinkTime = millis();
ledState = !ledState;
digitalWrite(led1, ledState);
digitalWrite(led4, ledState);
digitalWrite(led2, !ledState);
digitalWrite(led3, !ledState);
}
break;
}
}
Add a video of the output (know more)