When I ran across the Jar of Fireflies Instructable sometime this past summer, I knew I had to build it.  Full credit to Keso for an excellent set of instructions!  I wouldn't have gotten to the point of developing the firefly behavior without such a solid base for building the hardware, as well as firmware to start it out with.  If you have already built a firefly jar according to those plans, it should be straightforward to reprogram it with my firmware to change its behavior.

I wanted to go a step farther when I built the firefly jar as a Christmas present.  My goal was to have multiple fireflies interacting, including males that sometimes synchronize with each other, and females that respond to the flashes of males. I also wanted to base it on the behavior of our backyard firefly, Photinus pyralis , the common Eastern firefly, which is also one of the best studied.  I wrote my own software for the microcontroller to simulate this behavior.  The full project is documented here , but this Instructable should be enough to follow.

The latest version of the firmware is available on GitHub .

You will need two things to use this software:
Both of these are outside the scope of this Instructable.  For the first, please refer to the detailed instructions at the original Jar of Fireflies .  If you need help with programming the AVR, a good place to start is the AVR tutorial from Adafruit .

I tried making a video to demonstrate the flashing behavior.  The quality isn't great (it's from an iPhone, and I reduced the quality to upload it):

Step 1: Hardware

The only changes I made to the original hardware were the addition of a push on/push off switch (scavenged from a stick-on LED light) and a 6-pin programming header to allow reprogramming of the jar after it had been built. To make testing and debugging easier, I also built a prototype board on a 2"x2" perfboard with the same schematic, so it is electrically identical but easier to see what it's doing, and less fragile.  I've added a view of the point-to-point soldering on the back of the prototype board in case that is helpful to anyone.  The prototype board can also be used as a target board for programming ATtiny85 (and -45) chips for other purposes.  In the real jar, the switch and header are fixed to the underside of the lid with J-B Qwik to keep them well attached.

Step 2: Software

After I built the firefly jar, I had planned to modify the software to change its behavior somewhat.  I was completely new to AVR programming, and I realized that I didn't understand much of what was going on in the original code, so I decided to write my own from the ground up.  It uses the AVR's internal timers for controlling the timing and brightness of the LEDs.

The simulated fireflies exhibit the following behavior, based on observations of the species Photinus pyralis , the common Eastern firefly (more details and references ):
  • Male fireflies will appear one at a time, flashing at a regular interval, 5.9 ± 0.6 seconds, and gradually synchronize with each other.
  • If all males are in sync, a female may respond to a male's flash after a delay of 2.1 ± 0.2 seconds.  The appearance of another male out of sync will cause the female to stop responding to the first one.  The female's flash is 1/10 the intensity of the males'.
  • Each pulse is 0.5 second in duration with a steep increase and slower falloff, which I modeled as y=x*e-x2
  • Females are stationary, they will flash at the same LED each time, while males can "move" around, flashing at randomly selected LEDs.
  • One reaching the end state of 6 males flashing and one female responding, it will continue that pattern until it is reset at a random interval, about 5-20 minutes.
The code was compiled with avr-gcc 4.2.0.  I used an ATtiny85, and it also works with an ATtiny45 on my prototype board.  There are also two shorter example programs included that can be used as a base for different behaviors.  All source code is freely available under the GPL .

Download the source code and compiled .hex files here .

