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@1:af991edb6dc1, 2019-07-04 (annotated)
- Committer:
- christodoulos
- Date:
- Thu Jul 04 09:09:54 2019 +0000
- Revision:
- 1:af991edb6dc1
- Parent:
- 0:958e045ea7d1
Handheld with solenoid
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| christodoulos | 0:958e045ea7d1 | 1 | #include "mbed.h" |
| christodoulos | 0:958e045ea7d1 | 2 | #include "co2.h" |
| christodoulos | 0:958e045ea7d1 | 3 | |
| christodoulos | 1:af991edb6dc1 | 4 | AnalogIn flowIn(A3); |
| christodoulos | 0:958e045ea7d1 | 5 | AnalogIn temp(A2); |
| christodoulos | 0:958e045ea7d1 | 6 | Timer t; |
| christodoulos | 0:958e045ea7d1 | 7 | Thread co2Thread; |
| christodoulos | 0:958e045ea7d1 | 8 | Thread flowThread; |
| christodoulos | 0:958e045ea7d1 | 9 | Thread tempThread; |
| christodoulos | 0:958e045ea7d1 | 10 | |
| christodoulos | 0:958e045ea7d1 | 11 | float flowVal; |
| christodoulos | 0:958e045ea7d1 | 12 | |
| christodoulos | 0:958e045ea7d1 | 13 | void flow(){ |
| christodoulos | 0:958e045ea7d1 | 14 | while(1){ |
| christodoulos | 0:958e045ea7d1 | 15 | flowVal=3.3*flowIn; //Logic level 3.3 |
| christodoulos | 0:958e045ea7d1 | 16 | // printf("Flow: %f", flowVal); |
| christodoulos | 0:958e045ea7d1 | 17 | } |
| christodoulos | 0:958e045ea7d1 | 18 | } |
| christodoulos | 0:958e045ea7d1 | 19 | |
| christodoulos | 0:958e045ea7d1 | 20 | float t2Cel; |
| christodoulos | 0:958e045ea7d1 | 21 | |
| christodoulos | 0:958e045ea7d1 | 22 | void getTemp(){ |
| christodoulos | 0:958e045ea7d1 | 23 | while(1){ |
| christodoulos | 0:958e045ea7d1 | 24 | float B = 3478; //Define thermistor constant |
| christodoulos | 0:958e045ea7d1 | 25 | float rRef=10e3; // Define reference resistance |
| christodoulos | 0:958e045ea7d1 | 26 | float r1=10e3; // Define thermistor resistance at 25 C |
| christodoulos | 0:958e045ea7d1 | 27 | float t1=25+273; // Define thermistor initial temperature s 25C in Kelvin |
| christodoulos | 0:958e045ea7d1 | 28 | float x = temp.read(); //Measure input voltage at pin A0 in bits |
| christodoulos | 0:958e045ea7d1 | 29 | float v = 3.3*x; //Convert bits into voltage |
| christodoulos | 0:958e045ea7d1 | 30 | float r2 = (3.3*rRef/v)-rRef; //Convert voltage into thermistor resistance |
| christodoulos | 0:958e045ea7d1 | 31 | float t2 = (B*t1)/(B-t1*log(r1/r2)); //Convert thermistor resistance into temperature in Kelvin (log means natural logarithm ln) |
| christodoulos | 0:958e045ea7d1 | 32 | t2Cel = t2-273; //Convert temperature from Kelvin to Celcius |
| christodoulos | 0:958e045ea7d1 | 33 | // printf("Temp: %f\n", t2Cel); |
| christodoulos | 0:958e045ea7d1 | 34 | } |
| christodoulos | 0:958e045ea7d1 | 35 | } |
| christodoulos | 0:958e045ea7d1 | 36 | |
| christodoulos | 0:958e045ea7d1 | 37 | |
| christodoulos | 0:958e045ea7d1 | 38 | |
| christodoulos | 0:958e045ea7d1 | 39 | int main() |
| christodoulos | 0:958e045ea7d1 | 40 | { |
| christodoulos | 0:958e045ea7d1 | 41 | |
| christodoulos | 0:958e045ea7d1 | 42 | int myArray[9]; |
| christodoulos | 0:958e045ea7d1 | 43 | long double var=0.0; |
| christodoulos | 0:958e045ea7d1 | 44 | float sigma=0.0; |
| christodoulos | 0:958e045ea7d1 | 45 | unsigned int sum=0; |
| christodoulos | 0:958e045ea7d1 | 46 | int difSum=0; |
| christodoulos | 0:958e045ea7d1 | 47 | int avg=0; |
| christodoulos | 0:958e045ea7d1 | 48 | unsigned long long sqSum=0; |
| christodoulos | 0:958e045ea7d1 | 49 | bool flag=false; |
| christodoulos | 0:958e045ea7d1 | 50 | |
| christodoulos | 0:958e045ea7d1 | 51 | flowThread.start(flow); |
| christodoulos | 0:958e045ea7d1 | 52 | tempThread.start(getTemp); |
| christodoulos | 0:958e045ea7d1 | 53 | carbon(); |
| christodoulos | 0:958e045ea7d1 | 54 | t.start(); |
| christodoulos | 0:958e045ea7d1 | 55 | |
| christodoulos | 1:af991edb6dc1 | 56 | // while(1){ |
| christodoulos | 1:af991edb6dc1 | 57 | // printf("CO2: %d, Flow: %f, Temp: %f\n", carbon(), flowVal, t2Cel); //COOL TERM |
| christodoulos | 1:af991edb6dc1 | 58 | //// printf("$d %f %f;", carbon(), flowVal, t2Cel); //Serial plotter |
| christodoulos | 1:af991edb6dc1 | 59 | // } |
| christodoulos | 1:af991edb6dc1 | 60 | |
| christodoulos | 1:af991edb6dc1 | 61 | //SOLENOID PART// |
| christodoulos | 1:af991edb6dc1 | 62 | while(1){ |
| christodoulos | 1:af991edb6dc1 | 63 | int array[9]={0,0,0,0,0,0,0,0,0}; |
| christodoulos | 1:af991edb6dc1 | 64 | for(int i=0;i<=8;i++){ |
| christodoulos | 1:af991edb6dc1 | 65 | array[i]=carbon(); |
| christodoulos | 1:af991edb6dc1 | 66 | } |
| christodoulos | 1:af991edb6dc1 | 67 | //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]); |
| christodoulos | 1:af991edb6dc1 | 68 | |
| christodoulos | 1:af991edb6dc1 | 69 | } |
| christodoulos | 1:af991edb6dc1 | 70 | |
| christodoulos | 1:af991edb6dc1 | 71 | for(int i=0;i<9;i++){ //Get first 9 carbon values |
| christodoulos | 1:af991edb6dc1 | 72 | myArray[i]=carbon(); |
| christodoulos | 1:af991edb6dc1 | 73 | printf("%d ", myArray[i]); |
| christodoulos | 1:af991edb6dc1 | 74 | } |
| christodoulos | 1:af991edb6dc1 | 75 | |
| christodoulos | 1:af991edb6dc1 | 76 | printf("\n"); |
| christodoulos | 1:af991edb6dc1 | 77 | |
| christodoulos | 1:af991edb6dc1 | 78 | while(flag!=true){ |
| christodoulos | 1:af991edb6dc1 | 79 | for(int i=0;i<9;i++){ |
| christodoulos | 1:af991edb6dc1 | 80 | sum+=myArray[i]; //Find sum |
| christodoulos | 0:958e045ea7d1 | 81 | } |
| christodoulos | 0:958e045ea7d1 | 82 | |
| christodoulos | 1:af991edb6dc1 | 83 | avg=sum/9;//find average |
| christodoulos | 0:958e045ea7d1 | 84 | |
| christodoulos | 1:af991edb6dc1 | 85 | printf("Avg: %d\n", avg); |
| christodoulos | 1:af991edb6dc1 | 86 | |
| christodoulos | 1:af991edb6dc1 | 87 | for(int i=0;i<9;i++){ |
| christodoulos | 1:af991edb6dc1 | 88 | difSum+=(myArray[i]-avg)*(myArray[i]-avg); //Find sum of difference between value X and mean |
| christodoulos | 1:af991edb6dc1 | 89 | } |
| christodoulos | 1:af991edb6dc1 | 90 | |
| christodoulos | 1:af991edb6dc1 | 91 | var=difSum/9; |
| christodoulos | 1:af991edb6dc1 | 92 | sigma=sqrt(var); |
| christodoulos | 1:af991edb6dc1 | 93 | |
| christodoulos | 1:af991edb6dc1 | 94 | printf("Standard dev: %f\n", sigma); |
| christodoulos | 1:af991edb6dc1 | 95 | |
| christodoulos | 1:af991edb6dc1 | 96 | if(sigma>=0.5){ //Check against carbon plateau, if sd greater than plateau startPoint |
| christodoulos | 1:af991edb6dc1 | 97 | for(int x=0;x<8;x++){ |
| christodoulos | 1:af991edb6dc1 | 98 | myArray[x]=myArray[x+1]; //Shift all carbon to left by 1 |
| christodoulos | 1:af991edb6dc1 | 99 | printf("%d ", myArray[x]); |
| christodoulos | 1:af991edb6dc1 | 100 | } |
| christodoulos | 1:af991edb6dc1 | 101 | |
| christodoulos | 1:af991edb6dc1 | 102 | myArray[8]=carbon(); // get new carbon value in last array slot |
| christodoulos | 1:af991edb6dc1 | 103 | printf("%d\n", myArray[8]); |
| christodoulos | 1:af991edb6dc1 | 104 | |
| christodoulos | 1:af991edb6dc1 | 105 | sum=0; //Reset variables |
| christodoulos | 1:af991edb6dc1 | 106 | sqSum=0; |
| christodoulos | 1:af991edb6dc1 | 107 | var=0; |
| christodoulos | 1:af991edb6dc1 | 108 | sigma=0; |
| christodoulos | 1:af991edb6dc1 | 109 | avg=0; |
| christodoulos | 1:af991edb6dc1 | 110 | difSum=0; |
| christodoulos | 1:af991edb6dc1 | 111 | |
| christodoulos | 1:af991edb6dc1 | 112 | }else{ |
| christodoulos | 1:af991edb6dc1 | 113 | printf("TURN ON SOLENOID\n"); //Turn on second sensor |
| christodoulos | 1:af991edb6dc1 | 114 | flag=true; //Terminate loop because sd is at plateau startPoint |
| christodoulos | 1:af991edb6dc1 | 115 | } |
| christodoulos | 1:af991edb6dc1 | 116 | } |
| christodoulos | 0:958e045ea7d1 | 117 | |
| christodoulos | 0:958e045ea7d1 | 118 | } |