Saturday, October 12, 2013

Lighting (Your Pumpkin) wth Arduino


Earlier this week, we showed you how to make your own eternal jack-o-lantern. You could toss some LED candles in there or you could jack that thing up to 11...

This will be the first of (hopefully) multiple posts on using the Arduino logic control platform for various home uses. I'm not a brilliant programmer, and that's kind of the point: I'm learning the system by looking at the world, deciding what I need to happen, and figure out how to get there. As I go along, I'll discuss my thought process in here. Feel free to jump in if there's something glaringly obvious that I'm missing.

What You'll Need:
  • Arduino board (I use an Uno)
  • LEDs, preferably high intensity
  • Assorted resistors
  • Wires, soldering iron, electronics tools, etc.

The first thing you might be asking is "What the hell's an Arduino?" An excellent question. Arduino is the programmable platform that has been made a big deal in the past few years in the hobbyist community. It's essentially a programmable circuit board with multiple outputs for lights, buzzers, sensors, whatever. There's a variety of versions, including ones you can build yourself, small versions for attaching to model airplanes or stitching into clothing, stuff like that. In a lot of ways, it bridges programming a computer and using an electronics kit. Electronics as its own field is interesting but very, very complicated. There's a lot of math, specialized skills to assemble it, that sort of thing. While I like it, there's a massive distance between "Wouldn't it be cool if" and final product. And since the logic part of the Arduino is programmable, you can upload new commands to the Arduino in seconds and fix mistakes.  Not always the case with electronics. Way more information on the Arduino concept is available here.

So here's what I needed the Arduino to do for me. I originally conceived of this project as an interactive "screamer" prop, triggered by breaking a laser beam aimed at a sensor to trigger a recordable sound module I bought at Radio Shack. That would output to computer speakers which had a microphone attached, which would translate the volume of the message into the brightness of the lights in the object (so the lights would automatically flash during the message). Then a countdown would start so that you can't just stand there and keep triggering the damn thing. Few things are as annoying as that screamer at the front door freaking out every few seconds at a party. So I made my own. But better.

I never knew what grades I was getting, but this had better been an A.
That's all a bit much for a Halloween prop, and we're just getting started. So let's start at the beginning. I want a convincing candle flicker, which I've found is best represented by three LEDs, one white and two yellow, all super bright, flickering independently of each other. I figured it out by watching a commercial LED candle flicker and comparing it to actual candles. Also, pulsing red glowing eyeballs, because that's creepy.

I usually write this out longhand on a dry erase board and start replacing the words in English with programming terms. So the first version I wrote reads: "Check and see if the flicker duration is up. If it is, randomly select a new brightness and duration for the flicker. Repeat this process for the other two LEDs. Also, the eyes fade up to full brightness and then back down."

This, when translated, starts to look like: "If [time's up], then [new brightness] and [new duration]." By the end, it reads as:

if (time>length)
   {brightness = random(32,255);
   length = time + random(flickerLow,flickerHigh);}


Similarly, the LED eyes should fade smoothly up and back down. Longhand, we get "Brightness should get higher, but when it reaches its maximum it should start counting back down and vice versa" Not gonna lie, totally based this one off the standard 'Fade' sketch that comes packaged with the Arduino program.

if (time>length4)
   {bright4 = bright4 + fadeAmount;
   length4 = time + fadeLength;}
     
if (bright4 <= 0 || bright4 >= 255)
   {fadeAmount = -fadeAmount;}


bright4 = constrain(bright4, 0, 255);

Translated into English, that reads "If the current time is higher than the defined amount since you last checked, change the brightness for the eyes by the defined amount. Then set the next time to check by adding a defined amount of time to the current clock. If the brightness of the eyes is equal to or greater than 0 or equal to or greater than the highest setting, change the fade amount to either positive or negative for next time. Make sure that the brightness of the eyes doesn't ever go below 0 or above the maximum." That guarantees that the brightness will decline if it's at max, or count up if the eyes are dark. The last line fixes a problem where the eye brightness would flicker if it tried to define a value above 255 or below zero, which the Arduino just starts over counting at the top or bottom, so it would go 252 to 254 to 256 (which it sees as 0...) and then down, so it would flicker offer for an instant. Same thing would happen at the bottom. Last statement stops that, and lets you use a fadeAmount that isn't 1 or 5 or any even factor in there.

