![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Diff: main.cpp
- Revision:
- 1:bdb889fd3bef
- Parent:
- 0:dba2bcefd284
- Child:
- 2:5628e75c533e
--- a/main.cpp Wed Aug 24 23:11:45 2011 +0000 +++ b/main.cpp Thu Aug 25 12:46:50 2011 +0000 @@ -1,3 +1,8 @@ +/* + * humidity1 + * + */ + #include "mbed.h" DigitalOut myled(LED1); @@ -5,30 +10,46 @@ AnalogIn hum(p20); AnalogIn ref(p19); -float av[5] = {0,0,0,0,0}; +int main() { + float av0 = 0.0; + float alpha = 0.5; -int main() { + printf("humidity1\n\r"); + while(1) { - float h = hum.read(); - float r = ref.read(); - float v = 5.0*h/r; + // read many times to remove noise + int n = 1000; + float h = 0.0; + float r = 0.0; + float v = 0.0; + for(int i=0; i<n; ++i) { + float h1, r1; + h1 = hum.read(); + r1 = ref.read(); + h += h1; + r += r1; + v += 5.0*h1/r1; // convert to 5v reference + } + h /= n; + r /= n; + v /= n; - av[0] = av[1]; - av[1] = av[2]; - av[2] = av[3]; - av[3] = av[4]; - av[4] = v; + // EMA + float av = av0 + alpha*(v-av0); + av0 = av; - float a = (av[0]+av[1]+av[2]+av[3]+av[4])/5.0; - - float m = (a-0.8)/3.0*100.0; + // scale to humidity% + float m = (av-0.8)/3.0*100.0; - printf("h=%1.2f r=%1.2f %1.2f %1.2f %3.1f\n\r", h, r, v, a, m ); + printf("h=%1.4f r=%1.4f %1.4fv %1.4fv %3.1f%%\n\r", h, r, v, av, m ); myled = 1; wait(0.2); myled = 0; wait(0.8); - } -} + + } // while + +} // main +