/*
* Arduino LED Control Program with Push Buttons
* Complete Documentation Version
*
* Features:
* - Controls 4 LEDs with 3 push buttons
* - SW1: Blinks all LEDs (1s on/off)
* - SW2: Right-to-left chase (2s per LED)
* - SW3: Left-to-right chase (4s per LED)
*/
// ===== PIN CONFIGURATION =====
// LED pin assignments (change numbers to match your wiring)
const int LED1 = 14; // GPIO14 for LED1
const int LED2 = 15; // GPIO15 for LED2
const int LED3 = 16; // GPIO16 for LED3
const int LED4 = 17; // GPIO17 for LED4
// Button pin assignments with functional descriptions
const int SW1 = 11; // GPIO11 for Button1 (Blink mode)
const int SW2 = 12; // GPIO12 for Button2 (Right-to-left chase)
const int SW3 = 13; // GPIO13 for Button3 (Left-to-right chase)
// ===== SETUP FUNCTION =====
void setup() {
// Initialize serial communication at 115200 baud
Serial.begin(115200);
// Configure LED pins as outputs
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
// Configure button pins as inputs with internal pull-up resistors
// Note: Buttons should be wired between these pins and GND
pinMode(SW1, INPUT_PULLUP);
pinMode(SW2, INPUT_PULLUP);
pinMode(SW3, INPUT_PULLUP);
// Initialize all LEDs to OFF state
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
Serial.println("System initialized. Ready for button input...");
}
// ===== MAIN LOOP FUNCTION =====
void loop() {
delay(10); // Small delay to improve simulation performance
// === Button1 (SW1) Handling ===
// Checks if button is pressed (LOW when pressed due to pull-up)
while(digitalRead(SW1) == LOW) {
Serial.println("LED IS BLINKING 1 SECOND DELAY, SWITCH1 IS PRESSED");
// Turn all LEDs ON
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
delay(1000); // Keep ON for 1 second
// Turn all LEDs OFF
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
delay(1000); // Keep OFF for 1 second
// Note: Effect continues while button is held
}
// === Button2 (SW2) Handling ===
// Right-to-left chasing effect
while(digitalRead(SW2) == LOW) {
Serial.println("LED IS RUNNING RIGHT TO LEFT 2 SECOND DELAY, SWITCH2 IS PRESSED");
// Sequence: LED1 → LED2 → LED3 → LED4
digitalWrite(LED1, HIGH);
delay(2000); // LED1 ON for 2 seconds
digitalWrite(LED1, LOW);
digitalWrite(LED2, HIGH);
delay(2000); // LED2 ON for 2 seconds
digitalWrite(LED2, LOW);
digitalWrite(LED3, HIGH);
delay(2000); // LED3 ON for 2 seconds
digitalWrite(LED3, LOW);
digitalWrite(LED4, HIGH);
delay(2000); // LED4 ON for 2 seconds
digitalWrite(LED4, LOW);
}
// === Button3 (SW3) Handling ===
// Left-to-right chasing effect
while(digitalRead(SW3) == LOW) {
Serial.println("LED IS RUNNING LEFT TO RIGHT 4 SECOND DELAY, SWITCH3 IS PRESSED");
// Sequence: LED4 → LED3 → LED2 → LED1
digitalWrite(LED4, HIGH);
delay(4000); // LED4 ON for 4 seconds
digitalWrite(LED4, LOW);
digitalWrite(LED3, HIGH);
delay(4000); // LED3 ON for 4 seconds
digitalWrite(LED3, LOW);
digitalWrite(LED2, HIGH);
delay(4000); // LED2 ON for 4 seconds
digitalWrite(LED2, LOW);
digitalWrite(LED1, HIGH);
delay(4000); // LED1 ON for 4 seconds
digitalWrite(LED1, LOW);
}
// === Default State (No Buttons Pressed) ===
// Ensure all LEDs are OFF when no buttons are pressed
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
Serial.println("LED OFF, PRESS SWITCH TO TURN ON THE LED");
delay(10); // Small delay to improve simulation performance
}
/*
* Program Notes:
* 1. Button Wiring:
* - Buttons should connect between GPIO pins and GND
* - Internal pull-up resistors make pins HIGH when not pressed
* - Pressing button pulls pin LOW
*
* 2. LED Wiring:
* - Use current-limiting resistors (220-1000 ohm)
* - Connect LED anodes to GPIO pins
* - Connect cathodes to GND through resistors
*
* 3. Timing:
* - All delays are in milliseconds
* - Effects continue as long as buttons are held
*/