---
description: Partner Work (2-3 students per group)
---

# Fireflies Assignment

Spend approximately 15 minutes on this site to understand how fireflies sync their internal clocks

{% embed url="https://ncase.me/fireflies/" %}
You should spend at least 15 minutes on this site testing and resetting the simulation
{% endembed %}

{% content-ref url="fireflies-assignment/native-american-spirit-animal" %}
[native-american-spirit-animal](fireflies-assignment/native-american-spirit-animal)
{% endcontent-ref %}

Now let's program the fireflies: We ant to create digital fireflies using multiple micro:bits (each microbit acts as a firefly). Each firefly will have its own internal clock or counter. Each partner needs to design a unique "flash" for their bug.

```
set clock = 0

```

Every time the clock strikes 15 we want the firefly to flash

```
forever
    if clock > 14
        flash firefly
        pause 200
        set clock to 0
    else
        pause 100
        set clock to clock + 1
```

Test this out and make sure your firefly flashes. Adjust the pauses and clock settings as you see fit to make a unique firefly

![](https://2507214486-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LpUPSf3tCxHePJxNjfg%2F-LpZ9SzTolMUxYuBnPiC%2F-LpZ9VzxR7b9dJy4xrYj%2Fdownload.jpg?alt=media\&token=f922b474-0ce4-4b78-9d86-06854cf088ad)

In real life fireflies adjust their timing when they are near another fire fly so we need to adjust our code to send and receive messages from nearby flies. When you flash your light you also need to send a signal over the radio. This will communicate to nearby flies that you are flashing

```
radio send number 0
```

When fireflies detect that there are other flashes nearby they actually increase the timing of their internal clocks, meaning they will flash faster than if they were alone.

```
on radio recevied number
    if clock < 15
        set clock to clock + 1
```

This will increment your clock if you are not already flashing

### TIPS: Check radio group settings&#x20;

Once you have installed your working program on all microbits, and each "firefly" has a unique flash - then test them in the hallway moving closer and farther apart to discover what is happening to the clocks.

This assignment and corresponding code are based on an activity found on [https://makecode.microbit.org/projects/fireflies](https://makecode.microbit.org/projects/fireflies) - originally accessed 9/22/2019
