/*
* Arduino LED Control Program with Array Implementation
*
* Features:
* - Controls 4 LEDs using arrays for efficient pin management
* - Three push buttons with different LED effects:
* - SW0: Blink all LEDs (1s on/off)
* - SW1: Right-to-left chase (2s per LED)
* - SW2: Left-to-right chase (4s per LED)
* - Uses internal pull-up resistors for button inputs
* - Serial monitor feedback for debugging
*/
// ===== PIN CONFIGURATION USING ARRAYS =====
// LED pin array - change numbers to match your actual wiring
int LED[4] = {14, 15, 16, 17}; // GPIO pins for LEDs 1-4 respectively
// Button pin array with functional descriptions
int SW[3] = {11, 12, 13}; // GPIO pins for:
// SW0: Blink mode (index 0)
// SW1: Right-to-left chase (index 1)
// SW2: Left-to-right chase (index 2)
// ===== SETUP FUNCTION =====
void setup() {
// Initialize serial communication at 115200 baud for debugging
Serial.begin(115200);
// Configure all LED pins as outputs using loop
for(int i = 0; i < 4; i++) {
pinMode(LED[i], OUTPUT); // Set each LED pin as output
}
// Configure all button pins as inputs with internal pull-up resistors
// Note: Buttons should be wired between these pins and GND
for(int i = 0; i < 3; i++) {
pinMode(SW[i], INPUT_PULLUP); // Enable internal pull-up resistor
}
// Initialize all LEDs to OFF state
for(int i = 0; i < 4; i++) {
digitalWrite(LED[i], LOW); // Ensure all LEDs start OFF
}
Serial.println("System initialized. Ready for button input...");
}
// ===== MAIN LOOP FUNCTION =====
void loop() {
delay(10); // Small delay to improve simulation performance
// === Button0 (SW[0]) Handling - Blink Mode ===
// Checks if button is pressed (LOW when pressed due to pull-up)
while(digitalRead(SW[0]) == LOW) {
Serial.println("LED IS BLINKING 1 SECOND DELAY, SWITCH0 IS PRESSED");
// Turn all LEDs ON using array iteration
for(int i = 0; i < 4; i++) {
digitalWrite(LED[i], HIGH); // Set current LED HIGH
}
delay(1000); // Keep all LEDs ON for 1 second
// Turn all LEDs OFF using array iteration
for(int i = 0; i < 4; i++) {
digitalWrite(LED[i], LOW); // Set current LED LOW
}
delay(1000); // Keep all LEDs OFF for 1 second
// Effect continues while button is held down
}
// === Button1 (SW[1]) Handling - Right-to-Left Chase ===
while(digitalRead(SW[1]) == LOW) {
Serial.println("LED IS RUNNING RIGHT TO LEFT FOR 2 SECOND DELAY, SWITCH1 IS PRESSED");
// Sequence: LED[0] → LED[1] → LED[2] → LED[3]
for(int i = 0; i < 4; i++) {
digitalWrite(LED[i], HIGH); // Turn current LED ON
delay(2000); // Keep ON for 2 seconds
digitalWrite(LED[i], LOW); // Turn current LED OFF
}
}
// === Button2 (SW[2]) Handling - Left-to-Right Chase ===
while(digitalRead(SW[2]) == LOW) {
Serial.println("LED IS RUNNING LEFT TO RIGHT FOR 4 SECOND DELAY, SWITCH2 IS PRESSED");
// Sequence: LED[3] → LED[2] → LED[1] → LED[0]
for(int i = 3; i >= 0; i--) {
digitalWrite(LED[i], HIGH); // Turn current LED ON
delay(4000); // Keep ON for 4 seconds
digitalWrite(LED[i], LOW); // Turn current LED OFF
}
}
// === Default State (No Buttons Pressed) ===
// Ensure all LEDs are OFF when no buttons are pressed
for(int i = 0; i < 4; i++) {
digitalWrite(LED[i], LOW); // Turn each LED OFF
}
Serial.println("LED OFF, PRESS SWITCH TO TURN ON THE LED");
delay(10); // Small delay to improve simulation performance
}
/*
* PROGRAM NOTES:
*
* 1. HARDWARE CONNECTIONS:
* - LEDs should be connected with current-limiting resistors (220-1000 ohm)
* - Connect LED anodes to GPIO pins, cathodes to GND through resistors
* - Buttons should connect between GPIO pins and GND
*
* 2. BEHAVIOR:
* - Button0 (SW[0]): Blinks all LEDs simultaneously (1s on/1s off)
* - Button1 (SW[1]): Chasing effect from first to last LED (2s each)
* - Button2 (SW[2]): Chasing effect from last to first LED (4s each)
* - Effects continue as long as buttons are held down
*
* 3. TIMING:
* - All delays are in milliseconds
* - Main loop includes small delays to improve simulation performance
*
* 4. SERIAL MONITOR:
* - Provides real-time feedback about system state
* - Baud rate must be set to 115200 to match the code
*/