2016-03-26 00:47:20 +01:00
|
|
|
# Digital Read Pin
|
|
|
|
|
2016-06-23 00:19:10 +02:00
|
|
|
Read a **digital** (`0` or `1`) signal from a [pin](/device/pins) on
|
2016-11-02 01:44:37 +01:00
|
|
|
the @boardname@ board.
|
2016-03-26 00:47:20 +01:00
|
|
|
|
|
|
|
```sig
|
|
|
|
pins.digitalReadPin(DigitalPin.P3)
|
|
|
|
```
|
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
## ~avatar
|
2016-06-23 00:19:10 +02:00
|
|
|
|
|
|
|
Some pins are also used by the [LED screen](/device/screen).
|
|
|
|
Please read the [page about pins](/device/pins) carefully.
|
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
## ~
|
2016-06-23 00:19:10 +02:00
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
## Parameters
|
2016-03-26 00:47:20 +01:00
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
* ``name`` is a [string](/types/string) that stores the name of the pin (``P0``, ``P1``, or ``P2``, up through ``P20``)
|
2016-03-26 00:47:20 +01:00
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
## Returns
|
2016-03-26 00:47:20 +01:00
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
* a [number](/types/number) that can be `0` or `1`
|
2016-03-26 00:47:20 +01:00
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
## Example: football score keeper
|
2016-03-26 00:47:20 +01:00
|
|
|
|
2016-06-23 00:19:10 +02:00
|
|
|
This program reads pin `P0` to find when a goal is scored. When `P0`
|
|
|
|
is `1`, the program makes the score bigger and plays a buzzer sound
|
|
|
|
through `P2` with ``digital write pin``.
|
2016-03-26 00:47:20 +01:00
|
|
|
|
|
|
|
```blocks
|
|
|
|
let score = 0
|
|
|
|
basic.showNumber(score)
|
|
|
|
basic.forever(() => {
|
|
|
|
if (pins.digitalReadPin(DigitalPin.P0) == 1) {
|
|
|
|
score++;
|
|
|
|
pins.digitalWritePin(DigitalPin.P2, 1)
|
|
|
|
basic.showNumber(score)
|
|
|
|
basic.pause(1000)
|
|
|
|
pins.digitalWritePin(DigitalPin.P2, 0)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
2016-06-23 00:19:10 +02:00
|
|
|
This program is a remote control for the score keeper program. If you
|
2016-11-02 01:44:37 +01:00
|
|
|
connect `P1` on the remote control @boardname@ to `P0` on the score
|
|
|
|
keeper @boardname@, you can press button `B` on the remote to buzz and
|
|
|
|
make the score bigger on the other @boardname@.
|
2016-03-26 00:47:20 +01:00
|
|
|
|
2016-06-23 00:19:10 +02:00
|
|
|
```blocks
|
2022-04-26 19:28:42 +02:00
|
|
|
input.onButtonEvent(Button.B, ButtonEvent.Click, () => {
|
2016-06-23 00:19:10 +02:00
|
|
|
pins.digitalWritePin(DigitalPin.P1, 1);
|
|
|
|
basic.pause(500);
|
|
|
|
pins.digitalWritePin(DigitalPin.P1, 0);
|
|
|
|
});
|
|
|
|
```
|
2019-12-02 05:58:26 +01:00
|
|
|
### ~hint
|
2016-06-23 00:19:10 +02:00
|
|
|
|
2016-11-02 01:44:37 +01:00
|
|
|
Remember to connect `GND` on both @boardname@s together!
|
2016-06-23 00:19:10 +02:00
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
### ~
|
2016-06-23 00:19:10 +02:00
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
## See also
|
2016-03-26 00:47:20 +01:00
|
|
|
|
2016-11-02 01:44:37 +01:00
|
|
|
[@boardname@ pins](/device/pins),
|
2016-06-23 00:19:10 +02:00
|
|
|
[digital write pin](/reference/pins/digital-write-pin),
|
|
|
|
[analog read pin](/reference/pins/analog-read-pin),
|
|
|
|
[analog write pin](/reference/pins/analog-write-pin),
|
|
|
|
[on pin pressed](/reference/input/on-pin-pressed),
|
|
|
|
[pin is pressed](/reference/input/pin-is-pressed)
|