Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
main.cpp
00001 #include "mbed.h" 00002 #include "co2.h" 00003 00004 AnalogIn flowIn(A3); 00005 AnalogIn temp(A2); 00006 Timer t; 00007 Thread co2Thread; 00008 Thread flowThread; 00009 Thread tempThread; 00010 00011 float flowVal; 00012 00013 void flow(){ 00014 while(1){ 00015 flowVal=3.3*flowIn; //Logic level 3.3 00016 // printf("Flow: %f", flowVal); 00017 } 00018 } 00019 00020 float t2Cel; 00021 00022 void getTemp(){ 00023 while(1){ 00024 float B = 3478; //Define thermistor constant 00025 float rRef=10e3; // Define reference resistance 00026 float r1=10e3; // Define thermistor resistance at 25 C 00027 float t1=25+273; // Define thermistor initial temperature s 25C in Kelvin 00028 float x = temp.read(); //Measure input voltage at pin A0 in bits 00029 float v = 3.3*x; //Convert bits into voltage 00030 float r2 = (3.3*rRef/v)-rRef; //Convert voltage into thermistor resistance 00031 float t2 = (B*t1)/(B-t1*log(r1/r2)); //Convert thermistor resistance into temperature in Kelvin (log means natural logarithm ln) 00032 t2Cel = t2-273; //Convert temperature from Kelvin to Celcius 00033 // printf("Temp: %f\n", t2Cel); 00034 } 00035 } 00036 00037 00038 00039 int main() 00040 { 00041 00042 int myArray[9]; 00043 long double var=0.0; 00044 float sigma=0.0; 00045 unsigned int sum=0; 00046 int difSum=0; 00047 int avg=0; 00048 unsigned long long sqSum=0; 00049 bool flag=false; 00050 00051 flowThread.start(flow); 00052 tempThread.start(getTemp); 00053 carbon(); 00054 t.start(); 00055 00056 // while(1){ 00057 // printf("CO2: %d, Flow: %f, Temp: %f\n", carbon(), flowVal, t2Cel); //COOL TERM 00058 //// printf("$d %f %f;", carbon(), flowVal, t2Cel); //Serial plotter 00059 // } 00060 00061 //SOLENOID PART// 00062 while(1){ 00063 int array[9]={0,0,0,0,0,0,0,0,0}; 00064 for(int i=0;i<=8;i++){ 00065 array[i]=carbon(); 00066 } 00067 //printf("%d %d %d %d %d %d %d %d %d\n", array[0], array[1], array[2], array[3], array[4], array[5], array[6], array[7], array[8]); 00068 00069 } 00070 00071 for(int i=0;i<9;i++){ //Get first 9 carbon values 00072 myArray[i]=carbon(); 00073 printf("%d ", myArray[i]); 00074 } 00075 00076 printf("\n"); 00077 00078 while(flag!=true){ 00079 for(int i=0;i<9;i++){ 00080 sum+=myArray[i]; //Find sum 00081 } 00082 00083 avg=sum/9;//find average 00084 00085 printf("Avg: %d\n", avg); 00086 00087 for(int i=0;i<9;i++){ 00088 difSum+=(myArray[i]-avg)*(myArray[i]-avg); //Find sum of difference between value X and mean 00089 } 00090 00091 var=difSum/9; 00092 sigma=sqrt(var); 00093 00094 printf("Standard dev: %f\n", sigma); 00095 00096 if(sigma>=0.5){ //Check against carbon plateau, if sd greater than plateau startPoint 00097 for(int x=0;x<8;x++){ 00098 myArray[x]=myArray[x+1]; //Shift all carbon to left by 1 00099 printf("%d ", myArray[x]); 00100 } 00101 00102 myArray[8]=carbon(); // get new carbon value in last array slot 00103 printf("%d\n", myArray[8]); 00104 00105 sum=0; //Reset variables 00106 sqSum=0; 00107 var=0; 00108 sigma=0; 00109 avg=0; 00110 difSum=0; 00111 00112 }else{ 00113 printf("TURN ON SOLENOID\n"); //Turn on second sensor 00114 flag=true; //Terminate loop because sd is at plateau startPoint 00115 } 00116 } 00117 00118 }
Generated on Sun Jul 31 2022 08:45:37 by
1.7.2