4
// Testing reading from the receiver. We're expecting a PWM signal, on
5
// interrupt 0 (which is pin 2 on an Arduino Uno).
7
// This program tries to measure the frequency of the signal pulses in
8
// microseconds. It takes several measurements and prints out the average over
12
// number of signal pulses to average
13
#define SIGNAL_SAMPLES 10
16
// set to the time that the last signal pulse was at
17
static volatile unsigned long _new_pulse_at = 0;
20
// ISR to handle the PWM signal
24
_new_pulse_at = micros();
32
// set up an interrupt handler on pin 2
33
attachInterrupt( 0, signal_handler, RISING );
38
unsigned long last_pulse = 0;
39
unsigned long intervals[ SIGNAL_SAMPLES ] = {0};
45
unsigned long new_pulse = _new_pulse_at;
46
bool got_pulse = false;
47
if( new_pulse < last_pulse )
48
last_pulse = new_pulse;
49
if( new_pulse > last_pulse )
52
unsigned long interval = new_pulse - last_pulse;
56
Serial.print( last_pulse );
58
Serial.print( new_pulse );
61
// if( interval > 19000 && interval < 20500 )
63
// update interval buffer
64
intervals[ interval_idx ] = interval;
65
if( ++interval_idx >= SIGNAL_SAMPLES )
71
last_pulse = new_pulse;
75
if( interval_idx == 0 && got_pulse )
78
for( int a = 0; a < SIGNAL_SAMPLES; a++ ) {
79
Serial.print( intervals[ a ] );
80
if( a == SIGNAL_SAMPLES - 1 )