/**************************************************
Explanation of the Code:
Pin Setup: The 10 LEDs are connected to pins 2 through 11.
frequency Variable: Set to 2 Hz by default, meaning the
LEDs will chase 2 times per second.
interval Calculation: The interval for switching LEDs
is based on the frequency and determines how long each LED stays on.
LED Chasing Logic:
The millis() function ensures non-blocking timing for
the LED chase.
Each LED is turned on for the calculated interval and
then turns off as the next LED lights up.
The currentLed wraps around to 0 after reaching the last LED,
making the chaser continuous.
Adjusting Speed:
To change the speed of the LED chase, you can modify the frequency variable. For example:
frequency = 5; would make the chase 5 times per second (faster).
frequency = 1; would make the chase 1 time per second (slower).
https://chatgpt.com/share/c0099c09-14ba-4cf1-aa96-49c3a8fd812d
by arvind patil
**************************************************/
const int numLeds = 10; // Number of LEDs
int ledPins[numLeds] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; // Pins connected to the LEDs
int currentLed = 0; // To track the current LED
unsigned long previousMillis = 0; // To store the last time an LED was updated
int frequency = 2; // Frequency in Hz (chasing frequency)
unsigned long interval = 1000 / (2 * frequency); // Calculate interval (half-period for switching)
void setup() {
// Set all LED pins as OUTPUT
for (int i = 0; i < numLeds; i++) {
pinMode(ledPins[i], OUTPUT);
}
}
void loop() {
unsigned long currentMillis = millis();
// Check if it's time to move to the next LED
if (currentMillis - previousMillis >= interval) {
// Save the last time we updated the LEDs
previousMillis = currentMillis;
// Turn off all LEDs
for (int i = 0; i < numLeds; i++) {
digitalWrite(ledPins[i], LOW);
}
// Turn on the current LED
digitalWrite(ledPins[currentLed], HIGH);
// Move to the next LED, wrap around if necessary
currentLed = (currentLed + 1) % numLeds;
}
}