|Top Previous Next|
Gets the high pulse width of a capture channel.
The integer number of counts in the most recent high pulse width on the capture channel input, or 0 if there has been no transition on the input since the last time the pulse width was read.
The capture channel must be assigned a pin using the pin property before the pulseHi function can return a valid value.
The value is in counts; use the resolution constant to get the number of counts per second. The actual pulse width (in fractional seconds) can computed as follows:
width = Cell.Capture.pulseHi(channel) / Cell.Capture.resolution;
where channel is the desired capture channel number.
Once the pulse width has been read, subsequent attempts to read the value will return 0 until a transition (low-to-high or high-to-low) occurs on the channel's input pin.
This example decodes the signal used by an R/C servo. The signal is a pulse 1.0 - 2.0 ms long, where 1.5 ms is considered zero. The pulses are typically sent at a frequency of 30 - 60 Hz. The example assumes there are two such signals to decode, connected to pins A6 and A7. The DecodeServo function returns a value in the range -1 to +1 based on the pulse width, or NaN if there is no signal present.
// Assign capture channels
pulse = Cell.Capture.pulseHi(chan);
if (pulse == 0)
// This first normalizes the pulse value to the
// range 2.0 - 4.0; subtract 3 to get -1.0 to +1.0
return pulse * 2000 / Cell.Capture.resolution - 3;