diff --git a/docs/examples/radio-dashboard.md b/docs/examples/radio-dashboard.md index 29228694..ae159d2d 100644 --- a/docs/examples/radio-dashboard.md +++ b/docs/examples/radio-dashboard.md @@ -15,9 +15,8 @@ * * If the radio packet is not received for 10sec, the LED starts blinking. */ - +const deadPing = 20000; const lostPing = 10000; -const group = 4; interface Client { // client serial id @@ -59,7 +58,7 @@ radio.onDataPacketReceived(packet => { return; client.ping = input.runningTime() - client.sprite.setBrightness(packet.receivedNumber & 0xff); + client.sprite.setBrightness(Math.max(1, packet.receivedNumber & 0xff)); }) // monitor the sprites and start blinking when no packet is received @@ -67,15 +66,20 @@ basic.forever(() => { const now = input.runningTime() for (const client of clients) { // lost signal starts blinking - if (now - client.ping > lostPing) + const lastPing = now - client.ping; + if (lastPing > deadPing) { + client.sprite.setBlink(0) + client.sprite.setBrightness(0) + } + else if (lastPing > lostPing) client.sprite.setBlink(500); - else client.sprite.setBlink(0); + else + client.sprite.setBlink(0); } basic.pause(500) }) // setup the radio and start! -radio.setGroup(group) -radio.setTransmitPower(6) -radio.setTransmitSerialNumber(true) +radio.setGroup(4) +game.addScore(1) ``` \ No newline at end of file diff --git a/docs/projects.md b/docs/projects.md index 2d43e702..d81a7549 100644 --- a/docs/projects.md +++ b/docs/projects.md @@ -39,6 +39,10 @@ Fun games to build with your @boardname@. "name": "Infection", "url": "/projects/infection", "imageUrl": "/static/mb/projects/infection.png" +}, { + "name": "Voting Machine", + "url": "/projects/voting-machine", + "imageUrl": "/static/mb/projects/voting-machine.png" }] ``` diff --git a/docs/projects/voting-machine.md b/docs/projects/voting-machine.md new file mode 100644 index 00000000..fdf8e786 --- /dev/null +++ b/docs/projects/voting-machine.md @@ -0,0 +1,77 @@ +# Voting Machine + +### ~ avatar + +Build a vote collection system using many micro:bit! + +### ~ + +https://youtu.be/77HOqf8BaNg + + +In this project, a **voter** program is uploaded on the player's @boardname@. The player uses the buttons to vote yes or no +and the vote sent to the **dashboard** @boardname@ via radio. The dashboard allocates one LED per player and turns it on for "yes" and off for "no". + +Both programs set their radio group to ``4`` to be able to communicate. + +## The voter program + +Assuming button ``A`` is no and ``B`` is yes, the voter program works as follows: + +* when button ``A`` is pressed, a number ``0`` is sent via radio and the ``X`` symbol is shown on the screen. + +```block +input.onButtonPressed(Button.A, () => { + radio.sendNumber(0) + basic.showIcon(IconNames.No) +}) +``` + +* when button ``B`` is pressed, a number ``255`` is sent via radio and the ``Y`` symbol is shown on the screen. + +```block +input.onButtonPressed(Button.B, () => { + radio.sendNumber(255) + basic.showIcon(IconNames.Yes) +}) +``` + +* In order to track the votes, we tell the radio to also transmit the device serial number. + +```block +radio.setTransmitSerialNumber(true) +``` + +* we arbritrarily choose ``4`` as the group used for the communications + +```block +radio.setGroup(4) +``` + +Combined together, the voter program looks as follows: + +```blocks +input.onButtonPressed(Button.A, () => { + radio.sendNumber(0) + basic.showIcon(IconNames.No) +}) +input.onButtonPressed(Button.B, () => { + radio.sendNumber(255) + basic.showIcon(IconNames.Yes) +}) +radio.setGroup(4) +radio.setTransmitSerialNumber(true) +basic.showIcon(IconNames.Ghost) +``` + +## The dashboard + +The dashboard code can be found at [/examples/radio-dashboard](/examples/radio-dashboard). + +Download the code from that example into the @boardname@ that will be used to display the result. + +When the dashboard receives a message from a @boardname@, it find a pixel for that board (and remembers it) +and uses the number received as the brightness of the LED. + +When a message hasn't been received by a board for some time, it's pixel will start to blink. After more time, it will simply turn off. + diff --git a/docs/static/mb/projects/railway-crossing.png b/docs/static/mb/projects/railway-crossing.png index d4b44571..f5621352 100644 Binary files a/docs/static/mb/projects/railway-crossing.png and b/docs/static/mb/projects/railway-crossing.png differ diff --git a/docs/static/mb/projects/voting-machine.png b/docs/static/mb/projects/voting-machine.png new file mode 100644 index 00000000..c35aa054 Binary files /dev/null and b/docs/static/mb/projects/voting-machine.png differ