pxt-calliope/docs/projects/fireflies.md

138 lines
3.2 KiB
Markdown
Raw Normal View History

2017-06-04 19:42:52 +02:00
# Fireflies
## ~ avatar
2017-06-04 19:42:52 +02:00
Turn your @boardname@ into fireflies.
## ~
2017-06-04 19:42:52 +02:00
https://youtu.be/ZGvtnE1Wy6U
2017-12-07 05:23:17 +01:00
## How do Fireflies synchronize?
2017-06-04 19:42:52 +02:00
Go to http://ncase.me/fireflies/ and read about the fireflies synchronization phenomenon.
2017-12-07 05:23:17 +01:00
## Code the firefly
2017-06-04 19:42:52 +02:00
2017-12-07 05:23:17 +01:00
We want to create virtual fireflies using multiple @boardname@s (each @boardname@ acts as a firefly).
Let's review some of the key points from the article:
2017-06-04 19:42:52 +02:00
2017-12-07 05:23:17 +01:00
### "Each firefly has its own individual internal clock..."
2017-06-04 19:42:52 +02:00
A clock in this case is like a counter, so we will start by adding a ``clock`` variable to our program.
```block
// the clock ticker
let clock = 1
```
2017-12-07 05:23:17 +01:00
### "...and every time the clock 'strikes twelve', it flashes."
2017-06-04 19:42:52 +02:00
2017-12-07 05:23:17 +01:00
We can use a ``||basic:forever||`` loop to repeat code that increments the clock.
When the clock reaches "noon" (let's pick `8` as noon), we turn on the screen briefly (by using the game score animation)
2017-06-04 19:42:52 +02:00
```block
// the clock ticker
let clock = 0
basic.forever(() => {
// if clock "hits noon", flash the screen
if (clock >= 8) {
// flash
game.addScore(1)
2017-06-05 07:34:28 +02:00
// wait for 2 ticks
basic.pause(200)
2017-06-04 19:42:52 +02:00
// reset the clock
clock = 0
} else {
// just wait a bit
basic.pause(100)
// increment the clock
clock += 1
}
})
```
2017-12-07 05:23:17 +01:00
### When you see a nearby firefly flash, nudge your clock a little bit forward
2017-06-04 19:42:52 +02:00
2017-12-07 05:23:17 +01:00
The @boardname@ can send radio messages to a neighbor @boardname@. We can use these messages to simulate the "flashes" of light.
2017-06-04 19:42:52 +02:00
2017-12-07 05:23:17 +01:00
When a firefly flashes, it also sends a number over radio using ``||radio:radio send number||``:
2017-06-05 07:34:28 +02:00
```block
// the clock ticker
let clock = 0
basic.forever(() => {
// if clock "hits noon", flash the screen
if (clock >= 8) {
// notify neighbors
radio.sendNumber(0)
// flash
game.addScore(1)
// wait for 2 ticks
basic.pause(200)
// reset the clock
clock = 0
} else {
// just wait a bit
basic.pause(100)
// increment the clock
clock += 1
}
})
```
Cherry picked fixes from v0 to v1 (#1476) * Fix example and do a few edits (#850) * Small fixes to the Karel project text (#862) * fix missing shadow type * add calibration info (#897) microbit-support:6348 User reported bug, but didn't realie micro:bit compass had to be calibrated on first run of the program. * Cherry pick adding various cross-editor compat APIs (#863) * fix nudge * Fix a typo ("screent") in the documentation (#1012) * Fix a typo "screent" -> "the LED screen" * Update showArrow doc string for consistency with other methods * Doc bugs found by xlators (#899) * Update rotation.md (#998) reflects actual behaviour of the board * fixed layout corruption (#1073) * Updating the new bug filer with the right link * Update challenges.md (#1325) For what I'm taught in school, 2 follows 1 :wink::wink:. * Update simulator.md (#1209) * Update plant watering.md (#1264) the connection to servo are male not female * support for MIDI simulator in v0 (#1331) * Doc improvements (#1337) * update grammar in radio-dashboard sample code * Chanllenge 2 follows 1 I open all challenges.md without "Challenge 2" and review the file `grep -r "Challenge 2" -L | grep challenge | xargs -I@ code @` Changes to be committed: modified: docs/lessons/counter/challenges.md modified: docs/lessons/game-counter/challenges.md modified: docs/lessons/night-light/challenges.md modified: docs/lessons/snowflake-fall/challenges.md * Revert "support for MIDI simulator in v0 (#1331)" This reverts commit 30a9c411fb80762656e7a636feff8e77b7fd9e67. * Revert "Cherry pick adding various cross-editor compat APIs (#863)" This reverts commit 7308dbef23e9ee402bebb7e721d7014d8252c9e5. * Fixed pin needed to replace button A (#1385) * Cherry pick part of the signature update in in2cWriteBuffer (#863): 7308dbe
2018-10-22 19:00:57 +02:00
When a firefly receives a radio packet
**and** it is not sending packet
, it increments its clock by one:
2017-06-04 19:42:52 +02:00
```block
// the clock ticker
let clock = 0
radio.onReceivedNumber(function (receivedNumber) {
2017-06-04 19:42:52 +02:00
// advance clock to catch up neighbors
Cherry picked fixes from v0 to v1 (#1476) * Fix example and do a few edits (#850) * Small fixes to the Karel project text (#862) * fix missing shadow type * add calibration info (#897) microbit-support:6348 User reported bug, but didn't realie micro:bit compass had to be calibrated on first run of the program. * Cherry pick adding various cross-editor compat APIs (#863) * fix nudge * Fix a typo ("screent") in the documentation (#1012) * Fix a typo "screent" -> "the LED screen" * Update showArrow doc string for consistency with other methods * Doc bugs found by xlators (#899) * Update rotation.md (#998) reflects actual behaviour of the board * fixed layout corruption (#1073) * Updating the new bug filer with the right link * Update challenges.md (#1325) For what I'm taught in school, 2 follows 1 :wink::wink:. * Update simulator.md (#1209) * Update plant watering.md (#1264) the connection to servo are male not female * support for MIDI simulator in v0 (#1331) * Doc improvements (#1337) * update grammar in radio-dashboard sample code * Chanllenge 2 follows 1 I open all challenges.md without "Challenge 2" and review the file `grep -r "Challenge 2" -L | grep challenge | xargs -I@ code @` Changes to be committed: modified: docs/lessons/counter/challenges.md modified: docs/lessons/game-counter/challenges.md modified: docs/lessons/night-light/challenges.md modified: docs/lessons/snowflake-fall/challenges.md * Revert "support for MIDI simulator in v0 (#1331)" This reverts commit 30a9c411fb80762656e7a636feff8e77b7fd9e67. * Revert "Cherry pick adding various cross-editor compat APIs (#863)" This reverts commit 7308dbef23e9ee402bebb7e721d7014d8252c9e5. * Fixed pin needed to replace button A (#1385) * Cherry pick part of the signature update in in2cWriteBuffer (#863): 7308dbe
2018-10-22 19:00:57 +02:00
if (clock < 8) {
clock += 1
}
2017-06-04 19:42:52 +02:00
})
```
2017-12-07 05:23:17 +01:00
## Put it all together
2017-06-04 19:42:52 +02:00
https://youtu.be/XzZeB4yYnEw
2017-12-07 05:23:17 +01:00
Download this program on as many @boardname@s as you can find and try it out in the dark!
2017-06-04 19:42:52 +02:00
2017-12-07 05:23:17 +01:00
**Note:** We've added a ``||radio:radio set group||`` block to specify which group the firefly will communicate on.
2017-06-04 19:42:52 +02:00
```blocks
// the clock ticker
let clock = 0
radio.onReceivedNumber(function (receivedNumber) {
2017-06-04 19:42:52 +02:00
// advance clock to catch up neighbors
clock += 1
})
basic.forever(() => {
// if clock hits noon, flash the screen
if (clock >= 8) {
// notify neighbors
2017-06-05 07:34:28 +02:00
radio.sendNumber(0)
2017-06-04 19:42:52 +02:00
// flash
game.addScore(1)
2017-06-05 07:34:28 +02:00
// wait for 2 ticks
basic.pause(200)
2017-06-04 19:42:52 +02:00
// reset the clock
clock = 0
} else {
// just wait a bit
basic.pause(100)
// increment the clock
clock += 1
}
})
radio.setTransmitPower(1)
radio.setGroup(12)
2017-07-19 02:22:29 +02:00
```
```package
radio
```