pxt-calliope/docs/reference/input/on-pin-pressed.md

33 lines
1.2 KiB
Markdown
Raw Normal View History

2016-03-26 00:47:20 +01:00
# On Pin Pressed
2016-04-13 17:27:45 +02:00
Register an [event handler](/reference/event-handler) that will execute whenever the user holds the `GND` pin with one hand, and presses pin `0`, `1`, or `2` with the other hand, thus completing a circuit; when you run a script with this function in a web browser, click pins 0 , 1, or 2 on the simulator.
2016-03-26 00:47:20 +01:00
*Note* that this function works best when the BBC micro:bit is powered by AAA battery.
```sig
input.onPinPressed(TouchPin.P0, () => {
})
```
### Example: pin pressed counter
This example counts how many times the P0 pin is pressed. Each time the pin is pressed, the global count variable is increased by 1 and displayed on the screen.
```blocks
let count = 0
basic.showNumber(count, 100)
input.onPinPressed(TouchPin.P0, () => {
count = count + 1
basic.showNumber(count, 100)
})
```
### Lessons
2016-04-13 17:27:45 +02:00
[love meter](/lessons/love-meter)
2016-03-26 00:47:20 +01:00
### 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