Skip to main content

Reply to "Arduino Lesson: How to Create a Library"

To finish up with this lesson, we will add a flashing LED feature on top of the PulseLed class. This will provide both flashing and fading capabilities in a class called "FlashLed". Note that because the PulseLed class can disable fading, the flashing feature will work either way; with fading or without.

Let's look at the header file for the FlashLed class.

/*
FlashLed.h - Flash LED Class Header File
*/

#ifndef FlashLed_h      // one time wrapper
#define FlashLed_h    // wrapper definition

#include <Arduino.h>
#include "PulseLed.h"

#define BASECLASS PulseLed

class FlashLed : public BASECLASS {
private:
bool _flashing = false;          // true when LED is flashing
int _onTime = 1000;              // flash on time in milliseconds
int _offTime = 1000;             // flash off time in milliseconds
unsigned long _timer = 0;   // millisecond timer
int _cycles = 0;                       // flash cycles counter
int _count = 0;                        // flash request count

public:
void setOnTime (int onTime);
void setOffTime (int offTime);
void on ();
void off ();
void toggle ();
void flash ();
void flash (int count);
void update ();
};

#endif         // end wrapper

You will notice that we again put a wrapper around the class definition. We include the standard system "Arduino.h" file and our own "PulseLed.h". Then we define a BASECLASS that the preprocessor will substitute both here and in the implementation file. Change the name of the base class and you have a different thing. Maybe you want to flash regular light bulbs. We've planned ahead for that possibility.

As I mentioned earlier, with a few changes to our PlainLed class, we could use that underlying functionality with this new flashing class to create a flasher that would work with LEDs that are not connected to the PWM pins of the Arduino. In other words, we would be able to flash regular LEDs but without the fading capability.

So BASECLASS will steer our FlashLed to use the proper functions for "on", "off", and "toggle" operations. Even though we define these functions for this class, the BASECLASS functions are called upon to do the actual work. You will see this in the implementation file.

Next is the class definition:

class FlashLed : public BASECLASS {

FlashLed is derived from the BASECLASS that has been defined as the PulseLed class.

Following this are member variables that are used to control the flashing capability. There is an on time and an off time that define the flash cycle in terms of milliseconds. These are initialized to values of 1 second on and 1 second off. There's a timer value that will mark the start of the flashing cycle. There's also a cycle counter for use with the "flash(count)" function.

There are corresponding functions to set the on time and off time of the flash cycle. Then come the familiar "on", "off", and "toggle" functions. Using any of these operations will stop any flashing of the LED. The "flash" function starts the flash cycle which will run continuously until stopped with "on", "off" or "toggle". To cause the LED to flash a specified number of times, use "flash(count)".

The "update" function works in the same way as the "update" function for the PulseLed class. This is where changes occur based on timing.

The new class files are included in the attachments below. Comments and questions may be submitted as you have the freedom to decide what should be done in a particular situation. My email is also available in my profile. The test program and video should be up shortly.

  -- Leo

Attachments

Last edited by Consolidated Leo

OGR Publishing, Inc., 1310 Eastside Centre Ct, Suite 6, Mountain Home, AR 72653
800-980-OGRR (6477)
www.ogaugerr.com

×
×
×
×
×