Maker.io main logo

Fireflies with MakeCode

2017-10-30 | By Adafruit Industries

License: See Original Project

Courtesy of Adafruit

Guide by Peli de Halleux

Fireflies in nature

 

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 code editor. Let's get started!

Circuit Playground Express

The Clock

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

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.

and every time the clock "strikes twelve", it flashes.

We can use a 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.

Microsoft MakeCode Code Editor 1

Copy Code
let clock = 0
loops.forever(function () {
if (clock >= 8) {
light.pixels.setAll(Colors.White)
loops.pause(200)
light.pixels.clear()
clock = 0
} else {
loops.pause(100)
clock += 1
}
})

 

The Nudge

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

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 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)!

Microsoft MakeCode Code Editor 2

Copy Code
let clock = 0
loops.forever(function () {
if (clock >= 8) {
light.pixels.setAll(Colors.White)
loops.pause(200)
light.pixels.clear()
clock = 0
} else {
loops.pause(100)
clock += 1
}
})
input.onLightConditionChanged(LightCondition.Bright, function () {
if (clock < 8) {
music.playSound("g5:1 g5:1")
clock += 1
}
})

 

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.

Microsoft MakeCode Code Editor 3

Copy Code
let clock = 0
loops.forever(function () {
if (clock >= 8) {
light.pixels.setAll(Colors.White)
loops.pause(200)
light.pixels.clear()
clock = 0
} else {
loops.pause(100)
clock += 1
}
})
input.onLightConditionChanged(LightCondition.Bright, function () {
if (clock < 8) {
music.playSound("g5:1 g5:1")
clock += 1
}
})
input.setLightThreshold(LightCondition.Bright, 40)
light.pixels.setBrightness(255)

 

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...

 

制造商零件编号 3333
CIRCUIT PLAYGROUND EXPRESS
Adafruit Industries LLC
制造商零件编号 3517
CIRCUIT PLAYGRND EXPRSS BASE KIT
Adafruit Industries LLC
Add all DigiKey Parts to Cart
TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum