EDIT
I've added a few functions in the class to make it more versatile: Stop(), SetPinHighTime(), SetPinLowTime() and SetFlashForever().
class Flasher{
// Class Member Variables. These are initialized at startup.
byte ledPin;
byte ledState;
byte flashForever;
unsigned int flashCounter;
unsigned long OnTime;highTime;
unsigned long OffTime;lowTime;
unsigned long previousMillis;
// Constructor. Create a LED Flasher object.
// Initialize the member variables and LED state.
public:
Flasher(byte pin, unsigned long on, unsigned long off, byte continueFlashing = 0){
ledPin = pin;
pinMode(ledPin, OUTPUT);
OnTimehighTime = on;
OffTimelowTime = off;
ledState = LOW;
flashForever = continueFlashing;
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 >= OnTimehighTime) && ((flashCounter > 0) || (flashForever == 1))){
ledState = LOW;
previousMillis = currentMillis; // Remember the time.
digitalWrite(ledPin, ledState); // Update the actual LED.
if(flashCounter > 0){flashCounter--;} // Update the flash counter.
}
else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTimelowTime) && ((flashCounter > 0) || (flashForever == 1))){
ledState = HIGH;
previousMillis = currentMillis; // Remember the time.
digitalWrite(ledPin, ledState); // Update the actual LED.
if(flashCounter > 0){flashCounter--;} // Update the flash counter.
}
}
void Start(unsigned int flashNumberOfTimes){
flashCounter = flashNumberOfTimes * 2;
flashForever = 0;
previousMillisledState = 0;LOW;
digitalWrite(ledPin, ledState);
}
void Stop(){
Start(0);
}
void SetPinHighTime(unsigned long pinHighTime){
highTime = pinHighTime;
}
void SetPinLowTime(unsigned long pinLowTime){
lowTime = pinLowTime;
}
void SetFlashForever(byte oneOrZero){
flashForever = oneOrZero;
if(oneOrZero == 0){Start(0);}
}
void SetPinToInput(){
pinMode(ledPin, INPUT);
}
};
// Used for testing purposes only.
byte doOnce = 0;
const byte led1PinNumber = 2;
const byte led2PinNumber = 3;
const byte led3PinNumber = 4;
// Instantiate LED Flasher Object(s).
// e.g. Flasher object_name(Pin Number, ON Time, OFF Time, OPTIONAL Flash Forever);
// Three argument constructor.
// The number of times to flash the LED is specified
// when you call the Start() function. The LED will NOT
// start flashing until you call Start().
Flasher led1(13led1PinNumber, 10, 990);
Flasher led2(12led2PinNumber, 100, 400);
// Flash anFour LEDargument foreverconstructor.
// FlasherFlash led1(13,an 100,LED 400,forever. 1);
//This Usedwill forstart testingthe purposesLED onlyflashing immediately.
byte led1doOnce =Flasher 0;
byteled3(led3PinNumber, led2doOnce1000, =1000, 0;1);
void setup(){}
void loop(){
// Call the Update function as fast as possible.
led1.Update();
led2.Update();
led3.Update();
// Test output. Flash 23 LEDs with different on/off times.
// Use the Start() function to flash each2 LEDof the LEDs X number
// of times, then restart each LED flasher object with
// a different number of flashes. Set 1 LED to flash forever,
// then stop it and start a different LED flashing forever,
// altering the flash rates of one LED before restarting it.
// Finally, set the 3 LED pins to INPUT.
// Start the led1 object 4 seconds after Arduino startup.
if(millis() > 4000 && millis() < 5000 && led1doOncedoOnce == 0){
led1.Start(3); // Flash the LED 3 times.
led1doOncedoOnce = 1;
}
// Start the led2 object 5 seconds after Arduino startup.
if(millis() > 5000 && millis() < 6000 && led2doOncedoOnce == 01){
led2.Start(5); // Flash the LED 5 times.
led2doOncedoOnce = 1;0;
}
// Restart the led1 object 10 seconds after Arduino startup.
if(millis() > 10000 && millis() < 11000 && led1doOncedoOnce == 10){
led1.Start(7); // Flash the LED 7 times.
led1doOncedoOnce = 0;1;
}
// Restart the led2 object 15 seconds after Arduino startup.
if(millis() > 15000 && millis() < 16000 && led2doOncedoOnce == 1){
led2.Start(9); // Flash the LED 9 times.
led2doOncedoOnce = 0;
}
// Stop flashing the led3 object.
// Start flashing the led1 object forever.
if(millis() > 30000 && millis() < 31000 && doOnce == 0){
led3.SetFlashForever(0);
led1.SetFlashForever(1);
doOnce = 1;
}
// Stop flashing the led1 object.
// Start flashing the led3 object forever and change
// it's flash rate.
if(millis() > 50000 && millis() < 51000 && doOnce == 1){
led3.SetPinHighTime(250);
led3.SetPinLowTime(500);
led3.SetFlashForever(1);
led1.SetFlashForever(0);
doOnce = 0;
}
// Stop flashing the led3 object. No LEDs should be flashing.
if(millis() > 60000 && millis() < 61000 && doOnce == 0){
led3.Stop();
doOnce = 1;
// My test shield has one High and one Low LED per pin.
// Set the pins to input to turn off the Low LEDs.
led1.SetPinToInput();
led2.SetPinToInput();
led3.SetPinToInput();
}
}