How do you plan to solve it?
Identify components and pins
INPUT_PULLUP
so the button reads LOW when pressed.currentPattern
).currentPattern == 0
, run Pattern 1 blinking logic.currentPattern == 1
, run Pattern 2 alternating blinking logic./*Paste your code here*/
const int buttonPin = 2;
const int leds[] = {3, 4, 5, 6};
int currentPattern = 0;
bool lastButtonState = HIGH;
void setup() {
for (int i = 0; i < 4; i++) {
pinMode(leds[i], OUTPUT);
}
pinMode(buttonPin, INPUT_PULLUP); // internal pull-up for button
}
void loop() {
bool buttonState = digitalRead(buttonPin);
if (buttonState == LOW && lastButtonState == HIGH) {
delay(50); // debounce
if (digitalRead(buttonPin) == LOW) {
currentPattern++;
if (currentPattern > 1) currentPattern = 0;
}
}
lastButtonState = buttonState;
if (currentPattern == 0) {
// Pattern 1: all LEDs blink together
for (int i = 0; i < 4; i++) digitalWrite(leds[i], HIGH);
delay(400);
for (int i = 0; i < 4; i++) digitalWrite(leds[i], LOW);
delay(400);
}
else if (currentPattern == 1) {
// Pattern 2: alternate LEDs (1+3 then 2+4)
for (int i = 0; i < 4; i++) digitalWrite(leds[i], LOW);
digitalWrite(leds[0], HIGH); // LED1
digitalWrite(leds[2], HIGH); // LED3
delay(400);
digitalWrite(leds[0], LOW);
digitalWrite(leds[2], LOW);
digitalWrite(leds[1], HIGH); // LED2
digitalWrite(leds[3], HIGH); // LED4
delay(400);
digitalWrite(leds[1], LOW);
digitalWrite(leds[3], LOW);
}
}
Add a video of the output (know more)