Threads

Dependencies:   mbed C12832 LM75B

Committer:
ciaranom
Date:
Sat Dec 05 15:57:53 2020 +0000
Revision:
7:d20cc6a9060c
Parent:
6:e883d7b9c790
Child:
8:a27db43e9d85
Threads

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okano 0:ce7a8546502b 1 #include "mbed.h"
chris 2:9e757151de9b 2 #include "LM75B.h"
ciaranom 7:d20cc6a9060c 3 #include <string>
ciaranom 7:d20cc6a9060c 4 #include <iostream>
ciaranom 7:d20cc6a9060c 5 #include <stdio.h>
ciaranom 7:d20cc6a9060c 6 #include <cstdlib>
chris 5:608f2bf4d3f7 7
chris 4:6df97cb10041 8 LM75B sensor(p28,p27);
chris 4:6df97cb10041 9 Serial pc(USBTX,USBRX);
ciaranom 7:d20cc6a9060c 10 float TempV; //making a float variable for the temperature value
ciaranom 7:d20cc6a9060c 11 float cycles = 300;
okano 0:ce7a8546502b 12
ciaranom 7:d20cc6a9060c 13 LocalFileSystem local("local"); // Create the local filesystem under the name "local"
ciaranom 6:e883d7b9c790 14
ciaranom 7:d20cc6a9060c 15 DigitalOut led1(LED1);
ciaranom 7:d20cc6a9060c 16 DigitalOut led2(LED2);
ciaranom 7:d20cc6a9060c 17 Thread Writing_thread;
ciaranom 7:d20cc6a9060c 18 Thread Read_Calc_thread;
ciaranom 6:e883d7b9c790 19
ciaranom 7:d20cc6a9060c 20 void Writing_thread()
okano 0:ce7a8546502b 21 {
ciaranom 7:d20cc6a9060c 22 while (true) {
ciaranom 7:d20cc6a9060c 23 fp = fopen("/local/temp3.csv", "a"); //Open the file for writing to
ciaranom 7:d20cc6a9060c 24
ciaranom 7:d20cc6a9060c 25 printf("Measuring temp... \n\r"); //Print confirmation of code running
ciaranom 7:d20cc6a9060c 26 while (i<cycles)
ciaranom 7:d20cc6a9060c 27 {
ciaranom 7:d20cc6a9060c 28 TempV = (float)sensor; //Temperature is the sensor value
ciaranom 7:d20cc6a9060c 29
ciaranom 7:d20cc6a9060c 30 fprintf(fp, "%.2f\n", TempV); //print values to file
ciaranom 7:d20cc6a9060c 31
ciaranom 7:d20cc6a9060c 32 i = i+1; // counter
ciaranom 7:d20cc6a9060c 33 wait(1); //Wait 1 seconds to 1*300s = 5 minutes
ciaranom 7:d20cc6a9060c 34
ciaranom 7:d20cc6a9060c 35 } //end while loop for writing function
ciaranom 7:d20cc6a9060c 36 fclose (fp);//close the file
ciaranom 7:d20cc6a9060c 37 //ThisThread::sleep_for(500);
ciaranom 7:d20cc6a9060c 38 //Set trigger to end thread
ciaranom 7:d20cc6a9060c 39 }
ciaranom 7:d20cc6a9060c 40 }
ciaranom 7:d20cc6a9060c 41
ciaranom 7:d20cc6a9060c 42 void Read_Calc_thread()
ciaranom 7:d20cc6a9060c 43 {
ciaranom 7:d20cc6a9060c 44 while (true)
ciaranom 6:e883d7b9c790 45 {
ciaranom 7:d20cc6a9060c 46
ciaranom 7:d20cc6a9060c 47 char temps[5]; //Create a string that will contain temerature values from file
ciaranom 7:d20cc6a9060c 48
ciaranom 7:d20cc6a9060c 49
ciaranom 7:d20cc6a9060c 50 fp = fopen("/local/temp3.csv", "r"); //Open rfile for reading
ciaranom 7:d20cc6a9060c 51
ciaranom 7:d20cc6a9060c 52 //min max total
ciaranom 7:d20cc6a9060c 53 double num = 0;
ciaranom 7:d20cc6a9060c 54 double total = 0;
ciaranom 7:d20cc6a9060c 55 double maxtemp = -99.99;
ciaranom 7:d20cc6a9060c 56 double mintemp = 99.99;
ciaranom 7:d20cc6a9060c 57
ciaranom 7:d20cc6a9060c 58 while (fscanf(fp, "%s", temps)!= EOF) //scan to end of file
ciaranom 7:d20cc6a9060c 59 {
ciaranom 7:d20cc6a9060c 60 num = atof(temps); //string to number --> https://os.mbed.com/questions/7171/How-to-convert-String-to-Float-value/
ciaranom 7:d20cc6a9060c 61
ciaranom 7:d20cc6a9060c 62 if(num > maxtemp) //Calculating max number
ciaranom 7:d20cc6a9060c 63 {
ciaranom 7:d20cc6a9060c 64 maxtemp = num;
ciaranom 7:d20cc6a9060c 65 }
ciaranom 7:d20cc6a9060c 66
ciaranom 7:d20cc6a9060c 67 if(num < mintemp) //Calculating min number
ciaranom 7:d20cc6a9060c 68 {
ciaranom 7:d20cc6a9060c 69 mintemp = num;
ciaranom 7:d20cc6a9060c 70 }
ciaranom 7:d20cc6a9060c 71
ciaranom 7:d20cc6a9060c 72 total = total+ num;
ciaranom 7:d20cc6a9060c 73
ciaranom 7:d20cc6a9060c 74 //ThisThread::sleep_for(500);
ciaranom 7:d20cc6a9060c 75 //Set trigger to end thread
ciaranom 7:d20cc6a9060c 76 }
ciaranom 7:d20cc6a9060c 77 }
ciaranom 7:d20cc6a9060c 78 }
ciaranom 7:d20cc6a9060c 79
ciaranom 7:d20cc6a9060c 80 int main () //Main function ***************************************************************************************
ciaranom 7:d20cc6a9060c 81 {
ciaranom 7:d20cc6a9060c 82 FILE *fp = fopen("/local/temp3.csv", "a"); //Create the file
ciaranom 7:d20cc6a9060c 83 fclose (fp); //Close the file
ciaranom 7:d20cc6a9060c 84
ciaranom 7:d20cc6a9060c 85 // Variables
ciaranom 7:d20cc6a9060c 86 int i =0;
ciaranom 7:d20cc6a9060c 87 // int j =0;
ciaranom 7:d20cc6a9060c 88
ciaranom 7:d20cc6a9060c 89
ciaranom 7:d20cc6a9060c 90 //while (j<288) //144 5 min cycles in 24 hours *** Main while loop for 3 functions, Writing, reading, displaying
ciaranom 7:d20cc6a9060c 91
ciaranom 7:d20cc6a9060c 92 if (sensor.open()) //Try to open the LM75B
ciaranom 7:d20cc6a9060c 93 {
ciaranom 7:d20cc6a9060c 94 printf("Device detected!\n\r");
ciaranom 7:d20cc6a9060c 95 }
ciaranom 7:d20cc6a9060c 96 else
ciaranom 6:e883d7b9c790 97 {
ciaranom 6:e883d7b9c790 98 error("Device not detected!\n");
ciaranom 6:e883d7b9c790 99 }//end if sensor open
ciaranom 7:d20cc6a9060c 100
chris 4:6df97cb10041 101
ciaranom 7:d20cc6a9060c 102 thread.start(Writing_thread); //Start first thread
ciaranom 7:d20cc6a9060c 103
ciaranom 7:d20cc6a9060c 104 thread.start(Read_Calc_thread(); //Start second thread
okano 0:ce7a8546502b 105
ciaranom 6:e883d7b9c790 106
ciaranom 7:d20cc6a9060c 107
ciaranom 7:d20cc6a9060c 108 //Display function
ciaranom 7:d20cc6a9060c 109 } //while loop creating sting of values from file ends
ciaranom 7:d20cc6a9060c 110 double avg = total/(cycles);
ciaranom 7:d20cc6a9060c 111
ciaranom 7:d20cc6a9060c 112 printf("Average: %.2f \n\r", avg);
ciaranom 7:d20cc6a9060c 113 printf("Max: %.2f \n\r", maxtemp);
ciaranom 7:d20cc6a9060c 114 printf("Min: %.2f \n\r", mintemp);
ciaranom 7:d20cc6a9060c 115
ciaranom 7:d20cc6a9060c 116 fclose(fp); // close file
ciaranom 7:d20cc6a9060c 117
ciaranom 7:d20cc6a9060c 118 //j=j+1 // Controls daily cycle
ciaranom 7:d20cc6a9060c 119
ciaranom 7:d20cc6a9060c 120
ciaranom 7:d20cc6a9060c 121
ciaranom 7:d20cc6a9060c 122 } //end main
ciaranom 7:d20cc6a9060c 123
ciaranom 7:d20cc6a9060c 124