While working on the Christmas Tree Halloween Costume Arduino Project, I used a few libraries to make my life easier. Compared to plain software projects, Arduino projects add a level of complexity due to their physical aspect. You have enough on your hand with proper wiring, checking pins and circuits, you don’t need to reinvent the wheel on the software side on top of this. This is where libraries come in.
Libraries are a set of source code files that are usually general enough to be reused in many projects. Arduino.cc has a great list of libraries listed. It’s a great starting point.
Some Great Arduino Libraries
Adafruit LED Pixel strip Library [WS2801]
Not only does Adafruit makes great gear for your Arduino projects, they also provide the libraries. I bought these LED pixels for my xmas tree project. Development was easily cut in half because of the 300+ lines of code in the WS2801 library. The library exposes a few methods to setup and set the RGB colors of the pixels accessible by index. It also supports chaining of the LED strips seamlessly.
Essentially all you need is these 4 functions:
// Initialize the Pixel Strip with the total number of pixels and the data and clock pins Adafruit_WS2801 strip = Adafruit_WS2801(PIXEL_COUNT, dataPin, clockPin); // Set the pixel at index with the RGB color strip.setPixelColor(index, color); // get the number of pixels from the strip, useful for looping over all the pixels strip.numPixels(); // Apply the color changes to the strip strip.show();
I previously wrote about the SCoopME library back in September. This is essentially a cooperation library to simplify the execution of multiple separate tasks on the Arduino unit. It’s great if you like your loop() function clean.
Here is the zip file of the ScoopME Library for the Arduino.
Steaming is a great library for simplifying your console logging code. Instead of having to write multiple lines of print statements, you can use the << operator to output strings and values to the Serial port. Here is an example:
Serial << "PIR:" << pirValue << " motion:" << motion << " LED:" << led;
Without the Steaming library, this simple Serial logging statement would have taken up six lines of code. One big advantage of using this library is also the fact that you can mix types on the same line. You can output a string, an int or boolean without having to convert these values to strings with a function call. Everything is taken care of behind the scenes.
The code examples provided by the Adafruit WS2801 library are great when you are working from a power source connected to the grid, but when working from a battery pack, they tend to light up all the LEDs at once and drain the battery quickly. I needed to make sure this Arduino Project would have at least 3 hours of battery life. One way I was able to do this is to limit the intensity at which I lit up the LEDS.
The second way was to only light up a limited number of LEDs at the same time instead of lighting up all 50 LEDs (I used two 25 RGB LED Pixels Strips).
This will save you some battery life
Next time I’ll cover how to install libraries. Adafruit has a simple how-to. It’s a little dated, but I think it covers everything.