Microbit Digital Inputs
Digital inputs are ways of interacting with the a Microbit that are either ON or OFF.
Examples include:
- A button is pressed / A button is not pressed
- The Microbit is being shaken / The Microbit is not being shaken
You can program your Microbit to handle digital inputs in two different ways:
- onEvent Functions
- event Loops
onEvent Functions
This code simply waits until your have pressed the button, then it executes the code in the function (does what you told it to!).
Example:
Here the Microbit will display the letter A when you press the A button.
Event Loops
Event loops work by code that checks a buttons state continuously.
Here the Microbit will display nothing until you press A, then it will display A until you let go of the button.
Challenge 1 – A, B or Both pressed?
Create a program that tells the user which button is pressed:
- if A is pressed, say A
- if b is pressed, say B
- if both are pressed, say C
Challenge 2 -Sad Microbit
Create a program on the Microbit that:
- When the program starts it displays a happy face
- When you shake the Microbit it displays a sad face.
Challenge 3 – Liftoff!
Make a program that…
When you press A:
- Count down from 10 to 1
- say ‘lift off!’
- display a rocket launching animation
Challenge 4 – Microbit Dice
Write a program that…
When you press A:
- Display a random number from 0 to 6
Hints – These blocks might help you!
Challenge 5 – Magic 8 Ball
Write a program that:
- Every time you shake the microbit:
- it gives you a random piece of advice.
Hints – These blocks might help you!
This webpage might help you out!
For top marks you should use a list instead of lots of if statements!
Challenge 6 – Counting Microbit
Write a program that…
- When your program starts 0 is displayed.
- Every time you press A the number is increased by 1
- Every time you press B the number is decreased by 1
- Every time you press both buttons the number is reset to zero.
Hints – These blocks might help you!
You will need to use a variable to keep count.
Extra Challenges
Reaction timer
Make a reaction timer game the tests the user’s reaction time. When a smiley face is displayed, the user has to press A as quickly as possible!
This block might help you!