8
8
// microseconds. It takes several measurements and prints it the average over
11
// NOTES: Due to the way our receiver works (by varying the position of the
12
// rising edge of the pulse and keeping the falling edge fixed), it is
13
// sufficient for us to measure the pule width to determine the channel value
14
// (e.g., throttle position). But it should be noted that this relies on a
15
// quirk of our receiver/transmitter and it is not truly measuring PPM. Our
16
// next experiment is measuring PPM (../ppm-read).
18
13
// number of signal pulses to average
19
#define AVERAGE_SAMPLES 1
21
// should average be result, or damping?
22
#define AVERAGE_DAMP 1
26
#define GRAPH_MIN 1000
27
#define GRAPH_MAX 2000
14
#define SIGNAL_SAMPLES 10
31
17
// set to the time that the last signal pulse was at
53
39
Serial.begin( 9600 );
59
unsigned long last_pulse_off = 0;
60
unsigned long intervals[ AVERAGE_SAMPLES ];
44
unsigned long last_pulse = 0;
45
unsigned long intervals[ SIGNAL_SAMPLES ] = {0};
61
46
int interval_idx = 0;
64
for( int a = 0; a < AVERAGE_SAMPLES; a++ )
69
char graph[ GRAPH_SIZE + 3 ];
70
for( int a = 1; a < GRAPH_SIZE + 1; a++ )
73
graph[ GRAPH_SIZE + 1 ] = ']';
74
graph[ GRAPH_SIZE + 2 ] = 0;
79
50
// detect pulse falling-edge
80
unsigned long pulse_on = _new_pulse_on;
81
unsigned long pulse_off = _new_pulse_off;
51
unsigned long new_pulse_on = _new_pulse_on;
52
unsigned long new_pulse_off = _new_pulse_off;
82
53
bool got_pulse = false;
83
if( pulse_off > last_pulse_off )
54
if( new_pulse_off > last_pulse )
86
unsigned long interval = pulse_off - pulse_on;
87
if( interval > 800 && interval < 3000 )
89
// update interval buffer
90
intervals[ interval_idx ] = pulse_off - pulse_on;
91
if( ++interval_idx >= AVERAGE_SAMPLES )
97
last_pulse_off = pulse_off;
56
// update interval buffer
57
intervals[ interval_idx ] = new_pulse_off - new_pulse_on;
58
if( ++interval_idx >= SIGNAL_SAMPLES )
61
last_pulse = new_pulse_off;
100
65
// display average?
102
( AVERAGE_DAMP || interval_idx == 0 ) )
66
if( interval_idx == 0 && got_pulse )
104
68
// calculate average
105
unsigned long ave = 0;
106
for( int a = 0; a < AVERAGE_SAMPLES; a++ )
70
for( int a = 0; a < SIGNAL_SAMPLES; a++ )
107
71
ave += intervals[ a ];
108
ave /= AVERAGE_SAMPLES;
113
int pos = ( GRAPH_SIZE ) *
114
( ave - GRAPH_MIN ) / ( GRAPH_MAX - GRAPH_MIN );
115
graph[ pos + 1 ] = '|';
116
Serial.println( graph );
117
graph[ pos + 1 ] = '-';
72
ave /= SIGNAL_SAMPLES;
121
74
// tell it like it is
122
Serial.println( ave );
75
Serial.println( round( ave ) );