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