case 03 Function Selector
6. case 03 Function Selector#
6.1. Our Goal#
Use basic kit to create a function selector.
1 x basic kit
6.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: 1080
Button C: 80130
Button D: 130160
Button E: 160600
Create a forever loop, read the value of P0 port in analog way, and then assign the return value to variable
itmmm so as to judge which button is pressed.
itmmm is under 10, it means button A is pressed. When button A is pressed, call function
func_A and set variable
flag(Variable for judging the internal loop in the function) to 0 after call.
itmmm is under 80, it means button B is pressed. When button B is pressed, call function
func_Band set variable
flag to 0 after call.
itmmm is under 130, it means button C is pressed. When button B is pressed, call function
flag is 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.
flag is 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.
The link of the whole program: https://makecode.microbit.org/_cuufKuP6FARo
You can also check the program from the page below.
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_C and 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.
6.9. Relative Readings#
Interrupt: When something unexpected occured during the operation of a computer, it will stop the current program and transfer to a new program. Once the new program is processed, it will return to and continue its original suspended program.