13
// number of signal pulses to average
14
#define SIGNAL_SAMPLES 10
13
// number of signal pulses to average (for damping)
14
#define DAMPING_SAMPLES 1
17
17
// set to the time that the last signal pulse was at
44
unsigned long last_pulse = 0;
45
unsigned long intervals[ SIGNAL_SAMPLES ] = {0};
44
unsigned long last_pulse_off = 0;
45
unsigned long intervals[ DAMPING_SAMPLES ] = {0};
46
46
int interval_idx = 0;
50
50
// detect pulse falling-edge
51
unsigned long new_pulse_on = _new_pulse_on;
52
unsigned long new_pulse_off = _new_pulse_off;
51
unsigned long pulse_on = _new_pulse_on;
52
unsigned long pulse_off = _new_pulse_off;
53
53
bool got_pulse = false;
54
if( new_pulse_off > last_pulse )
54
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;
57
unsigned long interval = pulse_off - pulse_on;
58
if( interval > 800 && interval < 3000 )
60
// update interval buffer
61
intervals[ interval_idx ] = pulse_off - pulse_on;
62
if( ++interval_idx >= DAMPING_SAMPLES )
68
last_pulse_off = pulse_off;
65
71
// display average?
66
if( interval_idx == 0 && got_pulse )
68
74
// calculate average
70
for( int a = 0; a < SIGNAL_SAMPLES; a++ )
75
unsigned long ave = 0;
76
for( int a = 0; a < DAMPING_SAMPLES; a++ )
71
77
ave += intervals[ a ];
72
ave /= SIGNAL_SAMPLES;
78
ave /= DAMPING_SAMPLES;
74
80
// tell it like it is
75
Serial.println( round( ave ) );
82
Serial.print( "\r\n" );