timer slow down


I have a transmitter and receiver for my RC car and I read one of the PWM signals of the receiver, but if I play a sound the readings the counts are lower and irregular. Can anyone help me? my code is attached.


RC_car_V8_sound.ino (1.44 KB)

I'm guessing that the tmrpcm library uses very frequent timer interrupts to play sounds. Some of those interrupts are going to occur during the interval you are trying to measure with a busy loop. The loop won't be counting during the time spent in the interrupt routines so it will get a lower answer. You need to find a hardware way to measure the pulse duration.

Great idea but how???

Pin 8 (Port B, bit 0) can be set up to store the value of Timer1 and generate an interrupt on a rising or falling edge. In the ISR you would save the stored timer value, switch from Rising to Falling and use the value stored by the next interrupt to calculate the length of the pulse. You can run the timer as fast as 16 MHz (16 counts per microsecond) but I would use that /8 prescaler to count at 2 MHz (two counts per microsecond) since you don't really need better resolution.

Of course this only works if TMRPCM doesn't use Timer1.