Skip to the content.

Firefly icon

firefly MicroCode program

Fireflies in the wild synchronize their glowing although there is no leader to give the tempo. Go to http://ncase.me/fireflies/ and read about the fireflies synchronization phenomenon.

Just like fireflies in the wild, you can create program that synchronize blinking between any number of micro:bit! The firefly program uses two pages, page 1 will run when the light is off and page 2 will handle a glow. You will need 3 micro:bit to make the synchronization work!

page 1 page 1

firefly page 1

In page 1 page 1, we add a rule that clears the screen and keeps a dot screen when the page starts.

firefly page 1 rule 1

The clock of the firefly will be held in get variable X variable X. It starts at value 1 1 and once it reaches 8, the firefly will glow.

The radio receive radio receive moves the clock by 1 whenever a neighboring firefly sent a message.

when radio receive, do increment X by 1

The timer timer rule moves the clock every quarter of a second quarter of a second;

when timer quarter of second, do increment X by 1

The next rule, variable X changed when variable X changed to 8, transitions to page 2 page 2 when the clock reaches 8.

when variable X changed to 8, switch to page 2

Since it is possible that we miss the number 8 because the firefly received many radio updates at once, we add one last rule, a timer every 3s that transitions to page 2.

when timer 3s, do switch to page 2

page 2 page 2

firefly page 1

A radio message is sent to other firefly so that they can nudge their clock (radio send).

when page started, send radio message

The variable X is set variable X set to value 1 1.

when page started, set variable X to 1

All the LEDs are turned on.

when page started, do turn all LEDs on

A sound is played.

when page started, do play hello

After half a second, we transition back to page 1 to restart the non-glow phase.

when timer half a second, do switch to page 1

improvement ideas