The snap:bit is an electronic component for the Snap Circuits educational electronic kit. It features a socket for connecting the BBC micro:bit. This allows the Snap Circuits to be programmatically controlled by the micro:bit.
This project demonstrates how to properly connect the Press Switch (S2) component of Snap Circuits to the snap:bit board. Pressing the button would trigger an event that can be processed by the code loaded on the micro:bit. The micro:bit display will change when the button is pressed.Snap Circuits diagram
Build the circuit shown in the diagram above.Code
You can build the code yourself in the MakeCode Editor. You will find the "on pin pressed" block under the Input section.
Alternatively, open the ready project here: https://makecode.microbit.org/#pub:_Fkx5gkDa7MWa
Once ready, download the code to your micro:bit. Then disconnect all cables from your micro:bit. Both the USB and the battery pack must be disconnected from the micro:bit.How it works...
When you close the slide switch (S1), the Battery Holder (B1) powers the snap:bit through the 3V snap and the micro:bit turns on. The “on start” event triggers and the micro:bit displays the smiley face. Press the press switch (S2) and release it within 1 second. This closes the circuit between the P2 and GND pins of the micro:bit. The “on pin P2 pressed” event triggers and the micro:bit now displays a grumpy face. Press and release the press switch once again. The micro:bit displays the smiley face again.
Note that the code uses a helper variable with the name “smile”. When the micro:bit displays the smiley face, it sets “smile” to 1. When the micro:bit displays the grumpy face, it sets “smile” to 0. The value of the “smile” variable is checked every time the “on pin P2 pressed” event triggers. This helps the micro:bit alternate the smiley and grumpy faces every time the press switch (S2) is pressed.
The press switch (S2) can be connected across any of the 0, 1 and 2 snaps and the GND snap. Pressing and releasing the press switch within one second triggers the events “on pin P0 pressed”, “on pin P1 pressed” or “on pin P2 pressed” respectively.