STEP1: Connect Push-button module to pin P0 of the expansion board; connect the LED to pin P1.
STEP2: Programming
1) Click “micro: bit” module, find “read digital pin P0” block and drag it to script area.
2) The “=” in the “Operators” module can be used to judge if the push-button is being pressed. If the value of “read digital pin P0” is “1”, the button is pressed, otherwise, it is unpressed. Nest the block “read digital pin P0” into the equation.
Figure 2.3.3 “=” Block
3) Find “if then…else” block in the “Control” module, and drag it into the “forever” block.
Figure 2.3.4 “If then…else” Block
4) Nest the conditions of equation into the “if then…else” block as shown below:
Figure 2.3.5 Nest Blocks
5) Find the block “set digital pin P0 output LOW” in the “micro: bit” module. Revise the block into “set digital pin P1 output HIGH” and place it under “if then” block. When the button is pressed, this command will be executed and the LED will be on. Then place “set digital pin P1 output LOW” block under the “…else” block. When the button is released, the LED will be off.
Figure 2.3.6 Run the Program
STEP1: Replace the push-button module in the last project with switch knob module.
STEP2: Programming
1. Connect the switch knob to pin P0. Click “micro: bit” module and select “read analog pin P0” block.
Figure 2.3.8 “read analog pin P0” Block
2. Drag “read analog pin P0” to the script area.
Figure 2.3.9 “read analog pin P0” Block
3. Drag “set analog pin P0 output” block of “micro: bit” module into “forever" block in script area, and change P0 to P1. Nest the “set analog pin P1 output” block with the block “read analog pin P0”.