All submissions

LED Blinking Patterns

Solving Approach

How do you plan to solve it?

 Identify components and pins

  • Use 4 LEDs connected to Arduino pins 3, 4, 5, 6.
  • Use 1 pushbutton connected to pin 2 with internal pull-up resistor.
  • Define LED patterns
    • Pattern 1 → all 4 LEDs blink ON and OFF together.
    • Pattern 2 → LEDs 1 & 3 blink first, then LEDs 2 & 4.
  • Handle button input
    • Use INPUT_PULLUP so the button reads LOW when pressed.
    • Add debounce (small delay) to avoid false triggers.
    • Each press of the button changes the current pattern (currentPattern).
  • Control LED outputs
    • If currentPattern == 0, run Pattern 1 blinking logic.
    • If currentPattern == 1, run Pattern 2 alternating blinking logic.
  • Repeat in loop
    • Continuously check the button.
    • Run the selected pattern.
    • Switch patterns each time the button is pressed.
       

Code

/*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);
  }
}

 

 

 

Output

Video

Add a video of the output (know more)

 

 

 

 

Submit Your Solution

Note: Once submitted, your solution goes public, helping others learn from your approach!