added voting project

This commit is contained in:
Peli de Halleux 2017-06-15 15:03:40 -07:00
parent 677c8e181c
commit 7bb252b484
5 changed files with 93 additions and 8 deletions

View File

@ -15,9 +15,8 @@
* *
* If the radio packet is not received for 10sec, the LED starts blinking. * If the radio packet is not received for 10sec, the LED starts blinking.
*/ */
const deadPing = 20000;
const lostPing = 10000; const lostPing = 10000;
const group = 4;
interface Client { interface Client {
// client serial id // client serial id
@ -59,7 +58,7 @@ radio.onDataPacketReceived(packet => {
return; return;
client.ping = input.runningTime() 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 // monitor the sprites and start blinking when no packet is received
@ -67,15 +66,20 @@ basic.forever(() => {
const now = input.runningTime() const now = input.runningTime()
for (const client of clients) { for (const client of clients) {
// lost signal starts blinking // 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); client.sprite.setBlink(500);
else client.sprite.setBlink(0); else
client.sprite.setBlink(0);
} }
basic.pause(500) basic.pause(500)
}) })
// setup the radio and start! // setup the radio and start!
radio.setGroup(group) radio.setGroup(4)
radio.setTransmitPower(6) game.addScore(1)
radio.setTransmitSerialNumber(true)
``` ```

View File

@ -39,6 +39,10 @@ Fun games to build with your @boardname@.
"name": "Infection", "name": "Infection",
"url": "/projects/infection", "url": "/projects/infection",
"imageUrl": "/static/mb/projects/infection.png" "imageUrl": "/static/mb/projects/infection.png"
}, {
"name": "Voting Machine",
"url": "/projects/voting-machine",
"imageUrl": "/static/mb/projects/voting-machine.png"
}] }]
``` ```

View File

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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.3 MiB

After

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB