adding pins.pulseIn API
This commit is contained in:
@ -13,6 +13,7 @@ pins.onPulsed(DigitalPin.P0, PulseValue.High, () => {
|
||||
|
||||
});
|
||||
pins.pulseDuration();
|
||||
pins.pulseIn(DigitalPin.P0, PulseValue.High);
|
||||
pins.servoWritePin(AnalogPin.P0, 180);
|
||||
pins.servoSetPulse(AnalogPin.P0, 1500);
|
||||
pins.i2cReadNumber(0, NumberFormat.Int8LE);
|
||||
@ -25,4 +26,4 @@ pins.analogSetPitchPin(AnalogPin.P0);
|
||||
|
||||
### See Also
|
||||
|
||||
[digitalReadPin](/reference/pins/digital-read-pin), [digitalWritePin](/reference/pins/digital-write-pin), [analogReadPin](/reference/pins/analog-read-pin), [analogWritePin](/reference/pins/analog-write-pin), [analogSetPeriod](/reference/pins/analog-set-period), [map](/reference/pins/map), [onPulsed](/reference/pins/on-pulsed), [pulseDuration](/reference/pins/pulse-duration), [servoWritePin](/reference/pins/servo-write-pin), [servoSetPulse](/reference/pins/servo-set-pulse), [i2cReadNumber](/reference/pins/i2c-read-number), [i2cWriteNumber](/reference/pins/i2c-write-number), [setPull](/reference/pins/set-pull), [analogPitch](/reference/pins/analog-pitch), [analogSetPitchPin](/reference/pins/analog-set-pitch), [spiWrite](/reference/pins/spi-write)
|
||||
[digitalReadPin](/reference/pins/digital-read-pin), [digitalWritePin](/reference/pins/digital-write-pin), [analogReadPin](/reference/pins/analog-read-pin), [analogWritePin](/reference/pins/analog-write-pin), [analogSetPeriod](/reference/pins/analog-set-period), [map](/reference/pins/map), [onPulsed](/reference/pins/on-pulsed), [pulseDuration](/reference/pins/pulse-duration), [pulseIn](/reference/pins/pulse-in), [servoWritePin](/reference/pins/servo-write-pin), [servoSetPulse](/reference/pins/servo-set-pulse), [i2cReadNumber](/reference/pins/i2c-read-number), [i2cWriteNumber](/reference/pins/i2c-write-number), [setPull](/reference/pins/set-pull), [analogPitch](/reference/pins/analog-pitch), [analogSetPitchPin](/reference/pins/analog-set-pitch), [spiWrite](/reference/pins/spi-write)
|
||||
|
@ -27,6 +27,12 @@ format from the 7-bit I2C address `32`.
|
||||
pins.i2cReadNumber(32, NumberFormat.UInt16BE);
|
||||
```
|
||||
|
||||
#### ~hint
|
||||
|
||||
This function is not supported in the simulator.
|
||||
|
||||
#### ~
|
||||
|
||||
### See also
|
||||
|
||||
[I2C](https://en.wikipedia.org/wiki/I%C2%B2C)
|
||||
|
@ -27,6 +27,13 @@ address `32` in big-endian 32-bit integer format.
|
||||
```blocks
|
||||
pins.i2cWriteNumber(32, 2055, NumberFormat.Int32BE);
|
||||
```
|
||||
|
||||
#### ~hint
|
||||
|
||||
This function is not supported in the simulator.
|
||||
|
||||
#### ~
|
||||
|
||||
### See also
|
||||
|
||||
[I2C](https://en.wikipedia.org/wiki/I%C2%B2C)
|
||||
|
53
docs/reference/pins/pulse-in.md
Normal file
53
docs/reference/pins/pulse-in.md
Normal file
@ -0,0 +1,53 @@
|
||||
# Pulse In
|
||||
|
||||
Returns the duration of a pulse (high or low) from a [pin](/device/pins) on
|
||||
the micro:bit board in microseconds.
|
||||
|
||||
```sig
|
||||
pins.pulseIn(DigitalPin.P0, PulseValue.High)
|
||||
```
|
||||
|
||||
### ~avatar
|
||||
|
||||
Some pins are also used by the [LED screen](/device/screen).
|
||||
Please read the [page about pins](/device/pins) carefully.
|
||||
|
||||
### ~
|
||||
|
||||
### Parameters
|
||||
|
||||
* ``name`` is a [string](/reference/types/string) that stores the name of the pin (``P0``, ``P1``, or ``P2``, up through ``P20``)
|
||||
* ``value`` is the value of the pulse, ``high`` or ``low``
|
||||
|
||||
### Returns
|
||||
|
||||
* a [number](/reference/types/number) that represents the pulse duration in micro-seconds
|
||||
|
||||
### Example: football score keeper
|
||||
|
||||
The following script sends a pulse on ``P0`` and reads the pulse returned by a HC-SR04 sonar to determine the distance of the object in front of the sensor.
|
||||
|
||||
```blocks
|
||||
basic.forever(() => {
|
||||
// send pulse
|
||||
pins.digitalWritePin(DigitalPin.P0, 0)
|
||||
control.waitMicros(2)
|
||||
pins.digitalWritePin(DigitalPin.P0, 1)
|
||||
control.waitMicros(10)
|
||||
pins.digitalWritePin(DigitalPin.P0, 0)
|
||||
|
||||
// read pulse
|
||||
led.plotBarGraph(pins.pulseIn(DigitalPin.P1, PulseValue.High) / 58, 0)
|
||||
basic.pause(100)
|
||||
})
|
||||
```
|
||||
|
||||
#### ~hint
|
||||
|
||||
This function is not supported in the simulator.
|
||||
|
||||
#### ~
|
||||
|
||||
### See also
|
||||
|
||||
[digital write pin](/reference/pins/digital-write-pin),
|
Reference in New Issue
Block a user