2016-03-26 00:47:20 +01:00
|
|
|
# On Pin Pressed
|
|
|
|
|
2016-06-10 23:44:49 +02:00
|
|
|
Start an [event handler](/reference/event-handler) (part of the program
|
|
|
|
that will run when something happens, like when a button is pressed).
|
|
|
|
This handler works when you press pin `0`, `1`, or `2` together with `GND`.
|
|
|
|
When you are using this function in a web browser, click the pins on the screen instead of the ones
|
|
|
|
on the BBC micro:bit.
|
2016-03-26 00:47:20 +01:00
|
|
|
|
2016-06-10 23:44:49 +02:00
|
|
|
If you hold the `GND` pin with one hand and touch pin `0`, `1`, or `2` with the other,
|
|
|
|
a very small (safe) amount of electricity will flow through your body and back into
|
|
|
|
the micro:bit. This is called **completing a circuit**. It's like you're a big wire!
|
2016-03-26 00:47:20 +01:00
|
|
|
|
|
|
|
```sig
|
|
|
|
input.onPinPressed(TouchPin.P0, () => {
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
2016-06-10 23:44:49 +02:00
|
|
|
## ~hint
|
|
|
|
|
|
|
|
This function works best when the BBC micro:bit is using batteries for power,
|
|
|
|
instead of the USB cable.
|
|
|
|
|
|
|
|
## ~
|
|
|
|
|
2016-03-26 00:47:20 +01:00
|
|
|
### Example: pin pressed counter
|
|
|
|
|
2016-06-10 23:44:49 +02:00
|
|
|
This program counts how many times you press the `P0` pin.
|
|
|
|
Every time you press the pin, the program shows the number of times on the screen.
|
2016-03-26 00:47:20 +01:00
|
|
|
|
|
|
|
```blocks
|
|
|
|
let count = 0
|
|
|
|
basic.showNumber(count, 100)
|
|
|
|
input.onPinPressed(TouchPin.P0, () => {
|
|
|
|
count = count + 1
|
|
|
|
basic.showNumber(count, 100)
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
|
|
|
### See also
|
|
|
|
|
2016-04-18 17:33:09 +02:00
|
|
|
[BBC micro:bit pins](/device/pins), [pin is pressed](/reference/input/pin-is-pressed), [analog read pin](/reference/pins/analog-read-pin), [analog write pin](/reference/pins/analog-write-pin), [digital read pin](/reference/pins/digital-read-pin), [digital write pin](/reference/pins/digital-write-pin)
|
2016-03-26 00:47:20 +01:00
|
|
|
|