Programming Examples

Arduino program to interface LEDs at pins 10,11,12,13 and button at pins 7. The press of button changes the pattern of LED glow. (considering four patterns of LED glow)


Pattern in Arduino

Write a program to interface LEDs at pins 10,11,12,13 and button at pins 7. The press of button changes the pattern of LED glow. (considering four patterns of LED glow) 

🔌 Connections

  • LEDs → Pins 10, 11, 12, 13 (with current-limiting resistors)
  • Push Button → Pin 7 (using internal pull-up)

💡 LED Patterns

  • All LEDs ON
  • All LEDs OFF
  • LEDs glow one by one (running pattern)
  • Alternate LEDs ON (10 & 12, then 11 & 13)
int leds[] = {10, 11, 12, 13};
int buttonPin = 7;
int pattern = 0;
int buttonState = HIGH;
int lastButtonState = HIGH;
void setup() {
  for (int i = 0; i < 4; i++) {
    pinMode(leds[i], OUTPUT);
  }
  pinMode(buttonPin, INPUT_PULLUP);  // Using internal pull-up
}
void loop() {
  buttonState = digitalRead(buttonPin);
  // Detect button press
  if (lastButtonState == HIGH && buttonState == LOW) {
    pattern++;
    if (pattern > 3) {
      pattern = 0;
    }
    delay(300); // Debounce delay
  }
  lastButtonState = buttonState;
  switch (pattern) {
    case 0: // Pattern 1: All LEDs ON
      for (int i = 0; i < 4; i++)
        digitalWrite(leds[i], HIGH);
      break;
    case 1: // Pattern 2: All LEDs OFF
      for (int i = 0; i < 4; i++)
        digitalWrite(leds[i], LOW);
      break;
    case 2: // Pattern 3: Running LED
      for (int i = 0; i < 4; i++) {
        digitalWrite(leds[i], HIGH);
        delay(200);
        digitalWrite(leds[i], LOW);
      }
      break;
    case 3: // Pattern 4: Alternate LEDs
      digitalWrite(10, HIGH);
      digitalWrite(11, LOW);
      digitalWrite(12, HIGH);
      digitalWrite(13, LOW);
      delay(300);
      digitalWrite(10, LOW);
      digitalWrite(11, HIGH);
      digitalWrite(12, LOW);
      digitalWrite(13, HIGH);
      delay(300);
      break;
  }
}

Output

  • Button press increments the pattern variable.
  • switch case selects one of four LED patterns.
  • INPUT_PULLUP avoids external resistor usage.
  • Delay is added for button debouncing.
Latest Current Affairs 2026 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2026 Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz