UNPKG

pxt-common-packages

Version:
41 lines (29 loc) 1.28 kB
# pulse Duration Get the length of time for last pulse at any of the digital pins. ```sig pins.pulseDuration() ``` If you have code in an [``||pins:on pulsed||``](/reference/pins/on-pulsed) event and you want to know how long the pulse lasted on that pin, use ``||pins:pulse duration||``. The ``||pins:pulse duration||`` block remembers how many microseconds the last pulse was. This is only for the most recent pulse period that happened at any of the pins. So, you use it in an ``||pins:on pulsed||`` event block so that you know which pin the pulse was on. ## Returns * a [number](/types/number) that is length of time (duration) of the last pulse, in microseconds. ## Example #example Count every pulse on pin `D4` that is longer than 2 milliseconds in duration. Write the total number of pulses to the serial port every time the count adds another thousand pulses. ```blocks let pulses = 0 pins.D4.setPull(PinPullMode.PullUp) pins.D4.onPulsed(PulseValue.Low, () => { if (pins.pulseDuration() > 2000) { pulses++ } if (pulses % 1000 == 0) { serial.writeValue("pulse count", pulses) } }) ``` ## See also #seealso [digital read](/reference/pins/digital-read), [set pull](/reference/pins/set-pull), [on pulsed](/reference/pins/on-pulsed)