In our previous experiment , we have learned how to use Micro:bit to control 2 LEDs and make them twinkle alternatively. This time we are going to use a button to control LED flash. That means when we press down the button,the 2 LEDs flash in turns; when release the button, the LEDs stop flashing.
This is a common component for controlling electronic devices. It is mostly used to connect or cut off the control circuit, it can achieve the control for motors or other electronic equipments.
Momentary Pushbutton Switch usually stays on. When it is pressed down, the circuit is connected; when it is released, it will bounce back to the status of disconnection.
Momentary Pushbutton Switch has 4 pins which can be divided into 2 groups: pin 1 short connects pin 2, pin 3 short connects pin 4.
1.Set pin P2 to the high potential in the bricks of “on start”.
2.Read the status from P2 to check if the button is pressed, if yes, digital write signal 0 to P0 port to turn off the LED; digital write signal 1 to P1 port to turn on the LED, then set the pause in 500ms; digital write signal 1 to P0 port to turn on the LED; digital write signal 0 to P1 port to turn off the LED, then set the pause in 500ms.
When the button is pressed, you can see the 2 LEDs flashing alternatively; When the button is released, they would stop flashing. If not, you need to go back and check your operations.