The Arduino library is heavily based off of bliptronics' original code, except we library-ized it and trimmed it down.
The library can be installed from the Arduino library manager.
Open up the Arduino library manager:
Search for the LPD6803 library and install it
We also have a great tutorial on Arduino library installation at:
http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use
After installing the LPD6803 library, you should be able to access the sample code by navigating through menus in this order: File→Sketchbook→Libraries→LPD6803→strandtest
Using the Library
The code to drive these dots is fairly simple, except that the PWM pin and the data clock pin are shared, meaning you can't just stick the PWM pin on a hardware timer output because then it's not possible to use it for the data clocking. Instead, the library uses an interrupt that goes off every few milliseconds. If there is data to be updated on the strip, it sends that data. If not, it just pulses pin to keep the PWM going.
Note that the interrupt uses Timer 1, meaning the pin 9 and 10 PWM outputs will not work for servos at the same time. If you need this capability, please use a 'software servo' library!
Let's look through the strandtest example code. To use the library in an Arduino sketch, you'll first need to globally declare an LPD6803 object to talk to the strip. It is invoked with three variables: the number of pixels and the data and clock pins:
int dataPin = 2; // 'yellow' wire int clockPin = 3; // 'green' wire // Set the first parameter to the NUMBER of pixels. 20 = 20 pixels in a row LPD6803 strip = LPD6803(20, dataPin, clockPin);
Next, we initialize the strip in the setup() procedure:
void setup() { // The Arduino needs to clock out the data to the pixels // this happens in interrupt timer 1, we can change how often // to call the interrupt. setting CPUmax to 100 will take nearly all all the // time to do the pixel updates and a nicer/faster display, // especially with strands of over 100 dots. // (Note that the max is 'pessimistic', its probably 10% or 20% less in reality) strip.setCPUmax(50); // start with 50% CPU usage. up this if the strand flickers or is slow // Start up the LED counter strip.begin(); // Update the strip, to start they are all 'off' strip.show(); }
setCPUmax() configures the Timer 1 interrupt that drives the strand. You can change this from 0 to 100, and it runs in the background. The 'max' is calculated assuming the longest possible timing (when sending data) so its a bit pessimistic. 50% should be plenty, you can mess with this to make the strip more or less 'flickery' You can change this 'on the fly' so for example set it to 0% just before doing a bunch of Ethernet stuff, then back to 50% later.
begin() actually starts the interrupt.
show() refreshes the displayed colors of the LEDs. You'll need to call show() after changing any pixel colors to see this reflected in the LEDs. This way you can change the entire strip at one time (it takes the same amount of time to change one pixel as it does for an entire strip, because the full strip data must be issued regardless).
Last, we'll look at an example function, colorWipe(). This creates a 'chase' sequence that fills the strip up with a color. It is basically a loop that increments through every pixel (which you can query with the numPixels() function) and sets the color of each (incremented with i) to the value passed (c — colors are expressed as a 16-bit variable type, though only the bottom 15 bits are used). The strip output is then updated with show(). Finally there is some delay (otherwise this would happen instantly).
Below that is a helper function that converts a color from separate 5-bit red, green and blue values into a combined 15-bit value (suitable for passing to colorWipe()). The brightness range is from 0 (off) to 31 (max brightness).
// fill the dots one after the other with said color // good for testing purposes void colorWipe(uint16_t c, uint8_t wait) { int i; for (i=0; i < strip.numPixels(); i++) { strip.setPixelColor(i, c); strip.show(); delay(wait); } } /* Helper functions */ // Create a 15 bit color value from R,G,B unsigned int Color(byte r, byte g, byte b) { //Take the lowest 5 bits of each value and append them end to end return( ((unsigned int)g & 0x1F )<<10 | ((unsigned int)b & 0x1F)<<5 | (unsigned int)r & 0x1F); }
For example, in the loop() function we call colorWipe(Color(31, 0, 0), 50) which will fill the strand with full-brightness red light, pausing about 50 milliseconds between pixels.
colorWipe(Color(31, 0, 0), 50); // red fill colorWipe(Color(0, 31, 0), 50); // green fill colorWipe(Color(0, 0, 31), 50); // blue fill
Page last edited March 08, 2024
Text editor powered by tinymce.