<p>I've had so much fun making these (even did some for a wedding), so thanks again for this! Seriously, thank you.</p><p>Have you ever considered adding a Piezo or something and have the fireflies get angry if you tap the jar? Like they go into a special sequence where they all randomly flash and have fast, agitated pulses. That would just be fun. If anybody tries this, please report back with your results.</p>
<p>I wish I understood what all this means I would love to make this </p>
<p>Never mind the part about the boot sequence not working. My chip had just failed to overwrite and still had it disabled.</p>
<p>The boot sequence has never worked for me. It's just ignored. </p><p>Also, I think it would be neat to have the boot sequence actually run the entire cycle once with the settings adjusted faster. Like a quick preview. Then go into the real thing. Anybody know how to do this? I mean, I know how to make it go faster, but not how to make it do that once and then start and loop the real thing only. I'm using ATtiny85, so there should be enough space to repeat the entire code, right?</p>
<p>I want the fireflies to stay on a little bit longer, but when I change PULSELEN from 20 to 40, I get LEDs which get stuck steady on for over a minute every now and then. I think this is happening when a new cycle starts. Can anybody help?</p>
<p>It is rather hard wired in the C code, I'm afraid. You're overrunning the end of the pulse arrays. You'll also have to change the length of the arrays mpulse and fpulse to be length 40 to match (and pad them out to stay on longer in the meantime). You always want the last value of each array to be 0 to keep the LED off between pulses. Otherwise you can make the pulse look like anything you want.</p>
OMG, I actually understood that! And it works! THANK YOU! :)<br><br>Now they are all off between cycles. However, there's a full minute where nothing happens before they start up again. Is there an easy change to make them begin again sooner?
A full minute between flashes? Or after a full cycle of building up to all 6 LEDs flashing? Either way, it shouldn't be that long. There are some other timing variables set up in the init() function you could play with.
Yes, it's after all six flash together and before the single flashing starts again. This was with no changes to the code. I thought it had gone into sleep mode early, but then it started again. I timed it the next time and it was just over a minute where absolutely nothing lit. <br><br>Unrelated, is there a proper way to disable sleep mode? I can set it to a negative number and tell it the count has to be less than that, and that works, but is there a better way?<br><br>Also, is there only ever one female? Can I make it randomly have more?<br><br>And is there a way to make it randomly select between several different mpulse and fpulse choices?<br><br>Anyway, thanks so much for your help. I actually have it working in a way that I really like. I'm just curious about how much I can tweak it.
<p>On line 170, it sets nmales=0 after each full cycle, so it will appear all off until the first male randomly turns up. If you make that 1, it will always have at least 1 going.</p><p>Your method works fine to disable sleep, you could also just take out the &quot;if(bigcount&gt;=SLEEPAFTER)&quot; statement entirely in the main loop to eliminate it.</p><p>Adding more females would take some larger changes in the logic. It's certainly possible, but that's up to you.</p><p>Choosing different pulses is certainly possible as well, you'd have to decide when it's going to choose which pulse. At the beginning (or each time through the full cycle) would be fairly straightforward.</p><p>Glad you enjoy it!</p>
Mine works! Thanks for sharing! <br>
Thanks, I preferred this pattern over the original
On your prototype board do you have a socket that the ATtiny85 chip is sitting in? If it is i cant seem to find one anywhere. Do you happen to have a part number if that is what you are using. :)
Sorry it took me so long to reply. I think I used Digi-Key part no. 3M5461-ND.
I can't seem to get the firefly software unzipped. could anyone send me a makefile or an unzipped set. Thanks ~Corry
Sorry you had trouble with the zipped file. I have put the firmware up at <a href="http://github.com/kjordahl/Pyralis">github</a>, where it should be easier to browse and get in a format you can use.
I just got all the parts today, I have a question on your picture with all the LEDs on the board in step two you have five resistors on the board but in the schematic you show 3? maybe the other resistors are for the batterys?
The schematic is correct.&nbsp; On the prototype board, there are two pairs of 20 &Omega; resistors (each pair connected in series), to make 40 &Omega; each, which matched better with the LEDs I&nbsp;used on that on that board than the 100 &Omega; used on the surface mount board.<br /> <br /> You may want to adjust the values of those resistors depending on the exact LEDs you use.<br /> <br /> <br />
The code (firefly.hex) from the original Keso Firefly jar works with this schematic and an ATTiny45, but I get no response using pyralis.hex. &nbsp; &nbsp;This is my first effort building, using, coding AVR, etc, so I may be missing something crucial. &nbsp;I'll experiment and let you know if I find a solution.
It seems to be the case that the current version of pyralis.hex I&nbsp;have posted does not work on an ATtiny45.&nbsp; I'm not sure why that is right now, previous versions worked fine for me.&nbsp; I&nbsp;will look into it when I&nbsp;get a chance.<br /> <br /> Sorry about that.&nbsp; You might try compiling it yourself, changing the Makefile appropriately (set DEVICE = attiny45).<br /> <br /> I've changed the text above to reflect that for now.<br />
I think I&nbsp;understand the issue.&nbsp; It seems that a .hex file compiled for ATtiny45 will work fine on an ATtiny85, but not vice versa (even if it is &lt;4k).&nbsp; I had gotten complacent when I&nbsp;found I&nbsp;could compile for either, but haddn't realized that the .hex files aren't completely interchangeable.&nbsp; I&nbsp;have changed the Makefile as I&nbsp;suggested above and recompiled, and it works for me now on both the ATtiny45 and the ATtiny85.<br /> <br /> I've updated the link <a href="http://mysite.verizon.net/kajordahl/ffsoftware.html" rel="nofollow">on my website</a> so that the pyralis.hex file there should work on both now, but you can always recompile it for your own hardware if that doesn't work for you.<br /> <br />
&nbsp;ThX! &nbsp;Like you, I am starting from the ground up so I can actually understand what I am writing/programming, but I REALLY wanted to compare/contrast the behaviour of your fireflies vs the original Keso code. &nbsp;I didn't have any green SMT LED around, so I used red SMT LEDs and made a jar of evil red blinky flies.
&nbsp;It worked! &nbsp;Thanks so much!

About This Instructable




More by kjordahl:New Jar of Fireflies 
Add instructable to: