Hi,
I have a series of 6 LED's and am trying to use millis() to get it turn on and off in a sequence.
I need the LED's to turn on every 5 seconds and then turn off in the reverse sequence with 5 seconds interval between each LED's. To put it in a sequence:
Turn On Sequence : LED1 - 5 Sec - LED2 - 5 Sec .... LED 6
Now wait for 5 seconds and then
Turn Off Sequence : LED 6 - 5 Sec - LED5 - 5 Sec ... LED 1.
But using the below program, the LED's turn on in a sequence, but after that, all the LED's turn off together and the cycle begins again to turn on. Can someone please help?
Note: LED's are connected as active low.
class Flasher
{
// Class Member Variables
// These are initialized at startup
int ledPin; // the number of the LED pin
unsigned long OnTime; // milliseconds of on-time
unsigned long OffTime; // milliseconds of off-time
// These maintain the current state
int ledState; // ledState used to set the LED
unsigned long previousMillis; // will store last time LED was updated
// Constructor - creates a Flasher
// and initializes the member variables and state
public:
Flasher(int pin, unsigned long on, unsigned long off)
{
ledPin = pin;
pinMode(ledPin, OUTPUT);
OnTime = on;
OffTime = off;
ledState = HIGH;
previousMillis = 0;
}
void Update()
{
// check to see if it's time to change the state of the LED
unsigned long currentMillis = millis();
if((ledState == HIGH) && (currentMillis - previousMillis >= OnTime))
{
ledState = LOW; // Turn it on
previousMillis = currentMillis; // Remember the time
digitalWrite(ledPin, ledState); // Update the actual LED
}
else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime))
{
ledState = HIGH; // turn it off
previousMillis = currentMillis; // Remember the time
digitalWrite(ledPin, ledState); // Update the actual LED
}
}
};
// Pegasus specific LED pin assignments
int LED1 = 2;
int LED2 = 3; //slowly turn on
int LED3 = 7;
int LED4 = 8;
int LED5 = A0;
int LED6 = A1;
Flasher led1(LED1, 1000, 6000);
Flasher led2(LED2, 2000, 5000);
Flasher led3(LED3, 3000, 4000);
Flasher led4(LED4, 4000, 3000);
Flasher led5(LED5, 5000, 2000);
Flasher led6(LED6, 6000, 1000);
void setup()
{
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, HIGH);
digitalWrite(LED6, HIGH);
}
void loop()
{
led1.Update();
led2.Update();
led3.Update();
led4.Update();
led5.Update();
led6.Update();
}