5. case 03 Function Selector¶
5.1. Our Goal¶
Use basic kit to create a function selector.
1 x basic kit
5.3. Hardware Connection¶
Connect the crash sensor to P0 port on basic:bit and connect the potentiometer to P1. See picture below.
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
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.
itmmmis under 10, it means button A is pressed. When button A is pressed, call function
func_Aand set variable
flag(Variable for judging the internal loop in the function) to 0 after call.
itmmmis under 80, it means button B is pressed. When button B is pressed, call function
func_Band set variable
flagto 0 after call.
itmmmis under 130, it means button C is pressed. When button B is pressed, call function
flagis 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.
flagis 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.
func_C: Clear the screen and finish the call of function.
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.
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.