I have a hb100 doppler motion sensor (http://www.amazon.com/Wireless-Module-Doppler-Microwave-Motion/dp/B008P683RI) which I have built an operational amplifier and filter circuit to create 2 outputs: a freq out and a vout. I attached the freqout to an arduino and found code from another post (Doppler Speed Radar - Project Guidance - Arduino Forum) to show the data for both the frequency and the speed of an approaching or retreating object in the serial monitor of the arduino. I would like to take the data and average "x" number of samples for the output in the serial monitor(in other words smoothing) since the sensor gives readings every split seconds. Can someone point me in the right direction in going about doing this?
Some things to note:
- The freqout of the sensor is connected to digital pin 5 of the arduino uno.
- I will use the smoothed data to turn on and off certain leds.
- The code uses timers and interrupts to help determine the frequency.
Code:
const uint16_t TICK_CNT = 3; // 255-(16MHz/1024/62Hz)
static uint16_t freq = 0;
double sped = 0; //"speed" seems to be a reserved term
void setup() {
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
Serial.begin(115200);
noInterrupts(); // disable all interrupts while we configure
// init Timer1 - 16-bit timer/counter
TCNT1 = 0; // start count at zero.
TCCR1B |= _BV(CS12) | _BV(CS11) | _BV(CS10); // Increment T1 input on each positive edge
// using an external source, pg 139.
// init Timer2 - 8-bit timer/counter
TCNT2 = TICK_CNT; // preload Timer2 to interrupt every 250 ms
TIMSK2 = _BV(TOIE2); // enable the Timer2 overflow interrupt
TCCR2B |= _BV(CS22) |_BV(CS21) | _BV(CS20); // init clock prescaler to 1024, page 164.
interrupts(); // enable all interrupts
Serial.println("Ready...");
}
ISR(TIMER2_OVF_vect) {
//Serial.print("TCNT1: ");
//Serial.println(TCNT1);
freq = TCNT1;
//Serial.println(freq);
TCNT1 = 0; //starts count at zero again
TCNT2 = TICK_CNT; //reloads Timer2 to interrupt every 250ms
}
void loop()
{
if (freq != 0)
{
freq = freq * 62; // multiple the frequency * 4, 250ms*4 = 1 sec.
sped = freq * .03225; // multiplying freq * 0.03225 will give speed in mph, 31Hz = 1 mph.
Serial.print("Freq: ");
Serial.print(freq, DEC);
Serial.print(" Hz, Speed: ");
Serial.print(sped, 0);
Serial.println(" mph");
freq = 0;
}
}