Skip to main content
I've added a few functions in the class to make it more versatile: Stop(), SetPinHighTime(), SetPinLowTime() and SetFlashForever().
Source Link
VE7JRO
  • 2.5k
  • 19
  • 28
  • 31

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();
  }

}
class Flasher{

  // Class Member Variables. These are initialized at startup.
  byte ledPin;
  byte ledState;
  byte flashForever;
  unsigned int flashCounter;
  unsigned long OnTime;
  unsigned long OffTime;
  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);
    OnTime = on;
    OffTime = 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 >= OnTime) && ((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 >= OffTime) && ((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;
    previousMillis = 0;
  }

};

// Instantiate LED Flasher Object(s).
// e.g. Flasher object_name(Pin Number, ON Time, OFF Time, OPTIONAL Flash Forever);

// The number of times to flash the LED is specified
// when you call the Start() function.
Flasher led1(13, 10, 990);
Flasher led2(12, 100, 400);

// Flash an LED forever.
// Flasher led1(13, 100, 400, 1);

// Used for testing purposes only.
byte led1doOnce = 0;
byte led2doOnce = 0;

void setup(){}

void loop(){

  // Call the Update function as fast as possible.
  led1.Update();
  led2.Update();

  // Test output. Flash 2 LEDs with different on/off times.
  // Use the Start() function to flash each LED X number
  // of times, then restart each LED flasher object with
  // a different number of flashes.

  // Start the led1 object 4 seconds after Arduino startup.
  if(millis() > 4000 && millis() < 5000 && led1doOnce == 0){
    led1.Start(3);  // Flash the LED 3 times.
    led1doOnce = 1;
  }

  // Start the led2 object 5 seconds after Arduino startup.
  if(millis() > 5000 && millis() < 6000 && led2doOnce == 0){
    led2.Start(5);  // Flash the LED 5 times.
    led2doOnce = 1;
  }

  // Restart the led1 object 10 seconds after Arduino startup.
  if(millis() > 10000 && millis() < 11000 && led1doOnce == 1){
    led1.Start(7);  // Flash the LED 7 times.
    led1doOnce = 0;
  }

  // Restart the led2 object 15 seconds after Arduino startup.
  if(millis() > 15000 && millis() < 16000 && led2doOnce == 1){
    led2.Start(9);  // Flash the LED 9 times.
    led2doOnce = 0;
  }

}

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 highTime;
  unsigned long 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);
    highTime = on;
    lowTime = 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 >= highTime) && ((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 >= lowTime) && ((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;
    ledState = 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(led1PinNumber, 10, 990);
Flasher led2(led2PinNumber, 100, 400);

// Four argument constructor.
// Flash an LED forever. This will start the LED flashing immediately.
Flasher led3(led3PinNumber, 1000, 1000, 1);

void setup(){}

void loop(){

  // Call the Update function as fast as possible.
  led1.Update();
  led2.Update();
  led3.Update();

  // Test output. Flash 3 LEDs with different on/off times.
  // Use the Start() function to flash 2 of 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 && doOnce == 0){
    led1.Start(3);  // Flash the LED 3 times.
    doOnce = 1;
  }

  // Start the led2 object 5 seconds after Arduino startup.
  if(millis() > 5000 && millis() < 6000 && doOnce == 1){
    led2.Start(5);  // Flash the LED 5 times.
    doOnce = 0;
  }

  // Restart the led1 object 10 seconds after Arduino startup.
  if(millis() > 10000 && millis() < 11000 && doOnce == 0){
    led1.Start(7);  // Flash the LED 7 times.
    doOnce = 1;
  }

  // Restart the led2 object 15 seconds after Arduino startup.
  if(millis() > 15000 && millis() < 16000 && doOnce == 1){
    led2.Start(9);  // Flash the LED 9 times.
    doOnce = 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();
  }

}
Corrected spelling mistake.
Source Link
VE7JRO
  • 2.5k
  • 19
  • 28
  • 31
class Flasher{

  // Class Member Variables. These are initialized at startup.
  byte ledPin;
  byte ledState;
  byte flashForever;
  unsigned int flashCounter;
  unsigned long OnTime;
  unsigned long OffTime;
  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);
    OnTime = on;
    OffTime = 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 >= OnTime) && ((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 >= OffTime) && ((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 flashNunmberOfTimesflashNumberOfTimes){
    flashCounter = flashNunmberOfTimesflashNumberOfTimes * 2;
    flashForever = 0;
    previousMillis = 0;
  }

};

// Instantiate LED Flasher Object(s).
// e.g. Flasher object_name(Pin Number, ON Time, OFF Time, OPTIONAL Flash Forever);

// The number of times to flash the LED is specified
// when you call the Start() function.
Flasher led1(13, 10, 990);
Flasher led2(12, 100, 400);

// Flash an LED forever.
// Flasher led1(13, 100, 400, 1);

// Used for testing purposes only.
byte led1doOnce = 0;
byte led2doOnce = 0;

void setup(){}

void loop(){

  // Call the Update function as fast as possible.
  led1.Update();
  led2.Update();

  // Test output. Flash 2 LEDs with different on/off times.
  // Use the Start() function to flash each LED X number
  // of times, then restart each LED flasher object with
  // a different number of flashes.

  // Start the led1 object 4 seconds after Arduino startup.
  if(millis() > 4000 && millis() < 5000 && led1doOnce == 0){
    led1.Start(3);  // Flash the LED 3 times.
    led1doOnce = 1;
  }

  // Start the led2 object 5 seconds after Arduino startup.
  if(millis() > 5000 && millis() < 6000 && led2doOnce == 0){
    led2.Start(5);  // Flash the LED 5 times.
    led2doOnce = 1;
  }

  // Restart the led1 object 10 seconds after Arduino startup.
  if(millis() > 10000 && millis() < 11000 && led1doOnce == 1){
    led1.Start(7);  // Flash the LED 7 times.
    led1doOnce = 0;
  }

  // Restart the led2 object 15 seconds after Arduino startup.
  if(millis() > 15000 && millis() < 16000 && led2doOnce == 1){
    led2.Start(9);  // Flash the LED 9 times.
    led2doOnce = 0;
  }

}
class Flasher{

  // Class Member Variables. These are initialized at startup.
  byte ledPin;
  byte ledState;
  byte flashForever;
  unsigned int flashCounter;
  unsigned long OnTime;
  unsigned long OffTime;
  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);
    OnTime = on;
    OffTime = 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 >= OnTime) && ((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 >= OffTime) && ((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 flashNunmberOfTimes){
    flashCounter = flashNunmberOfTimes * 2;
    flashForever = 0;
    previousMillis = 0;
  }

};

// Instantiate LED Flasher Object(s).
// e.g. Flasher object_name(Pin Number, ON Time, OFF Time, OPTIONAL Flash Forever);

// The number of times to flash the LED is specified
// when you call the Start() function.
Flasher led1(13, 10, 990);
Flasher led2(12, 100, 400);

// Flash an LED forever.
// Flasher led1(13, 100, 400, 1);

// Used for testing purposes only.
byte led1doOnce = 0;
byte led2doOnce = 0;

void setup(){}

void loop(){

  // Call the Update function as fast as possible.
  led1.Update();
  led2.Update();

  // Test output. Flash 2 LEDs with different on/off times.
  // Use the Start() function to flash each LED X number
  // of times, then restart each LED flasher object with
  // a different number of flashes.

  // Start the led1 object 4 seconds after Arduino startup.
  if(millis() > 4000 && millis() < 5000 && led1doOnce == 0){
    led1.Start(3);  // Flash the LED 3 times.
    led1doOnce = 1;
  }

  // Start the led2 object 5 seconds after Arduino startup.
  if(millis() > 5000 && millis() < 6000 && led2doOnce == 0){
    led2.Start(5);  // Flash the LED 5 times.
    led2doOnce = 1;
  }

  // Restart the led1 object 10 seconds after Arduino startup.
  if(millis() > 10000 && millis() < 11000 && led1doOnce == 1){
    led1.Start(7);  // Flash the LED 7 times.
    led1doOnce = 0;
  }

  // Restart the led2 object 15 seconds after Arduino startup.
  if(millis() > 15000 && millis() < 16000 && led2doOnce == 1){
    led2.Start(9);  // Flash the LED 9 times.
    led2doOnce = 0;
  }

}
class Flasher{

  // Class Member Variables. These are initialized at startup.
  byte ledPin;
  byte ledState;
  byte flashForever;
  unsigned int flashCounter;
  unsigned long OnTime;
  unsigned long OffTime;
  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);
    OnTime = on;
    OffTime = 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 >= OnTime) && ((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 >= OffTime) && ((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;
    previousMillis = 0;
  }

};

// Instantiate LED Flasher Object(s).
// e.g. Flasher object_name(Pin Number, ON Time, OFF Time, OPTIONAL Flash Forever);

// The number of times to flash the LED is specified
// when you call the Start() function.
Flasher led1(13, 10, 990);
Flasher led2(12, 100, 400);

// Flash an LED forever.
// Flasher led1(13, 100, 400, 1);

// Used for testing purposes only.
byte led1doOnce = 0;
byte led2doOnce = 0;

void setup(){}

void loop(){

  // Call the Update function as fast as possible.
  led1.Update();
  led2.Update();

  // Test output. Flash 2 LEDs with different on/off times.
  // Use the Start() function to flash each LED X number
  // of times, then restart each LED flasher object with
  // a different number of flashes.

  // Start the led1 object 4 seconds after Arduino startup.
  if(millis() > 4000 && millis() < 5000 && led1doOnce == 0){
    led1.Start(3);  // Flash the LED 3 times.
    led1doOnce = 1;
  }

  // Start the led2 object 5 seconds after Arduino startup.
  if(millis() > 5000 && millis() < 6000 && led2doOnce == 0){
    led2.Start(5);  // Flash the LED 5 times.
    led2doOnce = 1;
  }

  // Restart the led1 object 10 seconds after Arduino startup.
  if(millis() > 10000 && millis() < 11000 && led1doOnce == 1){
    led1.Start(7);  // Flash the LED 7 times.
    led1doOnce = 0;
  }

  // Restart the led2 object 15 seconds after Arduino startup.
  if(millis() > 15000 && millis() < 16000 && led2doOnce == 1){
    led2.Start(9);  // Flash the LED 9 times.
    led2doOnce = 0;
  }

}
Added a Start() function to the class so LED flashing is event driven and is restartable with a different number of flashes.
Source Link
VE7JRO
  • 2.5k
  • 19
  • 28
  • 31

I've modified your sketch so that when you instantiate a LED Flasher object with 4 argumentsa 4th argument, it flashes the number of times you specifyforever. If you instantiate a Flasher object with 3 arguments, use the Start() function to start the LED flashing the number of times you specify. Just call it once, not repeatedly in the loop() function (e.g. when a button is pressed). You can call the Start() function again and specify a different number of flashes. You can have multiple LED flasher objects. Each object can have different on/off times. Some LEDs can flash forever, and some the number of times specified.

class Flasher{

  // Class Member Variables. These are initialized at startup.
  byte ledPin;
  byte ledState;
  byte flashForever;
  unsigned int flashCounter;
  unsigned long OnTime;
  unsigned long OffTime;
  unsigned long previousMillis;
     
  // Constructor. Create a LED Flasher object and.
  // initializeInitialize the member variables and LED state.
  public:
  Flasher(byte pin, unsigned long on, unsigned long off, unsigned intbyte countercontinueFlashing = 0){
    ledPin = pin;
    pinMode(ledPin, OUTPUT);
    OnTime = on;
    OffTime = off;      
    ledState = LOW;
    flashCounter = counter;
    if(flashCounter == 0){
      flashForever = 1;
    }
    else{
      flashForever = 0;
      flashCounter = counter * 2;
    }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 >= OnTime) && ((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 >= OffTime) && ((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 flashNunmberOfTimes){
    flashCounter = flashNunmberOfTimes * 2;
    flashForever = 0;
    previousMillis = 0;
  }

};

// Instantiate LED Flasher ObjectsObject(s).
 
// Flash forevere.
//g. Flasher led1object_name(12Pin Number, 100ON Time, 500OFF Time, OPTIONAL Flash Forever);

// Flash XThe number of times to flash the LED is specified
// when you call the Start() function.
Flasher led1(1213, 20010, 1800990);
Flasher led2(12, 5100, 400); 

// Flash an LED forever.
// Flasher led2led1(13, 10100, 990400, 101);

// Used for testing purposes only.
byte led1doOnce = 0;
byte led2doOnce = 0;

void setup(){}

void loop(){ 

  // Call the Update function as fast as possible.
  led1.Update();
  led2.Update();

  // Test output. Flash 2 LEDs with different on/off times.
  // Use the Start() function to flash each LED X number
  // of times, then restart each LED flasher object with
  // a different number of flashes.

  // Start the led1 object 4 seconds after Arduino startup.
  if(millis() > 4000 && millis() < 5000 && led1doOnce == 0){
    led1.Start(3);  // Flash the LED 3 times.
    led1doOnce = 1;
  }

  // Start the led2 object 5 seconds after Arduino startup.
  if(millis() > 5000 && millis() < 6000 && led2doOnce == 0){
    led2.Start(5);  // Flash the LED 5 times.
    led2doOnce = 1;
  }

  // Restart the led1 object 10 seconds after Arduino startup.
  if(millis() > 10000 && millis() < 11000 && led1doOnce == 1){
    led1.Start(7);  // Flash the LED 7 times.
    led1doOnce = 0;
  }

  // Restart the led2 object 15 seconds after Arduino startup.
  if(millis() > 15000 && millis() < 16000 && led2doOnce == 1){
    led2.Start(9);  // Flash the LED 9 times.
    led2doOnce = 0;
  }

}

I've modified your sketch so that when you instantiate a Flasher object with 4 arguments, it flashes the number of times you specify. If you instantiate a Flasher object with 3 arguments, it flashes forever.

class Flasher{

  // Class Member Variables. These are initialized at startup.
  byte ledPin;
  byte ledState;
  byte flashForever;
  unsigned int flashCounter;
  unsigned long OnTime;
  unsigned long OffTime;
  unsigned long previousMillis;
     
  // Constructor. Create a Flasher object and
  // initialize the member variables and state.
  public:
  Flasher(byte pin, unsigned long on, unsigned long off, unsigned int counter = 0){
    ledPin = pin;
    pinMode(ledPin, OUTPUT);
    OnTime = on;
    OffTime = off;      
    ledState = LOW;
    flashCounter = counter;
    if(flashCounter == 0){
      flashForever = 1;
    }
    else{
      flashForever = 0;
      flashCounter = counter * 2;
    }
    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) && ((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 >= OffTime) && ((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.
    }
  }
};

// Instantiate LED Flasher Objects.
 
// Flash forever.
// Flasher led1(12, 100, 500);

// Flash X number of times.
Flasher led1(12, 200, 1800, 5);
Flasher led2(13, 10, 990, 10);

void setup(){}

void loop(){
  led1.Update();
  led2.Update();
}

I've modified your sketch so that when you instantiate a LED Flasher object with a 4th argument, it flashes forever. If you instantiate a Flasher object with 3 arguments, use the Start() function to start the LED flashing the number of times you specify. Just call it once, not repeatedly in the loop() function (e.g. when a button is pressed). You can call the Start() function again and specify a different number of flashes. You can have multiple LED flasher objects. Each object can have different on/off times. Some LEDs can flash forever, and some the number of times specified.

class Flasher{

  // Class Member Variables. These are initialized at startup.
  byte ledPin;
  byte ledState;
  byte flashForever;
  unsigned int flashCounter;
  unsigned long OnTime;
  unsigned long OffTime;
  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);
    OnTime = on;
    OffTime = 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 >= OnTime) && ((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 >= OffTime) && ((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 flashNunmberOfTimes){
    flashCounter = flashNunmberOfTimes * 2;
    flashForever = 0;
    previousMillis = 0;
  }

};

// Instantiate LED Flasher Object(s).
// e.g. Flasher object_name(Pin Number, ON Time, OFF Time, OPTIONAL Flash Forever);

// The number of times to flash the LED is specified
// when you call the Start() function.
Flasher led1(13, 10, 990);
Flasher led2(12, 100, 400); 

// Flash an LED forever.
// Flasher led1(13, 100, 400, 1);

// Used for testing purposes only.
byte led1doOnce = 0;
byte led2doOnce = 0;

void setup(){}

void loop(){ 

  // Call the Update function as fast as possible.
  led1.Update();
  led2.Update();

  // Test output. Flash 2 LEDs with different on/off times.
  // Use the Start() function to flash each LED X number
  // of times, then restart each LED flasher object with
  // a different number of flashes.

  // Start the led1 object 4 seconds after Arduino startup.
  if(millis() > 4000 && millis() < 5000 && led1doOnce == 0){
    led1.Start(3);  // Flash the LED 3 times.
    led1doOnce = 1;
  }

  // Start the led2 object 5 seconds after Arduino startup.
  if(millis() > 5000 && millis() < 6000 && led2doOnce == 0){
    led2.Start(5);  // Flash the LED 5 times.
    led2doOnce = 1;
  }

  // Restart the led1 object 10 seconds after Arduino startup.
  if(millis() > 10000 && millis() < 11000 && led1doOnce == 1){
    led1.Start(7);  // Flash the LED 7 times.
    led1doOnce = 0;
  }

  // Restart the led2 object 15 seconds after Arduino startup.
  if(millis() > 15000 && millis() < 16000 && led2doOnce == 1){
    led2.Start(9);  // Flash the LED 9 times.
    led2doOnce = 0;
  }

}
Source Link
VE7JRO
  • 2.5k
  • 19
  • 28
  • 31
Loading