Buttons for input
Buttons have been used as human input devices since computers first existed. Watch this video and see how they let the user tell the micro:bit to do something.
This micro:bit activity guides you to create a program with three variables that will keep score for a game of Rock Paper Scissors.
To do this, you will need to create variables for the parts of scorekeeping that change over the course of a gaming session. What are those variables?
First, let’s consider the names of our variables. In general, variable names should clearly describe what type of information they hold. They should be clear and easy for a reader to understand regardless of their familiarity with your program.
In MakeCode, from the Variables menu, make and name these three variables: PlayerAWins
, PlayerBWins
, PlayersTie
.
It is important to give your variables an initial value. The initial value is the value the variable will hold each time the program starts. For our counter program, we will give each variable the value 0 (zero) at the start of the program.
let PlayerAWins = 0
let PlayerBWins = 0
let PlayersTie = 0
In our program, we want to keep track of the number of times each player wins and the number of times they tie. We can use the buttons A and B to do this.
Pseudocode:
We already initialized these variables and now need to code to update the values at each round of the game.
PlayerAWins
.PlayerBWins
.PlayersTie
.From the Input menu, drag 3 of the ‘on button A pressed’ event handlers to your Programming Workspace.
Leave one block with ‘A’. Use the drop-down menu in the block to choose ‘B’ for the second block and ‘A+B’ for the third block.
From the Variables menu, drag 3 of the ‘change PlayersTie by 1’ blocks to your Programming Workspace.
Place one change block into each of the Button Pressed blocks.
Choose the appropriate variable from the pull down menus in the change blocks.
let PlayerAWins = 0
let PlayerBWins = 0
let PlayersTie = 0
input.onButtonPressed(Button.A, () => {
PlayerAWins += 1
})
input.onButtonPressed(Button.B, () => {
PlayerBWins += 1
})
input.onButtonPressed(Button.AB, () => {
PlayersTie += 1
})
Whenever the scorekeeper presses button A, button B, or both buttons together, we will give the user visual feedback acknowledging that the user pressed a button. We can do this by coding our program to display:
We can display an ‘A’, ‘B’, or ‘T’ using either the ‘show leds’ block or the ‘show string’ block.
In this example, we have used the ‘show leds’ block.
let PlayerAWins = 0
let PlayerBWins = 0
let PlayersTie = 0
input.onButtonPressed(Button.A, () => {
PlayerAWins += 1
basic.showLeds(`
. # # # .
. # . # .
. # # # .
. # . # .
. # . # .
`)
basic.clearScreen()
})
input.onButtonPressed(Button.B, () => {
PlayerBWins += 1
basic.showLeds(`
. # # . .
. # . # .
. # # # .
. # . # .
. # # . .
`)
basic.clearScreen()
})
input.onButtonPressed(Button.AB, () => {
PlayersTie += 1
basic.showLeds(`
. # # # .
. . # . .
. . # . .
. . # . .
. . # . .
`)
basic.clearScreen()
})
Notice that we added a ‘clear screen’ block after showing ‘A’, ‘B’, or ‘T’. What do you think would happen if we did not clear the screen? Try it.
To finish our program, we can add code that tells the micro:bit to display the final values of our variables. Since we have already used buttons A and B, we can use the ‘on shake’ event handler block to trigger this event. We can use the ‘show string’, ‘show leds’, ‘pause’, and ‘show number’ blocks to display these final values in a clear way. Here is the complete program.
let PlayersTie = 0
let PlayerBWins = 0
let PlayerAWins = 0
input.onButtonPressed(Button.A, () => {
PlayerAWins += 1
basic.showLeds(`
. # # # .
. # . # .
. # # # .
. # . # .
. # . # .
`)
basic.clearScreen()
})
input.onButtonPressed(Button.B, () => {
PlayerBWins += 1
basic.showLeds(`
. # # . .
. # . # .
. # # # .
. # . # .
. # # . .
`)
basic.clearScreen()
})
input.onButtonPressed(Button.AB, () => {
PlayersTie += 1
basic.showLeds(`
. # # # .
. . # . .
. . # . .
. . # . .
. . # . .
`)
basic.clearScreen()
})
input.onGesture(Gesture.Shake, () => {
basic.showString("Wins:")
basic.showLeds(`
. # # # .
. # . # .
. # # # .
. # . # .
. # . # .
`)
basic.showNumber(PlayerAWins)
basic.pause(1000)
basic.showLeds(`
. # # . .
. # . # .
. # # # .
. # . # .
. # # . .
`)
basic.showNumber(PlayerBWins)
basic.pause(1000)
basic.showString("Ties:")
basic.showNumber(PlayersTie)
basic.pause(1000)
basic.clearScreen()
})
PlayerAWins = 0
PlayerBWins = 0
PlayersTie = 0
Download the Scorekeeper program to the micro:bit, and find someone to play Rock, Paper, Scissors with you using your micro:bit to act as the Scorekeeper!
There is more we can do with the input we received using this program. We can use mathematical operations on our variables.
Example: Perhaps you’d like to keep track of, and show the player the total number of ‘rounds’ that were played. To do this, we can add the values stored in the variables we created to keep track of how many times each player won and how many times they tied.
In order to do this, we can add the code to our program under the ‘on shake’ event handler.
PlayerAWins
, PlayerBWins
, and PlayersTie
and then display the sum of this mathematical operation.PlayerAWins
and PlayerBWins
, then add PlayersTie
.let PlayersTie = 0
let PlayerBWins = 0
let PlayerAWins = 0
input.onGesture(Gesture.Shake, () => {
basic.showString("Total rounds played:")
basic.showNumber(PlayerAWins + PlayerBWins + PlayersTie)
})
Remember that the micro:bit is a device that processes input and displays it as output in some way. By storing values in variables, you can perform mathematical operations on that data that provides you with useful information.
What other math operations could provide valuable information from the values stored in these variables?
Examples:
Questions:
Answers: