gavin beardall / Mbed 2 deprecated humidity1

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * humidity1
00003  *
00004  */
00005 
00006 /*
00007 +5 --------
00008     |
00009     |
00010    +-+
00011    | |      |\
00012    | |->----|+\
00013    | |      |  \--+----+------
00014    +-+   +- |-/   |    |
00015     |    |  |/    |   +-+
00016     |    +--------+   | |
00017 0v ------             +_+
00018                        |--------
00019                       +-+
00020                       | |
00021                       +-+
00022                        |
00023 
00024 
00025 
00026 */ 
00027 
00028 #include "mbed.h"
00029 
00030 DigitalOut myled(LED1);
00031 
00032 AnalogIn hum(p20);
00033 AnalogIn ref(p19);
00034 AnalogIn tst(p18);
00035 
00036 //Timer timer;
00037 
00038 
00039 int main() {
00040     float av0 = 0.0;
00041     float alpha = 0.2;
00042 
00043     printf("\rhumidity1: %u %u\n\r", sizeof(unsigned long long), sizeof(uint64_t) );
00044 
00045     // decimation and downsamplig to reduce noise and increment resolution
00046 
00047     while(1) {
00048         
00049         //   32 - ? 8081
00050         //   64 - 8 8082
00051         //  128 - 6 807d
00052         //  256 - 4 8071
00053         //  512 - 2 805e
00054         // 1024 - .3 803f
00055         // 2048 - ? 8014
00056         // 4096 - ? 7fe8
00057         // 8192 - ? 7fca
00058         // 16384 - ? 7fba
00059         // 32768 - ? 7fb2
00060         // 65536 - ? 7faf
00061         // 131072 - ? 7fad
00062         
00063         // Sample values and their differences
00064         // 7FB7 7FC7 7FD7 7FE7 7FF7 8008 8018 8028
00065         //    10   10   10   10   11   10   10
00066         
00067         // Max conversion rate: 14KHz (~70us)
00068         // ToDo        
00069         // many samples to create new data set
00070         // different sampling frequencies, see if effect same
00071         //   measure raw sampling freq
00072         // fourier analysis
00073         // freq distribution
00074         // overall and diff sized moving avg to see if effect is due to software.overflow or noise in data
00075         // what does graph do at even larger sample sizes
00076         // is there a temp drift?
00077         
00078         
00079         //unsigned long long t = 0; // uint64_t
00080         uint64_t t = 0;
00081         uint64_t n = 1000; // 128/4096
00082         //timer.start();
00083         //float b1 = timer.read();
00084         //int begin = timer.read_ms();
00085         for(int i=0; i<n; ++i) {
00086             //uint16_t v = tst.read_u16();
00087             //uint64_t v = tst.read_u16();
00088             uint64_t v = ref.read_u16();
00089             //v >>= 4; // back to 12bits
00090             
00091             t += v;
00092 #if 0            
00093             switch((v>>4)&0x01) {
00094             case 0: wait_us(7); break;
00095             case 1: wait_us(17); break;
00096             default: printf("?");
00097             } // switch
00098 #endif            
00099         } // for 
00100         //int end = timer.read_ms();
00101         //float e1 = timer.read();
00102         //printf("timer: %d %f %f\n\r", end-begin, e1-b1, (float)n*1000.0/(float)(end-begin) );
00103 
00104         uint64_t x = t / n;
00105         //t = t/(float)n;
00106         
00107         printf("%llu\n\r", x);
00108         //printf("%.0f\n\r", t);
00109         
00110         //wait(4); // <<<<<<<<
00111         
00112         //wait_us(31);
00113     }
00114 
00115     while(1) {
00116     
00117         // read many times to remove noise
00118         int n = 1000;
00119         float h = 0.0;
00120         float r = 0.0;
00121         float v = 0.0;
00122         for(int i=0; i<n; ++i) {
00123             float h1, r1;
00124             h1 = hum.read();
00125             r1 = ref.read();
00126             h += h1;
00127             r += r1;
00128             //v += 5.0*h1/r1; // convert to 5v reference
00129             wait_us(100);
00130         }
00131         h /= n;
00132         r /= n;
00133         //v /= n;
00134         
00135         v = 5.0*h/r; // convert to 5v reference
00136 
00137         
00138         // Exponential Moving Average
00139         float av = av0 + alpha*(v-av0);
00140         av0 = av;
00141         
00142         // scale to humidity%
00143         float m = (av-0.8)/3.0*100.0;
00144                 
00145         printf("h=%1.4f r=%1.4f %1.4fv %1.4fv %3.2f%%\n\r", h, r, v, av, m );
00146     
00147         myled = 1;
00148         wait(0.2);
00149         myled = 0;
00150         wait(0.8);
00151         
00152     } // while
00153     
00154 } // main
00155