Several friends recently planned a party for maker, e-textiler and all-around awesome person Liza Stark — and I wanted to celebrate her with blinkies appropriate for the occasion.
The event was to take place in a rented house with a porch overlooking a slice of woods.
I decided to fill the trees with digital fireflies.
Over the course of the evening, I wanted the forest to fill with blinks that mimicked fireflies' occasional green flashes — at least initially. Then I wanted them to gradually change their patterns, blinking as fireflies but blue and then an unblinking collection of blues and then touch of orange and so on.
And this would happen over several hours, from 8 p.m. until after 1 a.m.
In my sketchbook I visualized the evening like this:
Liza was the one who first introduced me to the ATtiny85 — an 8-legged, inexpensive digital critter that works like a mini Arduino. This would be each firefly's brain.
For the light, an RGB (red-green-blue) LED would provide the colors I needed. And I'd power it all from a watch battery.
Here's the circuit, which attaches three different legs of the ATtiny to the R, G and B legs of the LED.
I wanted at least 50 fireflies in the woods — so I drew the circuit in Eagle, which is like an Adobe Illustrator for circuit boards.
Sparkfun has a fantastic step-by-step guide to drawing schematics and circuit boards and even ordering your boards. It takes some getting used to, and I highly recommend using a multi-button mouse or trackball.
Below is a picture of the board drawing, and here are the Eagle files themselves.
In just a few days, I had a stack of boards and all the parts.
I put on some great podcasts and started soldering away — assembling 50 fireflies in an evening.
I wanted the code to accomplish three things:
- Slowly cycle through the different patterns.
- Keep the fireflies somewhat synchronized — bit not precisely so. I wanted the transitions between patterns to happen gradually.
- Conserve the batteries so the fireflies worked into the night.
To save battery power, I learned from insideGadgets how to put the ATtiny to "sleep," and tried to use that whenever the LED wasn't on, such as those long pauses between firefly flashes.
There's a hitch, though. The ATtiny's internal counter — usually checked with the mills() function — doesn't count when the ATtiny is sleeping. So I made my own counter, which I called
frame. I decided every frame would be 10 seconds. So no matter what the ATtiny was doing I'd tick the
frame variable up by 1 after 10 seconds of blinking, waiting or sleeping. So six frames in a minute and 360 frames in an hour.
void loop() section handles the patterns by calling different display function depending on the frame count.
As for the pseudo-synchrony ... the whole program starts when the battery is inserted into the firefly, and the physical process of inserting batteries into 50 fireflies ensured they wouldn't be perfectly synchronized! Plus, the internal clocks of each chip aren't perfectly tuned.
So here's the code I wrote for the ATtiny, with comments to help guide you. If you want to try it yourself, be sure to copy-paste the raw version. And High-Low Tech has a great guide to programming the ATtiny.