Threads

Dependencies:   mbed C12832 LM75B

Revision:
8:a27db43e9d85
Parent:
7:d20cc6a9060c
diff -r d20cc6a9060c -r a27db43e9d85 main.cpp
--- a/main.cpp	Sat Dec 05 15:57:53 2020 +0000
+++ b/main.cpp	Sat Dec 05 17:43:31 2020 +0000
@@ -5,56 +5,67 @@
 #include <stdio.h>
 #include <cstdlib>
 
+
 LM75B sensor(p28,p27);
 Serial pc(USBTX,USBRX);
-float TempV; //making a float variable for the temperature value 
-float cycles = 300;
+//float TempV = 0; //making a float variable for the temperature value 
+int cycles = 300;
 
 LocalFileSystem local("local"); // Create the local filesystem under the name "local"
+FILE* fp;
+int i = 0;
+int j = 0;
 
-DigitalOut led1(LED1);
-DigitalOut led2(LED2);
-Thread Writing_thread;
-Thread Read_Calc_thread;
+double num = 0;
+double total = 0;
+double maxtemp = -99.99;
+double mintemp = 99.99;
+
+Thread WritingThread;
+Thread ReadingThread;
 
-void Writing_thread()
+void Writing()
 {
-    while (true) {
-        fp = fopen("/local/temp3.csv", "a"); //Open the file for writing to    
-    
+    fp = fopen("/local/temp3.csv", "a"); //Open the file for writing to  
+ 
+    //TempV = (float)sensor; //Temperature is the sensor value    
+    if (sensor.open()) //Try to open the LM75B
+    {
+        printf("Device detected!\n\r");        
+    }    
+    else 
+    {
+        error("Device not detected!\n");
+    }//end if sensor open
+
     printf("Measuring temp... \n\r"); //Print confirmation of code running      
     while (i<cycles)
     {
-        TempV = (float)sensor; //Temperature is the sensor value 
-
-        fprintf(fp, "%.2f\n", TempV); //print values to file 
+        printf("Test");
+                                //TempV
+        fprintf(fp, "%.2f\n", (float)sensor.temp()); //print values to file 
 
         i = i+1; // counter
         wait(1); //Wait 1 seconds to 1*300s = 5 minutes         
             
-    } //end while loop for writing function
+    } end while loop for writing function
+    
     fclose (fp);//close the file 
-        //ThisThread::sleep_for(500);
-        //Set trigger to end thread 
-    }
+
+    j=1;  
 }
 
-void Read_Calc_thread()
+void Reading()
 {
-    while (true) 
+    while (j!=1)
     {
+        //wait for thread one
+    }
     
-    char temps[5]; //Create a string that will contain temerature values from file
-
-        
     fp = fopen("/local/temp3.csv", "r"); //Open rfile for reading 
-            
-    //min max total
-    double num = 0;
-    double total = 0;
-    double maxtemp = -99.99;
-    double mintemp = 99.99;
-            
+ 
+    char temps[5]; //Create a string that will contain temerature values from file          
+    //min max total            
         while (fscanf(fp, "%s", temps)!= EOF) //scan to end of file
         {          
             num = atof(temps); //string to number --> https://os.mbed.com/questions/7171/How-to-convert-String-to-Float-value/
@@ -70,55 +81,34 @@
             }
                 
             total = total+ num;
-
             //ThisThread::sleep_for(500);
             //Set trigger to end thread
-        }   
-    }
+        }  
+        j=2;
 }
 
 int main () //Main function ***************************************************************************************
 {
-    FILE *fp = fopen("/local/temp3.csv", "a");  //Create the file
-    fclose (fp);                                //Close the file
-
-// Variables 
-    int i =0;
-//    int j =0;
-
+    WritingThread.start(Writing); //Start first thread
+    
+    //ReadingThread.start(Reading); //Start second thread 
+    WritingThread.join();
+    //while(j!=1)
+    //{
+        
+    //}
+    printf("left Thread");
     
-//while (j<288) //144 5 min cycles in 24 hours *** Main while loop for 3 functions, Writing, reading, displaying 
     
-    if (sensor.open()) //Try to open the LM75B
-    {
-        printf("Device detected!\n\r");        
-    }    
-    else 
-    {
-        error("Device not detected!\n");
-    }//end if sensor open
+    double avg = total/(cycles);
+        
+    printf("Average: %.2f \n\r", avg);
+    printf("Max: %.2f \n\r", maxtemp);
+    printf("Min: %.2f \n\r", mintemp);
+        
+    fclose(fp); // close file
+    
+    //j=j+1 // Controls daily cycle 
 
 
-    thread.start(Writing_thread); //Start first thread
-    
-    thread.start(Read_Calc_thread(); //Start second thread 
-
-
-    
-//Display function 
-    } //while loop creating sting of values from file ends
-        double avg = total/(cycles);
-        
-        printf("Average: %.2f \n\r", avg);
-        printf("Max: %.2f \n\r", maxtemp);
-        printf("Min: %.2f \n\r", mintemp);
-        
-        fclose(fp); // close file
-    
-    //j=j+1 // Controls daily cycle      
-
-   
-
-}   //end main
-
-
+}   //end main
\ No newline at end of file