#define ECHO_TIMEOUT (CLOCK_HZ/20) // 50 ms
#define MEASUREMENT_WAIT (4*ECHO_TIMEOUT)
+#define MEASUREMENT_SHIFT 2 // running avg (1 << M_SHIFT)
#define N_TRIGGERS 3
#define N_SENSORS 12
to_start &= ~mask;
to_measure |= mask;
} else if ((to_measure & mask) && !(bits & mask)) {
+ uint16_t old_d, new_d;
+ uint8_t idx = trig*N_TRIG_SENSORS+i;
// echo end
to_measure &= ~mask;
- distances[trig*N_TRIG_SENSORS + i]
- = now - starttimes[i];
+ new_d = now - starttimes[i];
+ old_d = distances[idx];
+
+ if (old_d == 0
+ || old_d == -1) {
+ distances[idx] = new_d;
+ } else {
+ distances[idx] = (
+ (old_d << MEASUREMENT_SHIFT)
+ + new_d
+ - old_d
+ ) >> MEASUREMENT_SHIFT;
+ }
}
}
}