ArduinoLibs
|
This example shows how to use the ChaseLEDs class to simulate the Cylon eye effect from Battlestar Galactica. Digital outputs are used to drive six LED's in a back and forth motion, using the following schematic:
We start by including the ChaseLEDs class:
The next step is to define the pins that the chase will run over:
The chase runs from the first pin to the sixth pin and back again, with each LED lit for 100 milliseconds before moving onto the next one. To complete the example, we need to call ChaseLEDs::loop() each time around our main loop to cause the chase to run:
While this example uses only six pins, it can be easily extended to any number of pins by modifying the pins
array and altering the schematic accordingly.
So far we are chasing only a single LED. We could change this to chase two adjacent LED's instead by defining a new CylonChase
class that inherits from ChaseLEDs:
The important part is the implementation of the advance()
method, which overrides ChaseLEDs::advance() to provide our own scheme for lighting the LED's each time the chase advances. We use ChaseLEDs::previousPin() to get the pin that is 2 steps back in the sequence, set it to LOW, and then set the previous pin (1 step back) and the next pin to HIGH. All that remains is to change our chase initialization to use the new class:
We can do even better than this. Instead of fully lighting both LED's, we could instead use the PWM outputs to dim the previous pin, creating a kind of "trailing flame" effect:
The current chase is fixed at 100 milliseconds per LED, which takes a full second to run the sequence. An alternative to hard-wiring the chase rate is to hook up a 10K potentiometer to the A0 analog input:
We then modify the advance()
method to read the new chase rate from the potentiometer each time the LED advances:
The full source code for the final version of the example follows: