13
13
// number of signal pulses to average
14
#define SIGNAL_SAMPLES 10
14
#define AVERAGE_SAMPLES 1
16
// should average be result, or damping?
17
#define AVERAGE_DAMP 1
21
#define GRAPH_MIN 1000
22
#define GRAPH_MAX 2000
17
26
// set to the time that the last signal pulse was at
39
48
Serial.begin( 9600 );
44
unsigned long last_pulse = 0;
45
unsigned long intervals[ SIGNAL_SAMPLES ] = {0};
54
unsigned long last_pulse_off = 0;
55
unsigned long intervals[ AVERAGE_SAMPLES ];
46
56
int interval_idx = 0;
59
for( int a = 0; a < AVERAGE_SAMPLES; a++ )
64
char graph[ GRAPH_SIZE + 3 ];
65
for( int a = 1; a < GRAPH_SIZE + 1; a++ )
68
graph[ GRAPH_SIZE + 1 ] = ']';
69
graph[ GRAPH_SIZE + 2 ] = 0;
50
74
// detect pulse falling-edge
51
unsigned long new_pulse_on = _new_pulse_on;
52
unsigned long new_pulse_off = _new_pulse_off;
75
unsigned long pulse_on = _new_pulse_on;
76
unsigned long pulse_off = _new_pulse_off;
53
77
bool got_pulse = false;
54
if( new_pulse_off > last_pulse )
78
if( pulse_off > last_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;
81
unsigned long interval = pulse_off - pulse_on;
82
if( interval > 800 && interval < 3000 )
84
// update interval buffer
85
intervals[ interval_idx ] = pulse_off - pulse_on;
86
if( ++interval_idx >= AVERAGE_SAMPLES )
92
last_pulse_off = pulse_off;
65
95
// display average?
66
if( interval_idx == 0 && got_pulse )
97
( AVERAGE_DAMP || interval_idx == 0 ) )
68
99
// calculate average
70
for( int a = 0; a < SIGNAL_SAMPLES; a++ )
100
unsigned long ave = 0;
101
for( int a = 0; a < AVERAGE_SAMPLES; a++ )
71
102
ave += intervals[ a ];
72
ave /= SIGNAL_SAMPLES;
103
ave /= AVERAGE_SAMPLES;
108
int pos = ( GRAPH_SIZE ) *
109
( ave - GRAPH_MIN ) / ( GRAPH_MAX - GRAPH_MIN );
110
graph[ pos + 1 ] = '|';
111
Serial.println( graph );
112
graph[ pos + 1 ] = '-';
74
116
// tell it like it is
75
Serial.println( round( ave ) );
117
Serial.println( ave );