![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
main.cpp@1:bdb889fd3bef, 2011-08-25 (annotated)
- Committer:
- gbeardall
- Date:
- Thu Aug 25 12:46:50 2011 +0000
- Revision:
- 1:bdb889fd3bef
- Parent:
- 0:dba2bcefd284
- Child:
- 2:5628e75c533e
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gbeardall | 1:bdb889fd3bef | 1 | /* |
gbeardall | 1:bdb889fd3bef | 2 | * humidity1 |
gbeardall | 1:bdb889fd3bef | 3 | * |
gbeardall | 1:bdb889fd3bef | 4 | */ |
gbeardall | 1:bdb889fd3bef | 5 | |
gbeardall | 0:dba2bcefd284 | 6 | #include "mbed.h" |
gbeardall | 0:dba2bcefd284 | 7 | |
gbeardall | 0:dba2bcefd284 | 8 | DigitalOut myled(LED1); |
gbeardall | 0:dba2bcefd284 | 9 | |
gbeardall | 0:dba2bcefd284 | 10 | AnalogIn hum(p20); |
gbeardall | 0:dba2bcefd284 | 11 | AnalogIn ref(p19); |
gbeardall | 0:dba2bcefd284 | 12 | |
gbeardall | 1:bdb889fd3bef | 13 | int main() { |
gbeardall | 1:bdb889fd3bef | 14 | float av0 = 0.0; |
gbeardall | 1:bdb889fd3bef | 15 | float alpha = 0.5; |
gbeardall | 0:dba2bcefd284 | 16 | |
gbeardall | 1:bdb889fd3bef | 17 | printf("humidity1\n\r"); |
gbeardall | 1:bdb889fd3bef | 18 | |
gbeardall | 0:dba2bcefd284 | 19 | while(1) { |
gbeardall | 0:dba2bcefd284 | 20 | |
gbeardall | 1:bdb889fd3bef | 21 | // read many times to remove noise |
gbeardall | 1:bdb889fd3bef | 22 | int n = 1000; |
gbeardall | 1:bdb889fd3bef | 23 | float h = 0.0; |
gbeardall | 1:bdb889fd3bef | 24 | float r = 0.0; |
gbeardall | 1:bdb889fd3bef | 25 | float v = 0.0; |
gbeardall | 1:bdb889fd3bef | 26 | for(int i=0; i<n; ++i) { |
gbeardall | 1:bdb889fd3bef | 27 | float h1, r1; |
gbeardall | 1:bdb889fd3bef | 28 | h1 = hum.read(); |
gbeardall | 1:bdb889fd3bef | 29 | r1 = ref.read(); |
gbeardall | 1:bdb889fd3bef | 30 | h += h1; |
gbeardall | 1:bdb889fd3bef | 31 | r += r1; |
gbeardall | 1:bdb889fd3bef | 32 | v += 5.0*h1/r1; // convert to 5v reference |
gbeardall | 1:bdb889fd3bef | 33 | } |
gbeardall | 1:bdb889fd3bef | 34 | h /= n; |
gbeardall | 1:bdb889fd3bef | 35 | r /= n; |
gbeardall | 1:bdb889fd3bef | 36 | v /= n; |
gbeardall | 0:dba2bcefd284 | 37 | |
gbeardall | 1:bdb889fd3bef | 38 | // EMA |
gbeardall | 1:bdb889fd3bef | 39 | float av = av0 + alpha*(v-av0); |
gbeardall | 1:bdb889fd3bef | 40 | av0 = av; |
gbeardall | 0:dba2bcefd284 | 41 | |
gbeardall | 1:bdb889fd3bef | 42 | // scale to humidity% |
gbeardall | 1:bdb889fd3bef | 43 | float m = (av-0.8)/3.0*100.0; |
gbeardall | 0:dba2bcefd284 | 44 | |
gbeardall | 1:bdb889fd3bef | 45 | printf("h=%1.4f r=%1.4f %1.4fv %1.4fv %3.1f%%\n\r", h, r, v, av, m ); |
gbeardall | 0:dba2bcefd284 | 46 | |
gbeardall | 0:dba2bcefd284 | 47 | myled = 1; |
gbeardall | 0:dba2bcefd284 | 48 | wait(0.2); |
gbeardall | 0:dba2bcefd284 | 49 | myled = 0; |
gbeardall | 0:dba2bcefd284 | 50 | wait(0.8); |
gbeardall | 1:bdb889fd3bef | 51 | |
gbeardall | 1:bdb889fd3bef | 52 | } // while |
gbeardall | 1:bdb889fd3bef | 53 | |
gbeardall | 1:bdb889fd3bef | 54 | } // main |
gbeardall | 1:bdb889fd3bef | 55 |