Here’s a simple Arduino project to recreate the digital firefly jars you might have seen around the last few years. From Frantone:
This was a simple little project that was really just an excuse for me to learn Arduino. I bought some really attractive Ball canning jars at the market and thought that it would be really cool to use them to make some kind of bedside firefly jar. I thought that I would use an array of RC transistor timers to drive some vintage 70’s amber lens LEDs I had in stock. But I wanted these virtual fireflies to act like the real thing, and I soon realized that there would have to be 4 separate operations to depict a realistic firefly blink, with a quick fade-on period, a brief stay-on period, a slower fade-off period, and a long off-rest period. Visualizing the large array of discrete RC timers I would need to create several fireflies was going to be prohibitively complex to shove into a jar lid, so I thought that this would be a really good job for a microcontroller.
Proto-typin’!
I got an Arduino UNO R3 board from Adafruit Industries that acts as a USB interface to an ATmega328 microcontroller, and I wrote the code on my PC using the Arduino open-source software.6 of the 14 ATmega328 I/O pins provide 8-bit pulse width modulated (PWM) output with the ‘analogWrite()’ function and I wrote the code to create the illusion of 6 firefly tails, giving each of them their own ‘personality’ through independently varying rates and fluctuations, so that the sequence would be ever changing. I also wanted my fireflies to respond to the environment, because as we all know the real things would never come out to do their dance before twilight – so I added a photoreactive circuit using a common photo resistor and a 2N3904 transistor to turn on the LEDs only when the ambient light was suitably dimmed.
This was going to end up being a self-contained autonomous display, so I would prototype it on the UNO board to prove the concept, then I would remove the programmed Atmega328 microcontroller chip and transplant it to my own smaller battery powered board that would contain the completed circuit, which I would build into the bottom of the jar lid.
I wrote this code as a single continuous loop program with 24 subroutines to independently control the four states (fading on, staying on, fading off, and staying off) of each of the 6 output pins (ledPin function) to drive each LED, and global variables had to be set to keep track of what state each firefly was in and at what point in that state, so that the results could be written to the pin outputs and those variables could also be updated with each cycle of the loop, which constantly changes the write values, creating the illusion of multitasking six independent routines with a single processor.
It is a very simple program and it works satisfyingly well – more satisfying was the fact that I was able to finish this code and have it debugged within just a day or two of beginning to read up on what the heck Arduino was. It is quite literally that easy to work with. If you have any coding experience at all in BASIC or C then you will have no problem transposing your coding chops to Arduino. It’s awesome!