About this project
In this project we'll use micro:bits to make a voting system. Have you ever watched a game show on TV where you "ask the audience" for their answer? We can make the same with micro:bits. To do this project you should have some experience with basic micro:bit programming and using the radio.
Let's start with a design:
- Every voter will have their own micro:bit.
- One person will start the vote by saying what the vote is for and pressing A+B.
- There will be 10 seconds to vote and this will be shown as a countdown on the LEDs on the micro:bit who called the vote.
- Each person will be prompted to vote with a question mark on their LEDs. They can vote yes by pressing A and no by pressing B.
- Once selected your vote is final!
- When the 10 seconds is up the result will be shown on the LEDs on the micro:bit who called the vote.
- The result is either yes (more yes votes than no votes), no (more no votes than yes votes) or a draw (the same of both).
We'll need a number of variables to implement this:
- 'state' - this will record what state the micro:bit is in. It can be one of:
- 'idle' - the micro:bit is not doing anything.
- 'calling_vote' - the micro:bit has called a vote and is waiting for results.
- 'voting' - the micro:bit is a voter and is waiting for the user to choose yes or no
- 'yes_count' - the number of yes votes received.
- 'no_count' - the number of no votes received.
- 'countdown' - the number of seconds remaining until voting is complete.
We'll use the following strings to send as radio messages:
- 'vote' - Sent from the micro:bit that is calling the vote. Causes the other micro:bits to vote.
- 'yes' - Sent from the voting micro:bits. The user voted yes.
- 'no' - Sent from the voting micro:bits. The user voted no.
Make sense? Let's code!
What you'll need
3 x BBC micro:bit (or more if you have them)
1 x Micro USB
1 x Computer or tablet
6 x AAA Batteries & 3 x Battery Holders (optional)
Project walk through
- We need to start by choosing a radio group. Choose a group number that no-one else in the room is using, in this example we've chosen group 1.
- Next we need to create a variable called 'state'. This will track what state the micro:bit is in. We start in 'idle'.
- To start a vote we need to change to the 'calling_vote' state when A+B is pressed. We need two make two variables to store the number of yes and no votes received. We also need to tell the other micro:bits to send in their votes. We do this by sending a radio message to them - 'vote'.
- To give everyone some time we'll wait 10 seconds for the votes to come in. This is done by making a new variable called 'countdown' and decreasing it every second until it gets to zero. We'll display this on the LEDs so you can tell everyone to hurry up and vote!
- After 10 seconds times up! Let's look at the votes we received and show the result! Either the result is the yes voters win, the no voters win or it's a draw. Once shown let's go back to the 'idle' state for the next vote.
- Now for the radio receiver. If we receive the 'vote' message and we're not doing anything else let's join in the vote. We change our state to 'voting' and prompt the user with question mark.
- Vote yes by pressing A. We need to check if we're in the 'voting' state - this means we only vote when we're asked and only once. The vote is shown on the LEDs and sent as a radio message.
- Vote no by pressing B. This is very similar to the yes code.
- All that's left to do is count up the votes. Extend the 'on radio received' block to collect these votes.
- Congratulations, you made it! Now you can get your friends together and vote on something. How about... "Do you love micro:bit?". One person asks the question, presses A+B and then everyone else either presses A for yes, or B for no. The person who started the vote can call out the result: Yes, No or Draw. Time for another vote!
- Can you show how many people voted for yes and no? Was it a close victory or did everyone agree?
- Can you make the result show on all the voters LEDs?
- Can you make a multiple choice vote? i.e. A, B, C, or D?