Software apps and online services
The current project builds on top of the "Measure Wind Speed with Micro:bit and Snap Circuits" project. We had such a great fun building the anemometer, that we decided to extend the game by introducing two players who take turns and try to blow stronger than the other. All our family had fun, so join us and code your game!
- Toss a coin to choose which player is first.
- Player 1 presses the A button on the micro:bit and blows.
- Player 2 presses the B button and blows.
- Press both A and B buttons to show the result of the game. The winner and the scores of both players are shown.
- Repeat for more fun!
Build the circuit shown in the diagram above.
The Kitronik :VIEW text32 LCD Screen must be plugged in the snap:bit. The BBC micro:bit must be plugged in the Kitronik :VIEW text32 LCD Screen.
The DYI anemometer is attached to the shaft of the Motor (M1). See the "Measure Wind Speed with Micro:bit and Snap Circuits" project to learn how to make your own anemometer.
Note that this circuit is powered by the batteries of the Kitronik :VIEW text32 LCD Screen. Use the switch on the left edge of the LCD screen to turn the whole circuits on and off.
The code for this project might be a little bit overwhelming but, hey, this is the code of a complete game. Don't worry, we will explain each part of it!
You can build the code yourself in the MakeCode Editor. You will find the blocks in the following sections:
- The "set" block is under the Variables section.
- The "function" and "call" blocks are under the Advanced > Functions section.
- The "analog read pin" block is under the Advanced > Pins section.
- The "plot bar graph" block is under the Led section.
- The "max" block is under the Math section
- The "show string" block works only if you have the Kitronik :VIEW text32 LCD Screen. You have to install the "kitronik-viewtext32" extension under the Advanced > Extensions section. Search for "text32" if you don't see it on the list.
- The "join" block is under the Advanced > Text section.
- The "pause" block is under the Basic section.
Alternatively, open the ready project here: https://makecode.microbit.org/_DeV39DiEeL2k
Once ready, download the code to your micro:bit. Then disconnect all cables from your micro:bit. Both the USB and the battery pack must be disconnected from the micro:bit.
The game has two players.
When button A is pressed the Kitronik :VIEW text32 LCD Screen shows the message: "Player 1 Ready!". The micro:bit waits for 3000 ms and now Player 1 is expected to blow. The micro:bit plots on the LED screen a bar graph of the measured signal on pin P1, where the Motor (M1) is connected. Then the strength of the wind gust is shown as a number on Kitronik LCD Screen.
When button B is pressed the Kitronik LCD screen shows the message: "Player 2 Ready!". The micro:bit waits for 3000 ms and now Player 2 is expected to blow. The micro:bit plots on the LED screen a bar graph of the measured signal on pin P1. Then the strength of the wind gust is shown as a number on the Kitronik LCD screen.
Finally, A+B buttons should be pressed simultaneously, so that the game ends and the winner is announced. The records of both players are displayed on the LCD screen and a new game is started.
The game introduces three states. Depending on the state, checked in the forever cycle, the micro:bit decides how the game should continue.
State 0 is the initial state of the game when the BBC micro:bit is started.
This state is also entered after a game has ended so that the user can start over a new game.
State 1 and 2 are used to keep track of the current player. We provide these states to the "play" function so that we know which player is playing now.
State 3 indicates that the game has finished. This state is entered when both A+B buttons are pressed.
When the BBC micro:bit is started, the game enters state 0 - the initial state. The "forever" cycle calls the "newGame" function.
This function is used to initialize the variables used in the game and notify the player how to start the game.
We already discussed the "state" variable. The "lastState" variable keeps track of the previous state, so that we know if the state has changed. The other variables hold the values of the maximum gust, the maximum for player 1 and 2.
The first player is expected to press a micro:bit button to start the game.
When a button is pressed, the state is changed to the player's number to keep track of who is playing.
If both A+B buttons are pressed we enter state 3.
If we refer to the "forever" cycle, we notice that when the state is 3 we call the game over function. Let's examine what exactly this function does:
It calls consequently two other functions. First, is the "showResults" function.
In this function, we check who the winner is by comparing the players' records. Then we report the winner with the "show string" instruction. After reporting the winner, we display the maximum blow results for both players.
The second function, called when the game ends is "newGame". We have already explained how it works when we talked about starting the game.
Let's now have a look at the "play" function.
It takes as an argument the current player. We pass the state variable as the states 1 and 2, indicate the current player. We "play" only in case the state has changed i.e we've started the game, or the player has changed. We check if the state/player has changed by comparing the last state and the current player. In case they are different we need to keep the records for the current player i.e. "play" the blow game.
We initialize the wind and maxWind speed and display a message "Player X Ready!". Then, we wait for 3000 ms and read the signal on pin 1 (P1) on the micro:bit. This is where the Motor (M1) is connected. The faster the motor rotates the stronger electric signal is generated from the motor to pin P1.
Once the analog signal is read, it is displayed as a bar graph of the measured wind. We decided that the maximum wind speed should be 200 and the measured value is plotted comparatively to this maximum value.
In case there is a new wind value that is larger than the previous records for that player, we need to set this as the "maxWind" value or the new record value for the player. Then the new record is displayed and the last state of the game is set to the last player who played.
Now that we have walked you through the code, you can watch how we play the game and have fun!
Now it is your turn. Enjoy!