I'm working on a rudimentary flight stabilization system for an airplane, which reads in 4 PWM inputs from an R/C receiver. The problem is, the pulseIn command is taking far too long to execute, so I need an alternative method of reading the four inputs. With all the code running except the PWM, my entire cycle takes ~ 45ms, however adding the four pulseIn(pin, HIGH) calls leads to ~250ms cycle times, which isn't working out for me. Is there an alternative, perhaps an interrupt-driven, way to read the duty cycle time of the PWM lines?

  • 3
    \$\begingroup\$ I guess the recipe would be along these lines: Free running timer which current value you can read at any given time. Four input pins that trigger an interrupt at both edges. Then the interrupt routine can read the timer at every edge and with a bit of memory you can calculate pulse ratio. You'll have to program the hardware much more directly than when using the Arduino libraries though, so you better start by finding the related datasheet. \$\endgroup\$ – jippie Feb 5 '13 at 7:42

the pulseIn is a blocking command that waits for it to complete, plus its overhead. Sounds like you should directly set up and us the Input Capture Interrupts. They capture the exact time of the edge, creating an interrupt. And some time (soon) after the Service of the interrupt reads the latch time. See example InputCapture.ino. From this example you can scale it capture all the inputs, assuming you have enough Input Capture Pins.

If you don't need exact (nano second) then you can use PinChangeInt to get an interrupt and at time of Interrupt service record the time stamp. A bit latent, but may have the precision you require.

| improve this answer | |
  • \$\begingroup\$ I did find a variation on the PinChangeInt method thats worked perfectly for me, my control loop is down to 50ms without the slightest hint of jitter. Thanks! \$\endgroup\$ – Chris Feb 8 '13 at 3:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.