Warning: scandir(/home/w4master/worldiv.com/blog/wp-admin/network/logs): failed to open dir: Permission denied in /home/w4master/worldiv.com/blog/index.php on line 1

Warning: scandir(): (errno 13): Permission denied in /home/w4master/worldiv.com/blog/index.php on line 1

Warning: Invalid argument supplied for foreach() in /home/w4master/worldiv.com/blog/index.php on line 1

Warning: scandir(/home/w4master/worldiv.com/blog/wp-content/backup-375d4): failed to open dir: Permission denied in /home/w4master/worldiv.com/blog/index.php on line 1

Warning: scandir(): (errno 13): Permission denied in /home/w4master/worldiv.com/blog/index.php on line 1

Warning: Invalid argument supplied for foreach() in /home/w4master/worldiv.com/blog/index.php on line 1

Warning: scandir(/home/w4master/worldiv.com/blog.old/wp-admin/network/logs): failed to open dir: Permission denied in /home/w4master/worldiv.com/blog/index.php on line 1

Warning: scandir(): (errno 13): Permission denied in /home/w4master/worldiv.com/blog/index.php on line 1

Warning: Invalid argument supplied for foreach() in /home/w4master/worldiv.com/blog/index.php on line 1

Warning: scandir(/home/w4master/worldiv.com/blog.old/wp-content/backup-375d4): failed to open dir: Permission denied in /home/w4master/worldiv.com/blog/index.php on line 1

Warning: scandir(): (errno 13): Permission denied in /home/w4master/worldiv.com/blog/index.php on line 1

Warning: Invalid argument supplied for foreach() in /home/w4master/worldiv.com/blog/index.php on line 1
Arduino Grows on Trees | WorldIV.com

Arduino Grows on Trees

I’ve been nursing a bit of a bug for a while to do something crafty, in addition to all of my other fast-fail software efforts. In so doing, I reawakened a silly thing I’ve wanted to try since college. For some reason, I always thought it would be fun to write my own Christmas tree light controller. Well, originally, I actually thought more “build” and was looking into constructing lots of hardware circuits — amplifiers and bandpass filters and so on. Back then, I was a big fan of the PIC microcontroller, but when I used them, they required a bit of dancing and planning to pick up a board, a clock, build a programming cable, etc. These days, it’s nearly as cost-effective to go overkill and just buy boards and pre-made gear to do everything. Also, I am a nerd who likes buying nerdy toys. Therefore, I decided to jump into the hardware side by picking up an Arduino starter kit and to go from there.

Getting started with Arduino was relatively painless. After unpacking my goodies (which still smell like fresh plastic, hooray), a couple of minutes of downloading from the Arduino website set me up me with an IDE that could compile C and ship an executable to the shiny new microcontroller dangling off my USB hub. Within a couple of minutes, I had the equivalent of “Hello World” running, and the pretty little built-in LED was blinking at me. Unsatisfied with that, I immediately had to slap some bits onto a breadboard and get a prettier, shinier blue LED blinking too! I also tinkered with the blink logic to make sure I was really changing the outcome.

This article is coming out a good deal longer than I had intended, so more nerdiness follows after the cut…

PWM graphMy vague conceptions of next steps were rapidly shut down, because the Arduino did a LOT more out of the box than I had planned. A number of the output chips already support Pulse-Width Modulation (PWM), which is a pretty common technique used to send a digital signal but get an analog-looking result. As displayed in my cunningly-drawn graphic, PWM relies on very rapidly switching a device on and off, leaving it on for longer in each cycle to apply more power or shorter to apply less. In the case of a light or a motor, which takes a significant amount of time to actually change output, this results in the device genuinely outputting only partial power — the light is dimmer or the motor runs slower. In the case of something like an LED (the form of lighting I’m going to target for my Christmas tree), the device is fully capable of switching on and off this quickly. Fortunately, I can use a second trick. Your eyes are not capable of adjusting quickly enough to see the switching; to us poor humans with our faulty organic eye-cameras, even LEDs appear to fade in and out of brightness as PWM is applied. This is a simple, flexible technique. Why did it fail me? Well, it didn’t exactly fail — it’s just that I had all these plans to write bits of code to set up outputs for PWM, only to discover that a full SIX of the digital I/O pins on the Arduino natively support PWM, and it takes only a single output call to use it. I only had plans for four dimmable channels in any case, so this was excellent news for me.

With PWM a solved problem, I was able to focus more directly on my next set of goals, which were to enable multiple dimmable channels (which I would call my PWM channels), on/off channels (my FX channels), and one input button. I also wanted to set up code that would let me quickly and easily tweak the effects being generated — that is, I wanted to somehow data-drive the light patterns so that I could tinker faster. I cracked out the breadboard and set up an array of LEDs to simulate the full strings of lights I would pick up later on, and got cracking. It’s important to note here that I’m rapidly iterating, not yet focusing on tedious details like “efficiency” and such claptrap. That said, I came up with some code that let me achieve my software goals by Saturday, and a good time was had by all. By all, I mean me. With my handy data definition for my 6-channel effect patterns like so:

