Tell me more ×
Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
3  
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. – jippie Feb 5 at 7:42

1 Answer

up vote 1 down vote accepted

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.

share|improve this answer
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! – Chris Feb 8 at 3:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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