How do you plan to solve it?
How It Works:
setup; This part runs once when the Arduino is turned on. It tells the Arduino which pins are used for the LEDs (as outputs) and the push button (as an input). The INPUT_PULLUP
setting is used for the button, which simplifies the wiring by using an internal resistor, meaning the button will be "HIGH" (on) by default, and "LOW" (off) when pressed.
loop; This part of the code runs continuously. It constantly checks if the button has been pressed.
press button; When you press the button, the code detects a "LOW" signal. It then waits a tiny bit (delay(50)
) to make sure it's a real press and not just a quick flicker (this is called debouncing). If the button is still pressed, it moves to the next pattern by incrementing the currentPattern
variable. The % numPatterns
part makes sure the number loops back to 0 after reaching the last pattern.
pattern; The switch
statement in the loop()
function checks the value of currentPattern
and calls the appropriate function to run the LED pattern. Each pattern function (pattern1()
, pattern2()
, etc.) contains the specific instructions for turning the LEDs on and off with delays to create the desired blinking effect.
/*Paste your code here*/
const int buttonPin = 2; // Push button connected to digital pin 2
const int ledPins[] = {9, 10, 11, 12}; // LEDs connected to digital pins 9, 10, 11, 12
const int numPatterns = 5;
const int numLeds = 4;
int currentPattern = 0;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
for (int i = 0; i < numLeds; i++) {
pinMode(ledPins[i], OUTPUT);
}
}
void loop() {
if (digitalRead(buttonPin) == LOW) { // Check if the button is pressed
delay(50); // Debounce delay
if (digitalRead(buttonPin) == LOW) {
currentPattern = (currentPattern + 1) % numPatterns; // Cycle to the next pattern
}
}
// Execute the current LED pattern
switch (currentPattern) {
case 0:
pattern1();
break;
case 1:
pattern2();
break;
case 2:
pattern3();
break;
case 3:
pattern4();
break;
case 4:
pattern5();
break;
}
}
void allLedsOff() {
for (int i = 0; i < numLeds; i++) {
digitalWrite(ledPins[i], LOW);
}
}
// Pattern 1: All 4 LEDs blinking simultaneously
void pattern1() {
digitalWrite(ledPins[0], HIGH);
digitalWrite(ledPins[1], HIGH);
digitalWrite(ledPins[2], HIGH);
digitalWrite(ledPins[3], HIGH);
delay(500);
digitalWrite(ledPins[0], LOW);
digitalWrite(ledPins[1], LOW);
digitalWrite(ledPins[2], LOW);
digitalWrite(ledPins[3], LOW);
delay(500);
}
// Pattern 2: LED 1 & 3 blink together, LED 2 & 4 blink together (opposite)
void pattern2() {
digitalWrite(ledPins[0], HIGH);
digitalWrite(ledPins[2], HIGH);
digitalWrite(ledPins[1], LOW);
digitalWrite(ledPins[3], LOW);
delay(500);
digitalWrite(ledPins[0], LOW);
digitalWrite(ledPins[2], LOW);
digitalWrite(ledPins[1], HIGH);
digitalWrite(ledPins[3], HIGH);
delay(500);
}
// Pattern 3: LEDs blink one by one in sequence
void pattern3() {
for (int i = 0; i < numLeds; i++) {
digitalWrite(ledPins[i], HIGH);
delay(200);
digitalWrite(ledPins[i], LOW);
}
}
// Pattern 4: LED 1 & 2 blink together, LED 3 & 4 blink together (opposite)
void pattern4() {
digitalWrite(ledPins[0], HIGH);
digitalWrite(ledPins[1], HIGH);
digitalWrite(ledPins[2], LOW);
digitalWrite(ledPins[3], LOW);
delay(500);
digitalWrite(ledPins[0], LOW);
digitalWrite(ledPins[1], LOW);
digitalWrite(ledPins[2], HIGH);
digitalWrite(ledPins[3], HIGH);
delay(500);
}
// Pattern 5: LED 1 & 4 blink together, LED 2 & 3 blink together (opposite)
void pattern5() {
digitalWrite(ledPins[0], HIGH);
digitalWrite(ledPins[3], HIGH);
digitalWrite(ledPins[1], LOW);
digitalWrite(ledPins[2], LOW);
delay(500);
digitalWrite(ledPins[0], LOW);
digitalWrite(ledPins[3], LOW);
digitalWrite(ledPins[1], HIGH);
digitalWrite(ledPins[2], HIGH);
delay(500);
}
Add a video of the output (know more)