dsadd

Dependencies:   mbed Sht31 MAX44009

Revision:
10:a9db57e6456e
Parent:
9:7c53c49afa40
Child:
11:3a63d6f65ff5
--- a/main.cpp	Fri Apr 24 13:33:28 2020 +0000
+++ b/main.cpp	Fri Apr 24 16:08:58 2020 +0000
@@ -11,7 +11,8 @@
 
 InterruptIn smthing_from_BLE(p12);
 DigitalOut BLE_Can_receive(p11); // 0 = can recieve
-
+AnalogIn input(p15);
+Timer t;
 class RGB{
     public :    int C;
                 int R;
@@ -44,7 +45,7 @@
 bool wrote=false;
 DigitalOut led(LED1);
 char    signal[1];
-//LocalFileSystem local("local"); //Create the local filesystem under the name "local"
+LocalFileSystem local("local"); //Create the local filesystem under the name "local"
 
  void letsee() {
             led=!led;
@@ -57,7 +58,8 @@
 
 int main()
 {   
-  //  set_time(1587699064); 
+   set_time(1587744159); 
+  
     led=1;
     //pc.read
     BLE_Can_receive = 0;
@@ -67,6 +69,31 @@
        
     while(1) { 
         counter = counter%10;
+        time_t seconds = time(NULL);
+        t.start();
+        float peakToPeak=0, signalMax=0, signalMin=1024;
+        float _sample;
+        int start = t.read_ms();
+        while (t.read_ms() - start < 50) {
+            _sample=input.read();
+            if (_sample>signalMax){
+                signalMax=_sample;
+                }
+            else if (_sample<signalMin){
+                signalMin=_sample;
+                }
+        } 
+        int end = t.read_ms();
+        
+    
+        t.reset();
+        peakToPeak = signalMax - signalMin;  // max - min = peak-peak amplitude
+    
+        float volts = peakToPeak * 0.707;  // convert to RMS voltage
+        float first = log10(volts/0.00631)*20;
+        float second = first + 94 - 44 - 25; //Gain == 11 board gain ==25
+      
+
         //time_t seconds = time(NULL);
         {
             int sensor_addr = 41 << 1;
@@ -202,24 +229,24 @@
             
         //pc.printf("test");
        // pc.printf("Shine Like:%s,%d,%d,%d,%d,%3.2f,%3.2f%,%0.001f\n",signal,(int)(RGBavg.C+0.5), (int)(RGBavg.R+0.5), (int)(RGBavg.G+0.5), (int)(RGBavg.B+0.5),THavg.t, THavg.h,luxavg);
-            //BLE_Can_receive = 0;
+            BLE_Can_receive = 0;
             counter++;
-           // if (counter == 10 &&_30Scounter==3){
-                //_30Scounter=0;
-               // FILE *fp = fopen("/local/out.txt", "a");   
-               //fprintf(fp, "[%d,%d,%d,%d,%d,%d,%d,%0.001f],\n",seconds,(int)(RGBavg.C+0.5),
-                       // (int)(RGBavg.R+0.5), (int)(RGBavg.G+0.5), (int)(RGBavg.B+0.5),(int)THavg.t,(int) THavg.h,luxavg);   
-               // fclose(fp); 
-               // allfilled = true; 
-           // }
-            //else 
+            if (counter == 10 &&_30Scounter==3){
+                _30Scounter=0;
+                FILE *fp = fopen("/local/out.txt", "a");   
+               fprintf(fp, "[%d,%d,%d,%d,%d,%d,%d,%0.001f,%0.001f],\n",seconds,(int)(RGBavg.C+0.5),
+                        (int)(RGBavg.R+0.5), (int)(RGBavg.G+0.5), (int)(RGBavg.B+0.5),(int)THavg.t,(int) THavg.h,luxavg,second);   
+                fclose(fp); 
+                allfilled = true; 
+            }
+            else 
             if (counter==10){
-               // _30Scounter++;
+                _30Scounter++;
                  allfilled = true; 
             }
-            //BLE_Can_receive = 1;
+            BLE_Can_receive = 1;
         if (wrote){
-           /* if (signal[0]=='s'){
+           if (signal[0]=='s'){
                 FILE *fp = fopen("/local/out.txt", "r"); 
                 char str[45];   
                  while(!feof(fp))
@@ -230,10 +257,10 @@
                  fclose(fp);
                 wrote=false;
             }
-            else */
+            else 
             {
                 sendCounter++;
-                pc.printf("%d,%d,%d,%d,%d,%3.2f,%3.2f%,%0.001f\n",sendCounter,(int)(RGBavg.C+0.5), (int)(RGBavg.R+0.5), (int)(RGBavg.G+0.5), (int)(RGBavg.B+0.5),THavg.t, THavg.h,luxavg);
+                pc.printf("%d,%d,%d,%d,%d,%3.2f,%3.2f%,%0.001f,%f\n",sendCounter,(int)(RGBavg.C+0.5), (int)(RGBavg.R+0.5), (int)(RGBavg.G+0.5), (int)(RGBavg.B+0.5),THavg.t, THavg.h,luxavg,second);
                 if (sendCounter == 10){ //10 = all element is filled {  
                     if (signal[0]=='i'){
                         pc.printf("end");