The sketch I designed is available for download here. The conversation is about to go deeper down the technical rabbit hole. If you have no interest in running the program or following along, skip to the end, where there is more building shots and maybe a video.

All, right, it should just be us. Let me lay out some assumptions in the program:

First up, I use the Pulse Width Modulation pins on the Arduino. These pins can do the fading of the LED like an analog light bulb by rapidly flicking the LED on and off for fractions of a second. They're marked on the board with (in the Uno's case) a ~ sign. So next I define the flicker range as between 1/40 and 1/10 of a second. I found that letting the range be 0 to 100 ms let the LEDs flutter too erratically and it looked "digital". Last of my constants, I define the length of each step in the red LED fade process. A step of fadeAmount 1 every .01 seconds means I go from 0 to 255 in just over 2.5 seconds. Slow enough to be spooky and somewhat subtle. Play with fadeLength and fadeAmount to fine tune the speed of the fade.

Each pass through the loop writes the brightness of the LEDs to its corresponding pin. Then it checks each LED to make sure if the timeline for its flicker has passed. If so, it sets a new duration and brightness. If not, on to the next one. Once through the first three, we check in on the fade and if appropriate, set new variables for the next couple passes through. A program this small means that each pass is happening very, very fast. As such, we don't need to worry about latency or load-times for any of this. At least, not on this scale.

It seems complicated, but that's only because it's tiny.
The final hurdle is actually wiring it! Each output pin goes to its own track on a project board. I don't solder complete circuit boards for things like this, it just changes too quickly. Feel free to do so if you know you'll use it the same way each time. I've re-written mine a bunch, so there you go. The Arduino outputs 5V through each pin (as would be expected from a digital device) so make your LED calculations from there. For example, the bright yellow LEDs have a forward voltage of 2.2, so you need (5-2.2)V/20mA=140 Ohm resistor. All those specs can be found on your LED package. Next highest I had was a 150 Ohm resistor, so that's what got installed. The red eyes are in series (because that's easier to solder) so it comes out to (5-4.8)V/20mA=10 Ohm resistor. I may not install a resistor at that point and just let the eyeballs heat up a little. It's only on for a couple hours a year, so I'm not worried.

They know, and they are judging me for it.
TECHNICAL STUFF IS OVER!

Welcome back. Cut a hole the size of your lights into your ping pong balls and hot glue them into place. Solder all your wiring (because wirenuts don't really work this small) and give the whole set-up a good few runs before you cram it into your pumpkin. I drilled a small hole down on one side where I could feed all the wires through it and got it all hooked up. Straight out the back works, too. A small stand or box to contain all the pieces directly underneath could also work. You have options.

Tape the eye balls in place and test it all again. The hot spot of the LED looks good as "pupils", so adjust a little to make sure it isn't wall-eyed. Or is, if that's your thing. Once in place, drip a little glue along the bottom "eyelid" to secure it in place. Now your eyes can't roll up into your head unexpectedly. Set all the other components inside the pumpkin. Depending on how your pumpkin is carved, drape the LED structure with some torn up white plastic bag or tissue paper. It gives the light something to bounce off and conceals the electronic gubbins from view.

All of this could be simplified by hot gluing some bright white Christmas lights into the ping pong balls and coiling the rest in the bottom of the pumpkin (though they will heat that sucker up quickly), but then you don't get as much control or science. And who wants less of those two things in their life?


Ah, who am I kidding, you're here for the video. So here it is:

[Video won't embed. As soon as we're smart enough to figure it out, it'll go right here. Until then, go watch it!]

Any other Arduino projects you want us to tackle? Hate the amount of technical jargon? Love the amount of same? Let us know below!

No comments:

Post a Comment