# Fireflies with MakeCode

## Fireflies in nature

https://youtu.be/ZGvtnE1Wy6U

&gt; A whole mangrove forest, lighting up all at once, plunging into darkness, then lighting up all again – in near-perfect synchrony. How do thousands of fireflies coordinate with each other? Who is the conductor of this silent symphony?

The fireflies phenomenom is astonishing. The web page http://ncase.me/fireflies/ provides an **amazing** explanation of this history and mechanics around it.

Let's create virtual fireflies using multiple Circuit Playground! The NeoPixel ring provides the flash, the light sensor can detect it and we can tie it all up with a bit of code in the [Microsoft MakeCode](https://learn.adafruit.com/makecode) code editor. Let's get started!

### Circuit Playground Express

[Circuit Playground Express](https://www.adafruit.com/product/3333)
 **Circuit Playground Express** is the next step towards a perfect introduction to electronics and programming. We've taken the original Circuit Playground Classic and made it even better! Not only did we pack even more sensors in, we also made it even easier to...

Out of Stock
[Buy Now](https://www.adafruit.com/product/3333)
[Related Guides to the Product](https://learn.adafruit.com/products/3333/guides)
![A Black woman's manicured hand holds a round microcontroller with lit up LEDs.](https://cdn-shop.adafruit.com/640x480/3333-05.jpg)

# Fireflies with MakeCode

## The Clock

As mentionned in the article, each fireflies has a clock that drives when they decide to flash.

&gt; each firefly has its own individual internal clock

A clock in this case is like a counter, so we will start by adding a ``clock`` variable to our program.

&gt; and every time the clock “strikes twelve”, it flashes.

We can use a [forever](/reference/basic/forever) loop to repeat code that increments the clock.
When the clock reaches "noon" (let's pick `8`), we turn on the NeoPixels briefly and turn them off to simulate a flash.

https://makecode.com/_1RK8wyirv9Cw

# Fireflies with MakeCode

## The Nudge

Another key observation from the article is the clock adjustment process:

&gt; when you see a nearby firefly flash, nudge your clock a little bit forward.

We can use light sensor on the Circuit Playground Express to detect a nearby flash. 

We can use the [on light bright](https://makecode.adafruit.com/reference/input/on-light-condition-changed) event to add code that will run when a bright light is detected. When this happens, we bump the clock and play a sound... unless we are also flashing (and clock is greater or equal to 8)!

https://makecode.com/_JTHEuX4ELFhz

# Fireflies with MakeCode

## Calibration

Based on your light condition, the threshold predefined for **on light bright** might not be good enough. In such case, we can use **set light threshold** to tune this value. You can make it a trial and error approach (using a binary search) or use serial to log values and do a deeper analysis (We'll dig deeper into logging and analysing values in another guide). In this case, we've just approximated the best value for the particular setting.

We also beef up the brightness of the LEDs to the maximum (255) to get the brightess flash possible.

https://makecode.com/_ExoDbhMH9AR0

# Fireflies with MakeCode

## Testing

It's time to test it:

* Gather your Circuit Playground Express boards (you'll want at least 3),
* Flash the fireflies code on each of them
* Find a dark place
* Turn them on and wait.

At first, you will hear sounds triggering at various moments but they will start to group up. When all board will sync, you should not hear any sound anymore...

https://youtu.be/vSvSM3Kol6M


## Featured Products

### Circuit Playground Express

[Circuit Playground Express](https://www.adafruit.com/product/3333)
 **Circuit Playground Express** is the next step towards a perfect introduction to electronics and programming. We've taken the original Circuit Playground Classic and made it even better! Not only did we pack even more sensors in, we also made it even easier to...

Out of Stock
[Buy Now](https://www.adafruit.com/product/3333)
[Related Guides to the Product](https://learn.adafruit.com/products/3333/guides)
### Circuit Playground Express - Base Kit

[Circuit Playground Express - Base Kit](https://www.adafruit.com/product/3517)
It's the **Circuit Playground Express Base Kit!** &nbsp;It provides&nbsp;the few things you'll need to get started with the new [Circuit Playground Express](https://www.adafruit.com/product/3333).&nbsp;This version of Circuit Playground is super powered, and will...

Out of Stock
[Buy Now](https://www.adafruit.com/product/3517)
[Related Guides to the Product](https://learn.adafruit.com/products/3517/guides)
### Circuit Playground Express - Black Girls CODE

[Circuit Playground Express - Black Girls CODE](https://www.adafruit.com/product/2599)
 **Circuit Playground Express** is the next step towards a perfect introduction to electronics and programming. We've taken the original Circuit Playground Classic and made it even better! Not only did we pack even more sensors in, we also made it even easier to...

Out of Stock
[Buy Now](https://www.adafruit.com/product/2599)
[Related Guides to the Product](https://learn.adafruit.com/products/2599/guides)

## Related Guides

- [Adafruit Circuit Playground Express](https://learn.adafruit.com/adafruit-circuit-playground-express.md)
- [Dance-Reactive Tutu Sparkle Skirt](https://learn.adafruit.com/dance-reactive-tutu-sparkle-skirt.md)
- [Stumble-Bot](https://learn.adafruit.com/stumble-bot-with-circuit-playground-and-crickit.md)
- [Mini Golf Course with Circuit Playground and Crickit](https://learn.adafruit.com/mini-golf-course-with-makecode-circuit-playground-crickit.md)
- [Make It Sense](https://learn.adafruit.com/make-it-sense.md)
- [Crickit Powered Dancin' Snowman!](https://learn.adafruit.com/crickit-powered-dancin-snowman.md)
- [Halloween Sentry-Bot with CRICKIT for CPX](https://learn.adafruit.com/halloween-sentry-bot.md)
- [Wind Blowing Emoji Prop](https://learn.adafruit.com/wind-face-emoji.md)
- [Drama Piñata](https://learn.adafruit.com/customizable-reusable-pinata.md)
- [Epoxy Resin Torch Lamp with Touch Control & 3D Printed Base](https://learn.adafruit.com/epoxy-resin-torch-lamp-with-touch-control-3d-printed-base.md)
- [Circuit Playground Seashell Pendant](https://learn.adafruit.com/circuit-playground-seashell-pendant.md)
- [CircuitPython Sound Box](https://learn.adafruit.com/circuitpython-sound-box.md)
- [CPX Glowing Disembodied Hand](https://learn.adafruit.com/cpx-glowing-disembodied-hand.md)
- [Circuit Playground Express Treasure Hunt](https://learn.adafruit.com/circuit-playground-treasure-hunt.md)
- [Con Badge with Circuit Playground Express](https://learn.adafruit.com/circuit-playground-express-con-badge.md)
- [Combo Dial Safe with Circuit Playground Express](https://learn.adafruit.com/combo-dial-safe-with-circuit-playground-express.md)
