All submissions

Solving Approach

How do you plan to solve it?

 
 To implement the sequential LED patterns with a button press, you'll need a simple setup: four LEDs and a single button connected to your microcontroller. The software will be the key, using a variable to keep track of the current pattern. Each time the button is pressed, the code will check for it, handle any button bounce, and then advance the pattern variable to the next number. A conditional statement, like a switch or if/else if, will then use this number to decide which of the four LEDs to turn on or off, creating the desired visual sequence. When the last pattern is reached, the variable will simply reset to the beginning, allowing the cycle to repeat with every button press.

 

 

 

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!