Skip to main content

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

Now that we have a working version of the PlainLed class, we can add new capabilities to extend the feature set of an LED connected to an Arduino. We want to make use of the variable intensity brightness of an LED matched up with the PWM output pins of the Arduino. This will allow us to create a fading effect when the LED is turned on or off.

Recall that a PlainLed will work with any of the Digital I/O pins of the Arduino. I think that even the Analog pins can be used with "digitalWrite" once they've been setup for output mode. But now we will be using the PWM outputs with the LEDs. The Arduino Nano has 6 individual pins that are setup to operate in this manner. We will be using pins 9, 10, and 11 for our tests.

We will create a new class called "PulseLed" that builds upon what we have already done. It will be a derived class of PlainLed. In this new relationship, PlainLed is the base or parent class and PulseLed is the derived or child class.

A derived class inherits all of the functionality of the base class. However, some of the base functionality can be overridden by the derived class. In other words, we can change the behavior of the "on" and "off" routines to achieve the desired fading effect.

To do this we need to declare new versions of the public functions that we wish to change. For our PulseLed class these include "on", "off", and "toggle". The base class versions used "digitalWrite" to turn the LEDs on and off. For our new class, we will be using "analogWrite" instead.

PulseLed will include new functions as well. These include "setMax", "setFading", "setUpTime", "setDownTime", and "update". Their usage is explained in the C++ implementation file "PulseLed.cpp" (see the attachments below). These new functions allow us to change settings that affect the class. We can specify how long it takes to fade up or fade down, set the value to use for full intensity brightness, and control if the fading feature should be enabled or disabled.

Here's the class header file for PulseLed:

/*
PulseLed.h - Pulse Width Modulation LED Class Header File
*/

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

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

#define DIVISIONS 25     // number of divisions of fading

class PulseLed : public PlainLed {
private:
byte _max = 250;                   // maximum intesity (0-255)
bool _enabled = true;          // true if fading is enabled
int _upTime = 500;               // fade up time in milliseconds
int _downTime = 500;         // fade down time in milliseconds
byte _upTimeInc = 20;        // fade up time increment ms.
byte _downTimeInc = 20;   // fade down time increment ms.
byte _valueInc = 10;            // fade value increment
byte _value = 0;                   // fade value
bool _fading = false;           // true while fading
bool _fadeUp = true;          // true if fading up
unsigned long _timer = 0; // millisecond timer

// private functions
void turnOn ();
void turnOff ();
void changeFade ();

public:
void setMax (byte max);                  // set max intensity value
void setFading (bool enabled);      // set fading enabled
void setUpTime (int upTime);        // set fade up time ms.
void setDownTime (int downTime);   // set fade down time ms.
void on ();                                           // turn the LED on
void off ();                                          // turn the LED off
void toggle ();                                   // change state of LED
void update ();                                 // time based update
};

#endif // end wrapper

Not a whole lot has changed from what we have seen before. There is still the preprocessor wrapper around the content. We have a class definition with private and public sections. There are member variables all declared "private" this time. There are "public" function prototypes for the operations that can be called on objects of this class. But there are some important differences.

The first is that the PlainLed header file is included. Without that, we could not declare that our new class is derived from the base class. This is done in the class declaration with:

class PulseLed : public PlainLed {

There are some utility private functions declared that are only available within the class itself. Everything else is pretty easy to comprehend.

Moving on to the implementation, there are a couple of things to mention. The "update" function is used to change the PWM value when the LED is fading up or down. It is intended to be used within the real-time processing "loop" on a somewhat frequent basis. Without it, no fading can occur. Keep in mind the relative timing differences between milliseconds (LED fade time) and microseconds (processor execution time).

All of the functionality of the base class is still available. Even with an object declared from PulseLed, the functions that are overridden can be referenced using the syntax myLed.PlainLed:: on();

Our new class does not include a "begin" function. That's because it already exists in the base class. Also retained from the base class (PlainLed) are "isOn" and "isOff". Remember that the "begin" function is used to associate our objects with a particular Arduino output pin.

Recall also that in PlainLed.h, we declared the member variables "_pin" and "_isOn" as protected. That allows the implementation of our derived PulseLed class to make use of these variables directly. If they had been set as private, we would have no access. If they were public, anyone could change them and that's bad policy.

Finally, a test program was developed that looks much like the test program for the PlainLed class. In this test, of course the green, yellow, and red LEDs are now declared as PulseLed objects. The "setup" is the same. The "loop" now includes switching the fading feature on and off after all the LEDs have been run through the test phases.

The circuit is exactly the same as before.

Pretty cool !!!  But wait, there's more.

Next time we'll get the LEDs to flash with a new class derived from the PulseLed class. Should be fun!

  -- 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

×
×
×
×
×