FeNO LCD code, Commented.

Dependencies:   mbed TextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "pressure.h"
00003 #include "TextLCD.h"
00004 #include "time.h"
00005 
00006 /////////////////////////
00007 // User has to hold his breath for 5 second at a correct amount of pressure
00008 // There is an indicator on the LCD which shows the acceptable boundaries 
00009 ///////////////////////
00010 
00011 //Serial ttl(PA_0, PA_1);
00012 PwmOut led(PB_6);
00013 TextLCD lcd(PC_8,PC_6,PB_15,PB_14,PB_13,PB_12, TextLCD::LCD16x2);
00014 Timeout t;
00015 
00016 int flag1;
00017 float sample2=0;
00018 float sample1;
00019 float delta;
00020 float sample0;
00021 float delta2;
00022 float sample3;
00023 float sample4;
00024 float delta3;
00025 int error1;
00026 int error2;
00027 int U;
00028 
00029 //  
00030 
00031 void okmsg() {
00032     lcd.locate(6,0);
00033     lcd.printf("well done");
00034     flag1=0;
00035 }  
00036        void one() {
00037        printf("1...\n");
00038        t.attach(&okmsg, 1.0);
00039        }
00040        void two() {
00041     lcd.locate(6,0);
00042     lcd.printf("hold: 2s ");
00043        t.attach(&one, 1.0);
00044        }
00045         void three() {
00046        lcd.locate(6,0);
00047     lcd.printf("hold: 3s ");
00048        t.attach(&two, 1.0);
00049        }
00050        void four() {
00051        lcd.locate(6,0);
00052     lcd.printf("hold: 4s ");
00053        t.attach(&three, 1.0);
00054        }
00055         void five() {
00056        lcd.locate(6,0);
00057     lcd.printf("hold: 5s ");
00058        t.attach(&four, 1.0);
00059        }
00060          void six() {
00061        lcd.locate(6,0);
00062     lcd.printf("hold: 6s ");
00063        t.attach(&five, 1.0);
00064        }
00065 
00066    void counting() {
00067     six();
00068 }     
00069 
00070 void RED1() {
00071             
00072 // At the "Situation 0", which is before breathing into the device, 
00073 // we reset everything and ask user to start breathing, set the brightness
00074 // of the LED to the lowest, and detach the timer
00075 
00076             sample0=0;
00077             sample2=0;
00078             sample1=0;
00079             sample4=0;
00080             error1=0;
00081             error2=0;
00082             flag1=0;
00083             U=0;
00084             lcd.locate(0,1);
00085             lcd.printf("Start Breathing ");
00086             led.write(0.20f);
00087             t.detach();
00088                 }
00089                 
00090     void Harder(){
00091         if (delta2>0 and error1==0) 
00092         {
00093                 t.detach();
00094                 sample0=0;
00095                 sample2=0;
00096                 sample1=0;
00097                 error1=1; 
00098                 U=1; //This means that the program is now ready to have the breath in the standard boundaries.
00099          }
00100                 }
00101  
00102 
00103 
00104 
00105 int main()
00106 {  
00107 
00108     pressure();
00109     flow();
00110     //ttl.baud(9600);
00111     //first we clear lcd
00112     lcd.cls();
00113     //then we define the location to start showing characters from there
00114     lcd.locate(2,0);    //column [0 to 15] , row [0,1] : in first row at the 2nd column I put the first line,
00115     lcd.printf("|");    //to show the first acceptable boundary.
00116     lcd.locate(3,0);    //in the same row, 3rd column, I put the second boundary.
00117     lcd.printf("|");
00118     //lcd.locate(0,1);
00119     //lcd.printf("breathe");
00120     
00121    int ft=0;
00122     while(1) {
00123      
00124      //lcd.locate(8,0);
00125    printf("f: %3.2f\n", flow());
00126      
00127        // ttl.printf("flow: %f\n", flow());
00128        
00129        //we're making comparasion using pressure because it was more reliable than flow
00130        
00131         //////////////////       SITUATION 0: Before Breathing
00132         if (pressure()<180.32)
00133         {
00134             //calls the following funtion which is defined before the main while loop:
00135             RED1();
00136         }
00137         
00138         
00139         //////////////////       SITUATION 1: Starts Breathing
00140         if (pressure()>=180.32 and pressure()<200)
00141         {
00142             sample0=pressure();
00143             delta2=sample1-sample0;
00144         //Naturally, at the beginning, the breath level goes to the situation 1 before situation 2
00145         //so we need to enable a flag called U at the following called function
00146         //so it means that the code is ready for the next stage:
00147             Harder();
00148             lcd.locate(0,1);
00149             lcd.printf("** OK           ");
00150             led.write(0.20f);
00151         }
00152         
00153         //////////////////       SITUATION 2: Standard level
00154         if (pressure()>=200 and pressure()<208.44)
00155         {
00156             sample1=pressure();
00157            delta=sample2-sample1;
00158            
00159         //If breath level has already reached the previous situation and now is in this situation,
00160         //then it is in a standard level and user has to keep it for 5 seconds
00161       
00162            if (U==1){
00163                //the following function activates a series of 7 other functions respectively.
00164                counting();
00165                U=0;
00166                }
00167            if (flag1==0 and delta < 0)
00168                 {
00169                     flag1=1; 
00170                     //the following function activates a series of 7 other functions respectively.   
00171                     counting(); 
00172                 }
00173             lcd.locate(0,1);
00174             lcd.printf("***  Good      ");
00175             led.write(0.40f);
00176         }
00177     
00178         //////////////////       SITUATION 3: Perfect level
00179         if (pressure()>=208.44 and pressure()<238.48)
00180         {
00181             lcd.locate(0,1);
00182             lcd.printf("**** Perfect    ");
00183             led.write(0.60f);
00184             sample2=pressure();
00185         }
00186 
00187         //////////////////       SITUATION 4: Standard level
00188         if (pressure()>=238.248 and pressure()<270.55)
00189         {
00190             sample3=pressure();
00191            delta3=sample4-sample3;
00192            if(delta3>0){
00193             counting();
00194             sample4=0;
00195             }
00196             lcd.locate(0,1);
00197             lcd.printf("***** Good      ");
00198             led.write(0.80f);
00199         }
00200 
00201         //////////////////       SITUATION 5: Too hard level
00202         if (pressure()>=270.55)
00203         {
00204             sample4=pressure();
00205             if (error2==0){
00206             sample0=0;
00207             sample2=0;
00208             sample1=0;    
00209             t.detach();
00210             error2=1; 
00211             }
00212             lcd.locate(0,1);
00213             lcd.printf("Breathe softer  ");
00214             led.write(1.00f);
00215         }
00216    // lcd.printf("%f\n", flow());
00217     wait (0.1);
00218                 
00219     }
00220 }