Case 03 Function Selector
Our Goal
- Use basic kit to create a function selector.
Material
- 1 x basic kit
Hardware Connection
- Connect the crash sensor to P0 port on basic:bit and connect the potentiometer to P1. See picture below.

Software
-
The crash sensor analog read the return value of I/O port. Here's the values:
-
Button A <10
- Button B: 10-80
- Button C: 80-130
- Button D: 130-160
- Button E: 160-600
Programming
Step 1
- Create a forever loop, read the value of P0 port in analog way, and then assign the return value to variable
itmmmso as to judge which button is pressed. - If
itmmmis under 10, it means button A is pressed. When button A is pressed, call functionfunc_Aand set variableflag(Variable for judging the internal loop in the function) to 0 after call. - If
itmmmis under 80, it means button B is pressed. When button B is pressed, call functionfunc_Band set variableflagto 0 after call. - If
itmmmis under 130, it means button C is pressed. When button B is pressed, call functionfunc_C.

Step 2
- Function
func_A: Whenflagis beyond 600(i.e. no buttons pressed), read the value of P1 port and plot it on micro:bit screen. Read the button status of P0 port. When button E is pressed, the loop is terminated and the function call is finished. - Function
func_B: Whenflagis beyond 600(i.e. no buttons pressed), display a flashing heart. Read the button status of P0 port after each flash. When button E is pressed, the loop is terminated and the function call is finished. - Function
func_C: Clear the screen and finish the call of function.

Program
The link of the whole program: https://makecode.microbit.org/_cuufKuP6FARo
You can also check the program from the page below.
Result
- When startup, micro:bit will display an image of house.
- Press button A to call function
func_A. We can use potentiometer to control the brightness of the LED screen . And we can press any button to finish the call of function. - Press button B to call function
func_B, and micro:bit will display a flashing heart. And we can press any button to finish the call of function. - Press button C to call function
func_Cand clear the screen. - For other situations, micro:bit will display a house image.
Think
Q: why there is nothing happened when a button is pressed down?
A: The button status judgement is not always happen. When other section of code is running, the button program will stop judge.