I'm trying to average a series of ADC readings for a given time .

23 Mar 2011

I'm trying to generate a series of pulses and during a window of approximately 2mSec. average the ADC. I'm new to both C programming and mbed. The segment of code goes as follows:

d_window =1; t.start(); while(t.read() <= t5) { avg = avg + ain; ++count; } avg= avg / count; signal = avg; t.stop();

What happens when I run the code is the while loop is bypassed. I assumed t.read() was larger than t5, so I try many values of t5. I also tryed t.read_us() with no luck. The examples for the timer looks simple enough, but they use a 'printf' command that I'm not using properly (?). Any help would be appreciated.

Dan Janssen

23 Mar 2011

Hi

I would first add a t.reset() before the t.start().

t.read() returns a floating point (so you need a t5 to be 0.002).

As for debugging: Simply add a printf of t.read() directly after the t.start() to see what the value is you're comparing with. It will ruin timing but you can see if the value if the timer is smaller or of the same magnitude than t5 when you start with and without the t.reset().

d_window=1;
t.reset(); 
t.start(); 
while(t.read()<=t5) { 
  avg=avg+ain; 
  ++count; 
} 
avg=avg/count; 
signal=avg; 
t.stop();
23 Mar 2011

Thanks,

This did the trick. It works well.

Dan Janssen