AND / Mbed 2 deprecated lungFunction

Dependencies:   mbed

Committer:
christodoulos
Date:
Wed Jun 24 11:39:43 2020 +0000
Revision:
2:322470aeeae9
Parent:
1:00fe5bf692b4
Lung function simple test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
christodoulos 0:d034cdad5b6d 1 #include "mbed.h"
christodoulos 0:d034cdad5b6d 2
christodoulos 2:322470aeeae9 3 Serial ble(PC_12,PD_2); //BLE
christodoulos 2:322470aeeae9 4 Serial co2(PC_10,PC_11); //CO2 sensor
christodoulos 2:322470aeeae9 5 AnalogIn flowIn(PA_1); //Flow sensor
christodoulos 2:322470aeeae9 6 I2C si7051(PB_9, PB_8); //Si7051 temperature sensor
christodoulos 0:d034cdad5b6d 7
christodoulos 0:d034cdad5b6d 8 float flowVal1;
christodoulos 0:d034cdad5b6d 9 float flowVal2;
christodoulos 0:d034cdad5b6d 10 float Pressure;
christodoulos 2:322470aeeae9 11 float finalflow;
christodoulos 1:00fe5bf692b4 12
christodoulos 2:322470aeeae9 13 ///Flow function///
christodoulos 2:322470aeeae9 14 float flow()
christodoulos 2:322470aeeae9 15 {
christodoulos 2:322470aeeae9 16 flowVal1=3.3*flowIn; //Logic level 3.3
christodoulos 2:322470aeeae9 17 flowVal2 = 1.5*flowVal1; //5v
christodoulos 2:322470aeeae9 18 Pressure =(125*flowVal2)-62.5; //Used for calibration
christodoulos 2:322470aeeae9 19 finalflow=(0.3425*sqrt(Pressure))-0.198; //Used for program, flow in litter per min
christodoulos 2:322470aeeae9 20 return finalflow;
christodoulos 0:d034cdad5b6d 21 }
christodoulos 0:d034cdad5b6d 22
christodoulos 2:322470aeeae9 23 ///CO2 setup///
christodoulos 2:322470aeeae9 24 int value;
christodoulos 2:322470aeeae9 25 float carbon()
christodoulos 2:322470aeeae9 26 {
christodoulos 2:322470aeeae9 27 bool allow = false;
christodoulos 2:322470aeeae9 28 char c;
christodoulos 2:322470aeeae9 29 char co2_measure[5];
christodoulos 2:322470aeeae9 30 int count=0;
christodoulos 2:322470aeeae9 31
christodoulos 2:322470aeeae9 32 while(1)
christodoulos 2:322470aeeae9 33 {
christodoulos 2:322470aeeae9 34 c = co2.getc();
christodoulos 2:322470aeeae9 35 //based on the user manual PDF for the CO2 sensor, the value starts with "Z"
christodoulos 2:322470aeeae9 36 //and we need to extract the right number of CO2 value
christodoulos 2:322470aeeae9 37 if(c=='Z') {
christodoulos 2:322470aeeae9 38 allow = true;
christodoulos 2:322470aeeae9 39 }
christodoulos 1:00fe5bf692b4 40
christodoulos 2:322470aeeae9 41 if(allow) {
christodoulos 2:322470aeeae9 42 if(c>=48 && c<=57) {
christodoulos 2:322470aeeae9 43 co2_measure[count]=c;
christodoulos 2:322470aeeae9 44 count++;
christodoulos 2:322470aeeae9 45 }
christodoulos 2:322470aeeae9 46
christodoulos 2:322470aeeae9 47 if(count>=6) {
christodoulos 2:322470aeeae9 48 value = ((co2_measure[0]-'0')*100000+co2_measure[1]-'0')*10000+(co2_measure[2]-'0')*1000+(co2_measure[3]-'0')*100;
christodoulos 2:322470aeeae9 49 float CAR;
christodoulos 2:322470aeeae9 50 CAR=(float)value/10000;
christodoulos 2:322470aeeae9 51 count=0;
christodoulos 2:322470aeeae9 52 allow=false;
christodoulos 2:322470aeeae9 53 return CAR;
christodoulos 2:322470aeeae9 54 }
christodoulos 2:322470aeeae9 55 }
christodoulos 2:322470aeeae9 56 }
christodoulos 2:322470aeeae9 57 }
christodoulos 2:322470aeeae9 58
christodoulos 2:322470aeeae9 59
christodoulos 2:322470aeeae9 60 int main()
christodoulos 2:322470aeeae9 61 {
christodoulos 2:322470aeeae9 62 ble.baud(115200);
christodoulos 2:322470aeeae9 63
christodoulos 2:322470aeeae9 64 ble.printf("$");//enter command mode only for rn
christodoulos 1:00fe5bf692b4 65 wait(0.1);
christodoulos 2:322470aeeae9 66 ble.printf("$$");//enter command mode
christodoulos 1:00fe5bf692b4 67 wait(0.5);
christodoulos 2:322470aeeae9 68 ble.printf("SN,Flow Calibration\r");//set new name
christodoulos 1:00fe5bf692b4 69 wait(0.5);
christodoulos 2:322470aeeae9 70 ble.printf("SS,C0\r");//set transparent uart
christodoulos 1:00fe5bf692b4 71 wait(0.5);
christodoulos 2:322470aeeae9 72 ble.printf("---\r");//enter data mode
christodoulos 1:00fe5bf692b4 73 wait(0.5);
christodoulos 1:00fe5bf692b4 74
christodoulos 2:322470aeeae9 75 ///TEMP SETUP///
christodoulos 2:322470aeeae9 76 int tempAddr=0x80; //si7050 8bit address
christodoulos 2:322470aeeae9 77
christodoulos 2:322470aeeae9 78 char wTemp[1];
christodoulos 2:322470aeeae9 79 wTemp[0]=0xE3; //Hold master mode
christodoulos 2:322470aeeae9 80 char rTemp[2]; //Temperature returns MSB and LSB
christodoulos 2:322470aeeae9 81 //assume MBS in rTemp[0] and LSB in rTemp[1]
christodoulos 2:322470aeeae9 82
christodoulos 2:322470aeeae9 83 char wInit[2];
christodoulos 2:322470aeeae9 84 wInit[0]=0xE6; //User register 1
christodoulos 2:322470aeeae9 85 wInit[1]=0x00; //Set 14 bit resolution, other read-ony registers shouldn't be affected
christodoulos 2:322470aeeae9 86
christodoulos 2:322470aeeae9 87 si7051.write(tempAddr,wInit,2);
christodoulos 2:322470aeeae9 88 ///END TEMP///
christodoulos 2:322470aeeae9 89
christodoulos 2:322470aeeae9 90 float temp_code;
christodoulos 2:322470aeeae9 91 float SiTemp;
christodoulos 2:322470aeeae9 92
christodoulos 2:322470aeeae9 93 while(1) {
christodoulos 2:322470aeeae9 94 si7051.write(tempAddr,wInit,2);
christodoulos 2:322470aeeae9 95 si7051.write(tempAddr,wTemp,1);
christodoulos 2:322470aeeae9 96 si7051.read(tempAddr,rTemp,2); //Returns 2 bytes
christodoulos 2:322470aeeae9 97 temp_code=(rTemp[0]<<8)+rTemp[1];
christodoulos 2:322470aeeae9 98 SiTemp=((175.72*temp_code)/65536)-46.85;
christodoulos 2:322470aeeae9 99
christodoulos 2:322470aeeae9 100 ble.printf("Flow:%.2fl/s,CO2:%.2f%,Temperature:%.2fC\n", flow(),carbon(),SiTemp);
christodoulos 2:322470aeeae9 101 wait(0.1);
christodoulos 2:322470aeeae9 102 }
christodoulos 0:d034cdad5b6d 103
christodoulos 0:d034cdad5b6d 104 }