All submissions

LED Blinking Patterns

Solving Approach

How do you plan to solve it?

To solve the LED pattern switching task using one push button and four LEDs connected to an Arduino, I would implement a finite state machine that cycles through four distinct patterns each time the button is pressed. The push button would be connected to a GPIO pin configured with an internal pull-up resistor, allowing it to detect a LOW signal when pressed. The four LEDs would be connected to separate GPIO output pins, each with a current-limiting resistor. In the code, I’d define an array or set of functions representing each pattern: Pattern 1A (all LEDs ON), Pattern 1B (all LEDs OFF), Pattern 2A (LEDs 1 & 2 ON, 3 & 4 OFF), and Pattern 2B (LEDs 1 & 2 OFF, 3 & 4 ON). A variable would track the current pattern index, and each valid button press would increment this index, looping back to the first pattern after the last. To ensure reliable input, I’d implement debouncing logic using either a short delay or timestamp comparison. On each press, the system would update the LED states according to the current pattern, creating a responsive and intuitive interface for cycling through visual outputs. This modular approach allows for easy expansion to more patterns or additional input methods.
 

 

 

 

Code

const int ledPins[] = {2, 3, 4, 5}; // LED 1 to LED 4
const int buttonPin = 6;

int patternIndex = 0;
int phaseIndex = 0;
unsigned long lastUpdate = 0;
const unsigned long interval = 1000; // 1 second

bool lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 50;

// Patterns: each pattern has multiple phases, each phase is a 4-element LED state
const bool patterns[][5][4] = {
  { {1, 1, 1, 1}, {0, 0, 0, 0} },                         // Pattern 1
  { {1, 0, 1, 0}, {0, 1, 0, 1} },                         // Pattern 2
  { {1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1} }, // Pattern 3
  { {1, 1, 0, 0}, {0, 0, 1, 1} },                         // Pattern 4
  { {1, 0, 0, 1}, {0, 1, 1, 0} }                          // Pattern 5
};

const int patternLengths[] = {2, 2, 4, 2, 2}; // Number of phases per pattern

void setup() {
  for (int i = 0; i < 4; i++) {
    pinMode(ledPins[i], OUTPUT);
  }
  pinMode(buttonPin, INPUT_PULLUP); // Button active LOW
}

void loop() {
  unsigned long currentTime = millis();

  // Handle LED phase update
  if (currentTime - lastUpdate >= interval) {
    lastUpdate = currentTime;

    // Show current phase
    for (int i = 0; i < 4; i++) {
      digitalWrite(ledPins[i], patterns[patternIndex][phaseIndex][i] ? HIGH : LOW);
    }

    // Move to next phase
    phaseIndex++;
    if (phaseIndex >= patternLengths[patternIndex]) {
      phaseIndex = 0;
    }
  }

  // Handle button press to change pattern
  bool reading = !digitalRead(buttonPin); // Invert for pull-up logic

  if (reading != lastButtonState) {
    lastDebounceTime = currentTime;
  }

  if ((currentTime - lastDebounceTime) > debounceDelay) {
    if (reading == HIGH) {
      patternIndex = (patternIndex + 1) % 5;
      phaseIndex = 0; // Reset phase when switching pattern
    }
  }

  lastButtonState = reading;
}

 

 

 

Output

https://wokwi.com/projects/442629354344939521

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!