pxt-calliope/docs/lessons/love-meter/activity.md

48 lines
1.1 KiB
Markdown
Raw Normal View History

2016-03-26 00:47:20 +01:00
# love meter blocks activity
2016-11-02 01:44:37 +01:00
Create a love meter with the @boardname@
2016-03-26 00:47:20 +01:00
## ~avatar avatar
2016-03-26 00:47:20 +01:00
2016-11-02 01:44:37 +01:00
Welcome! This activity will help you create a love meter with the @boardname@. Let's get started!
2016-03-26 00:47:20 +01:00
## ~
2016-03-26 00:47:20 +01:00
Begin by registering an event with `on pin pressed` *P0* to know when someone is holding pin *P0* and pin *Gnd*.
```blocks
input.onPinPressed(TouchPin.P0, () => {
})
```
We are going to create a meter that displays a random number from *0* to *10*. We use *10* as `random number up to` returns a number between *0* and *n*.
```blocks
input.onPinPressed(TouchPin.P0, () => {
2018-06-01 20:42:38 +02:00
let x = Math.randomRange(0, 10)
2016-03-26 00:47:20 +01:00
})
```
2016-11-02 01:44:37 +01:00
Finally, let's show that number on the @boardname@. You are registering an event handler that will execute on the @boardname@ whenever the user holds the GND pin with one hand, and presses pin 0 with the other hand, thus completing a circuit
2016-03-26 00:47:20 +01:00
```blocks
input.onPinPressed(TouchPin.P0, () => {
2018-06-01 20:42:38 +02:00
let x = Math.randomRange(0, 10)
2016-03-26 00:47:20 +01:00
basic.showNumber(x)
})
```
## ~avatar avatar
2016-03-26 00:47:20 +01:00
2016-04-13 17:27:45 +02:00
Excellent, you're ready to continue with the [challenges](/lessons/love-meter/challenges)
2016-03-26 00:47:20 +01:00
## ~
2016-03-26 00:47:20 +01:00