ArduinoLibs
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Public Member Functions | Protected Member Functions | List of all members
ChaseLEDs Class Reference

Chase LED's on output pins in a defined sequence. More...

#include <ChaseLEDs.h>

Public Member Functions

 ChaseLEDs (const uint8_t *pins, int num, unsigned long advanceTime)
 Initializes the LED chaser. More...
 
void loop ()
 
unsigned long advanceTime () const
 Returns the number of milliseconds that each LED will be lit in the chase sequence. More...
 
void setAdvanceTime (unsigned long advanceTime)
 Sets the number of milliseconds to advance between LED's to advanceTime. More...
 

Protected Member Functions

virtual void advance (uint8_t prevPin, uint8_t nextPin)
 Advances to the next LED in sequence, turning off prevPin, and turning on nextPin. More...
 
uint8_t previousPin (int n) const
 Returns the pin that is n steps back in the sequence. More...
 

Detailed Description

Chase LED's on output pins in a defined sequence.

The following example performs a LED chase over the 6 PWM outputs on the Arduino Uno, with a 150 millisecond delay between each LED:

uint8_t pins[] = {3, 5, 6, 9, 10, 11};
ChaseLEDs chaser(pins, sizeof(pins), 150);
void loop() {
chaser.loop();
}

After pin 11 is lit, the pattern will repeat at pin 3. To cause the chase to oscillate back and forth instead, extend the sequence as follows:

uint8_t pins[] = {3, 5, 6, 9, 10, 11, 10, 9, 6, 5};
ChaseLEDs chaser(pins, sizeof(pins), 150);

See the Cylon example for more information on how to use the ChaseLEDs class in a practical application.

Definition at line 28 of file ChaseLEDs.h.

Constructor & Destructor Documentation

ChaseLEDs::ChaseLEDs ( const uint8_t *  pins,
int  num,
unsigned long  advanceTime 
)

Initializes the LED chaser.

The chase sequence consists of num pins, whose names are given by the pins array. Each LED is lit for advanceTime milliseconds before advancing to the next LED.

This constructor configures all of the pins for output and sets their state to be LOW. The first LED will be lit when the program first calls loop().

See Also
loop()

Definition at line 71 of file ChaseLEDs.cpp.

Member Function Documentation

void ChaseLEDs::advance ( uint8_t  prevPin,
uint8_t  nextPin 
)
protectedvirtual

Advances to the next LED in sequence, turning off prevPin, and turning on nextPin.

The default implementation is equivalent to the following code:

digitalWrite(prevPin, LOW);
digitalWrite(nextPin, HIGH);

This method may be overridden in subclasses to provide special effects. See the documentation for previousPin() for some example effects.

See Also
previousPin()

Definition at line 136 of file ChaseLEDs.cpp.

unsigned long ChaseLEDs::advanceTime ( ) const
inline

Returns the number of milliseconds that each LED will be lit in the chase sequence.

See Also
setAdvanceTime(), advance()

Definition at line 35 of file ChaseLEDs.h.

void ChaseLEDs::loop ( )

Perform a single iteration of the control loop for this LED chaser.

Definition at line 87 of file ChaseLEDs.cpp.

uint8_t ChaseLEDs::previousPin ( int  n) const
inlineprotected

Returns the pin that is n steps back in the sequence.

If n is zero, then the current pin is returned; if n is 1, then the previous pin is returned; and so on.

This function may be called by subclasses in their advance() method to manipulate pins that are further back in the chase sequence than the immediately previous pin.

For example, the following code implements a LED chaser that lights two pins at a time:

void DoubleChaser::advance(uint8_t prevPin, uint8_t nextPin)
{
digitalWrite(previousPin(2), LOW);
digitalWrite(prevPin, HIGH);
digitalWrite(nextPin, HIGH);
}

As another exmaple, the following code uses PWM outputs to fade out the previous pin rather than turn it off immediately:

void FadingChaser::advance(uint8_t prevPin, uint8_t nextPin)
{
digitalWrite(previousPin(2), LOW);
analogWrite(prevPin, 32);
digitalWrite(nextPin, HIGH);
}

Note: it is possible to retrieve the following pin in sequence using previousPin(-1). This could be used to fade in the LED that follows nextPin.

See Also
advance()

Definition at line 40 of file ChaseLEDs.h.

void ChaseLEDs::setAdvanceTime ( unsigned long  advanceTime)
inline

Sets the number of milliseconds to advance between LED's to advanceTime.

See Also
advanceTime(), advance()

Definition at line 36 of file ChaseLEDs.h.


The documentation for this class was generated from the following files: