// Define the pins for the LEDs
int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
int numLEDs = 10;
void setup() {
// Initialize all LED pins as OUTPUT
for (int i = 0; i < numLEDs; i++) {
pinMode(ledPins[i], OUTPUT);
}
}
void loop() {
// Blink even number of LEDs twice
for (int i = 0; i < numLEDs; i += 2) {
digitalWrite(ledPins[i], HIGH); // Turn the LED on
delay(500); // Wait for 500 milliseconds
digitalWrite(ledPins[i], LOW); // Turn the LED off
delay(500); // Wait for 500 milliseconds
}
delay(1000); // Wait for 1 second between patterns
// Blink odd number of LEDs twice
for (int i = 1; i < numLEDs; i += 2) {
digitalWrite(ledPins[i], HIGH); // Turn the LED on
delay(500); // Wait for 500 milliseconds
digitalWrite(ledPins[i], LOW); // Turn the LED off
delay(500); // Wait for 500 milliseconds
}
delay(1000); // Wait for 1 second between patterns
}
/************************
sure, let's break down the Arduino code step by step:
// Define the pins for the LEDs
int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
int numLEDs = 10;
Define the LED pins: An array ledPins[] is created to store the pin numbers to which the LEDs are connected. In this case, the LEDs are connected to pins 2 through 11.
Number of LEDs: The variable numLEDs is set to 10, representing the total number of LEDs connected.
void setup() {
// Initialize all LED pins as OUTPUT
for (int i = 0; i < numLEDs; i++) {
pinMode(ledPins[i], OUTPUT);
}
}
Setup function: This function runs once when the Arduino is powered on or reset. It sets up the environment for the rest of the code.
Initialize LED pins: A for loop iterates through each element in the ledPins array and sets each pin as an OUTPUT using pinMode(). This allows the Arduino to control the LEDs.
void loop() {
// Blink even number of LEDs twice
for (int i = 0; i < numLEDs; i += 2) {
digitalWrite(ledPins[i], HIGH); // Turn the LED on
delay(500); // Wait for 500 milliseconds
digitalWrite(ledPins[i], LOW); // Turn the LED off
delay(500); // Wait for 500 milliseconds
}
delay(1000); // Wait for 1 second between patterns
Loop function: This function runs repeatedly in a loop after the setup() function.
Blink even-numbered LEDs: Another for loop iterates through the even indices of the ledPins array (0, 2, 4, 6, 8). For each even-numbered LED:
digitalWrite(ledPins[i], HIGH); turns the LED on.
delay(500); waits for 500 milliseconds (0.5 seconds).
digitalWrite(ledPins[i], LOW); turns the LED off.
delay(500); waits for another 500 milliseconds.
Pause: After blinking the even-numbered LEDs, delay(1000); waits for 1 second before moving to the next section.
cpp
Copy code
// Blink odd number of LEDs twice
for (int i = 1; i < numLEDs; i += 2) {
digitalWrite(ledPins[i], HIGH); // Turn the LED on
delay(500); // Wait for 500 milliseconds
digitalWrite(ledPins[i], LOW); // Turn the LED off
delay(500); // Wait for 500 milliseconds
}
delay(1000); // Wait for 1 second between patterns
}
Blink odd-numbered LEDs: A for loop iterates through the odd indices of the ledPins array (1, 3, 5, 7, 9). For each odd-numbered LED:
digitalWrite(ledPins[i], HIGH); turns the LED on.
delay(500); waits for 500 milliseconds.
digitalWrite(ledPins[i], LOW); turns the LED off.
delay(500); waits for another 500 milliseconds.
Pause: After blinking the odd-numbered LEDs, delay(1000); waits for 1 second before the loop function restarts from the beginning.
This cycle of blinking even-numbered LEDs followed by odd-numbered LEDs continues indefinitely.
****************/