2022-05-09 19:10:17 +02:00
|
|
|
# On Pin Event
|
2016-03-26 00:47:20 +01:00
|
|
|
|
2016-07-16 01:43:26 +02:00
|
|
|
Start an [event handler](/reference/event-handler) (part of the
|
|
|
|
program that will run when something happens, like when a button is
|
2019-12-02 05:58:26 +01:00
|
|
|
pressed). This handler works when you touch pin `0`, `1`, or `2`
|
2022-08-10 18:36:19 +02:00
|
|
|
together with `GND`, and release it within 1 second. You can choose another event type by using
|
|
|
|
the [button event block](/reference/input/button-event). Possible event types are `pressed down` (1), `released up` (2), `clicked` (3), `long clicked` (4) or `hold` (5).
|
2019-12-02 05:58:26 +01:00
|
|
|
When you are using this function in a web
|
2016-11-02 01:44:37 +01:00
|
|
|
browser, click the pins on the screen instead of the ones on the
|
|
|
|
@boardname@.
|
2016-07-16 01:43:26 +02:00
|
|
|
|
2022-08-10 18:36:19 +02:00
|
|
|
If you hold the `GND` pin with one hand and touch pin `0`, `1`, `2` or `3`
|
2016-07-16 01:43:26 +02:00
|
|
|
with the other, a very small (safe) amount of electricity will flow
|
2016-11-02 01:44:37 +01:00
|
|
|
through your body and back into the @boardname@. This is called
|
2016-07-16 01:43:26 +02:00
|
|
|
**completing a circuit**. It's like you're a big wire!
|
2016-03-26 00:47:20 +01:00
|
|
|
|
|
|
|
```sig
|
2022-08-10 18:36:19 +02:00
|
|
|
input.onPinTouchEvent(TouchPin.P0, input.buttonEventClick(), () => {
|
2016-03-26 00:47:20 +01:00
|
|
|
})
|
|
|
|
```
|
|
|
|
|
2016-06-10 23:44:49 +02:00
|
|
|
## ~hint
|
|
|
|
|
2016-11-01 18:42:42 +01:00
|
|
|
This function works best when the @boardname@ is using batteries for power,
|
2016-06-10 23:44:49 +02:00
|
|
|
instead of the USB cable.
|
|
|
|
|
|
|
|
## ~
|
|
|
|
|
2016-07-16 01:43:26 +02:00
|
|
|
## Parameters
|
|
|
|
|
|
|
|
* ``name`` means the pin that is being pressed, either `P0`, `P1`, or `P2`
|
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
## Pin presses in action
|
|
|
|
|
|
|
|
See how the @boardname@ detects a press at a pin or on something connected to a pin in this video:
|
|
|
|
|
|
|
|
https://www.youtube.com/watch?v=GEpZrvbsO7o
|
|
|
|
|
|
|
|
## Example: pin pressed counter
|
2016-03-26 00:47:20 +01:00
|
|
|
|
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
|
2019-12-02 05:58:26 +01:00
|
|
|
basic.showNumber(count)
|
2022-08-10 18:36:19 +02:00
|
|
|
input.onPinTouchEvent(TouchPin.P0, input.buttonEventClick(), () => {
|
2016-03-26 00:47:20 +01:00
|
|
|
count = count + 1
|
2019-12-02 05:58:26 +01:00
|
|
|
basic.showNumber(count)
|
2016-03-26 00:47:20 +01:00
|
|
|
})
|
|
|
|
```
|
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
## See also
|
2016-03-26 00:47:20 +01:00
|
|
|
|
2022-08-10 18:36:19 +02:00
|
|
|
[@boardname@ 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), [button event block](/reference/input/button-event)
|
2016-03-26 00:47:20 +01:00
|
|
|
|