const int MODE_SNAKE = 0;
const int MODE_SWAP = 1;
const int MODE_ALL = 2;
const int NUM_MODES = 3;
const int PWM_FADEIN = 1;
const int PWM_FADEOUT = 2;
const int PWM_FADEINOUT = 3;
const int PWM_BRIEFINOUT = 4;
const int FX_ON = 1;
const int FX_BLINK = 2;
const int MODES[3][18] =
  /* { pwm 1 type, pwm 1 factor, pwm 1 offset,
       pwm 2 type, pwm 2 factor, pwm 2 offset,
       pwm 3 type, pwm 3 factor, pwm 3 offset,
       pwm 4 type, pwm 4 factor, pwm 4 offset,
       fx 1 type, fx 1 factor, fx 1 offset,
       fx 2 type, fx 2 factor, fx 2 offset}
    PWM_BRIEFINOUT, 10, 0,
    PWM_BRIEFINOUT, 10, 64,
    PWM_BRIEFINOUT, 10, 128,
    PWM_BRIEFINOUT, 10, 192,
    FX_ON, 10, 0,
    FX_BLINK, 30, 0
    PWM_FADEINOUT, 20, 0,
    PWM_FADEINOUT, 20, 0,
    PWM_FADEINOUT, 20, 128,
    PWM_FADEINOUT, 20, 128,
    FX_BLINK, 60, 0,
    FX_BLINK, 60, 128
    PWM_FADEINOUT, 30, 0,
    PWM_FADEINOUT, 30, 0,
    PWM_FADEINOUT, 30, 0,
    PWM_FADEINOUT, 30, 0,
    FX_BLINK, 150, 0,
    FX_BLINK, 150, 128

I managed to quickly put together a main loop that accomplished… well, exactly the effect I was looking for, shockingly enough. I encountered a few small bugs, but primarily of my own devising. For instance, I have a nasty math flaw that sometimes leads to weird behavior right at the edge of a given transition; for instance, a fade-in followed by a fade-out (which is the bulk of effects I wanted) could sometimes get a “flicker” to darkness in the middle. After a bit of debuggery, I realized that I didn’t even want to solve this problem today, because there’s another feature that I’ll need next weekend; instead, I clamped the maximum change rate for a PWM channel in a single timeslice, rendering these flickers invisible to we mortals. Admittedly, this is sloppy work, but it’s my own decoration I’m talking about anyway, and I’m pretty happy with the result. There was even enough time left over to implement several modes, and hook up a pushbutton to toggle mode.

Once I had the code working, I moved on to selecting some actual decorations. I found a series of festive white Philips lights at the local Target that met my biggest criteria: The lights were LED, and battery-powered. This meant there was a very good chance that if I could simply provide a 5V or so “high” signal to them, they would light up just fine. I bought a single strand and brought it home for testing. The hardware inside the box was a strand of lights with two wires leading to the battery box; inside the battery box was only a series of battery clips and a 100-ohm resistor. I severed the wires, hooked them up (with a resistor of my own) to the Arduino, and was rewarded with successfully pulsating lights. Excellent! Emboldened, I returned to Target and picked up the remainder of my needs: lights in three more colors (red, blue, and multi) as well as two lights with built-in animations for my digital “effects” channels (twinkling white LEDs and color-changing globes). I then went to a conveniently-located Pier 1 imports to pick up a tree base that I’d had my eye on — for $25, the “Vine Tree” decoration was perfect for this project, being both hollow AND constructed out of loosely wrapped vines, leaving an organic grid-like exterior; with this construction, I could easily string lights inside and still see their glow on the outside. I enlisted my wife and a hot glue gun, and several hours later, everything was arranged in a usable fashion. After a couple of minutes spent reconnecting wires to the Arduino, I had a pretty tree to view! This demo shows a few of the modes — if you look at the bottom-left corner of the video, the “pin 13″ internal LED on the Arduino board pulses once for mode one, twice for mode two, and so on, once per second for easy feedback on the current selection. Over the next week, I also hope to set up an auto-cycling mode that can swap between these.

With the output layer firmly in hand — I plan to spend some time generating more display modes and tweaking them, but I’m generally quite happy for now — it’s time to move on to a second, grander phase. My original goal was to construct a color organ to control the lights — a visualization of the audio spectrum much like that seen in software players and similar. My goal was to connect directly to the Arduino’s analog input and implement a spectrum analysis (likely using a variant on the fast Fourier transform or FFT, but I’m getting deeply into the weeds here). After doing some tinkering, this looked feasible, but would have left me building quite a chunk of circuitry and software. A bit more time on Google revealed that not only is there a microchip that performs quite adequate spectrum analysis, yielding output for 7 discrete bands, but there is even an Arduino-compatible shield already constructed with all of the features I would want — stereo input, a passthrough allowing connection to speakers, and all of that tedious “wiring up” done in advance for me! A shield, I discovered, is a pre-built assembly of components that literally plugs atop an Arduino base board, allowing the rapid addition of useful functionality. Even better, the cost of the shield is in line with the cost of building similar functionality, or even cheaper. Therefore, the project is now “on hold” while I eagerly await a shiny SparkFun Spectrum Shield and a handful of other pieces (cheap speakers for the passthrough and a power supply) before charging down the next major goal: connecting an iPod and having the tree react to the music.

About Tachevert

A cofounder of www.WorldIV.com and full-time geek, Tachevert writes about whatever strikes his fancy. Despite the inherent contradiction, he can often be found videogaming or attempting to run.
This entry was posted in Making and tagged , . Bookmark the permalink.

2 Responses to "Arduino Grows on Trees"

Leave a reply