Miguel Angel Torres Lazaro / Mbed 2 deprecated Pruebas_Ventilador_Eplimin_01_Backup

Dependencies:   mbed QEI FastPWM

Files at this revision

API Documentation at this revision

Comitter:
miguelangel_2511
Date:
Thu Apr 23 17:05:36 2020 +0000
Parent:
7:f4e248182d31
Child:
9:95fdcdc0977e
Commit message:
Se agrego la medicion y visualizacion grafica del volumen, el flujo y la presion. Sin embargo, aun no se han aplicado filtros a las seniales medidas.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
nextion_interface.cpp Show annotated file Show diff for this revision Revisions of this file
nextion_interface.h Show annotated file Show diff for this revision Revisions of this file
pressure_sensors.cpp Show annotated file Show diff for this revision Revisions of this file
pressure_sensors.h Show annotated file Show diff for this revision Revisions of this file
project_defines.h Show annotated file Show diff for this revision Revisions of this file
ventilator.cpp Show annotated file Show diff for this revision Revisions of this file
ventilator.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Apr 20 21:58:36 2020 +0000
+++ b/main.cpp	Thu Apr 23 17:05:36 2020 +0000
@@ -9,56 +9,35 @@
 #include "ventilator.h"
 
 
-// ******************************
-//      OBJECT CONSTRUCTION
-// ******************************
-
-Ticker ticker_int_10us; // Ticker interrupt
-
-
-// ******************************
-//      GLOBAL VARIABLES
-// ******************************
-
-
+int main(void) {
+    
+    System_Initialize();
 
-// ******************************
-//      FUNCTION DECLARATION
-// ******************************
-void Ticker_ISR(void); // ISR that is called when the timer reaches the set value
-
-
-int main(void) {
-    Buttons_Initialize();
-    ticker_int_10us.attach(&Ticker_ISR, 0.01);
-    Nextion_Interface_Initialize();
-    Pressure_Sensors_Initialize();
-    Stepper_Driver_Init();
-    
     while(1){     
          Stepper_Driver_State_Machine();
-         
          Nextion_Encoder_Interaction();
          
          if(read_pressure_sensors_flag){
              Pressure_Sensors_Read();
+             Calculate_Flow_From_Pressure();
+             Calculate_Volume_From_Flow();
              read_pressure_sensors_flag = 0;
          }
- /*        
+         
          if((stepper_driver_state != Stepper_Inspiration_Finish) && 
             (stepper_driver_state != Stepper_Expiration_Finish)){
+            /* Display sensed values if necessary */    
             if(pressure_sensor_display_update_flag){
-                Nextion_Update_Sensor_Values();
-                pressure_sensor_display_update_flag = 0;
+               Nextion_Update_Sensor_Values();
+               pressure_sensor_display_update_flag = 0;
             }
-            
+            /* Plot sensed values if necessary */
             if(pressure_sensor_waveform_update_flag){
                 Nextion_Update_Waveform_Values();
                 pressure_sensor_waveform_update_flag = 0;
-            }
+            }  
                  
-         }
- */        
+         }      
     }
   
     return 0;
@@ -66,21 +45,9 @@
 
 
 
-// Ticker Interrupt Service Routine
-void Ticker_ISR(void){
-   
-    static uint8_t conta_10ms = 0;
-    
-    Encoder_Read();
-    Button_Debounce();
-    
-    conta_10ms++;
-    
-    if(conta_10ms == 10){
-        conta_10ms = 0;
-        read_pressure_sensors_flag = 1;
-        pressure_sensor_display_update_flag = 1;
-        pressure_sensor_waveform_update_flag = 1;
-    }
-    
-}  
+  
+
+
+
+
+
--- a/nextion_interface.cpp	Mon Apr 20 21:58:36 2020 +0000
+++ b/nextion_interface.cpp	Thu Apr 23 17:05:36 2020 +0000
@@ -9,6 +9,7 @@
 #include "ventilator.h"
 
 
+
 /* Object definition */
 Serial  nextion_display(NEXTION_DISPLAY_TX_PIN,NEXTION_DISPLAY_RX_PIN);
 // For testing pusposes only
@@ -41,26 +42,39 @@
 
 void Nextion_Interface_Initialize(void){
     /* Configure baud rate */ 
+    nextion_display.baud(9600);
+    wait(1);
+    nextion_display.printf("baud=%d", NEXTION_DISPLAY_BAUD_RATE);
+    nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
+    wait(1);
     nextion_display.baud(NEXTION_DISPLAY_BAUD_RATE);
-    //pc.baud(115200);
-    
-    //pc.printf("PUERTO SERIAL HABILITADO\n\n\r");
+    wait(1);
+
+    /* Draw separators for the displayed values */
+    nextion_display.printf("line 10,160,190,160,%d", WHITE);
+    nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
+    nextion_display.printf("line 10,320,190,320,%d", WHITE);
+    nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
+    nextion_display.printf("line 610,160,790,160,%d", WHITE);
+    nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
+    nextion_display.printf("line 610,320,790,320,%d", WHITE);
+    nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);  
     
     /* Set font color */
-    nextion_display.printf("t6.pco=%d", NORMAL_COLOR);
+    nextion_display.printf("t1.pco=%d", NORMAL_COLOR);
     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff); 
+    nextion_display.printf("t4.pco=%d", NORMAL_COLOR);
+    nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
     nextion_display.printf("t7.pco=%d", NORMAL_COLOR);
     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-    nextion_display.printf("t8.pco=%d", NORMAL_COLOR);
-    nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-    
-   // pc.printf("t6.pco=%d\n\r", NORMAL_COLOR);
-    //pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff); 
-   // pc.printf("t7.pco=%d\n\r", NORMAL_COLOR);
-   // pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);
-   // pc.printf("t8.pco=%d\n\r", NORMAL_COLOR);
-    //pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);
-    
+  /*  
+    pc.printf("t6.pco=%d\n\r", NORMAL_COLOR);
+    pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff); 
+    pc.printf("t7.pco=%d\n\r", NORMAL_COLOR);
+    pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);
+    pc.printf("t8.pco=%d\n\r", NORMAL_COLOR);
+    pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);
+ */   
     /* Print default values */
     Volume_Setpoint_Fix();
     Resp_Frequency_Fix();
@@ -76,22 +90,16 @@
      /* Remove the selection box from the previous position */
       switch(past_parameter_selection_index){
             case 0:
-                nextion_display.printf("t6.pco=%d", NORMAL_COLOR);
+                nextion_display.printf("t1.pco=%d", NORMAL_COLOR);
                 nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-               // pc.printf("t6.pco=%d", NORMAL_COLOR);
-              //  pc.printf("%c%c%c", 0xff, 0xff, 0xff);
                 break;
             case 1:
+                nextion_display.printf("t4.pco=%d", NORMAL_COLOR);
+                nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
+                break;
+            case 2:
                 nextion_display.printf("t7.pco=%d", NORMAL_COLOR);
                 nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-               // pc.printf("t7.pco=%d\n\r", NORMAL_COLOR);
-               // pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);
-                break;
-            case 2:
-                nextion_display.printf("t8.pco=%d", NORMAL_COLOR);
-                nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-               // pc.printf("t8.pco=%d\n\r", NORMAL_COLOR);
-               // pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);
                 break;
             default:
                 break;  
@@ -101,22 +109,16 @@
      /* Draw the selection box in the new position */ 
       switch(parameter_selection_index){
             case 0:
-                nextion_display.printf("t6.pco=%d", SELECT_COLOR);
+                nextion_display.printf("t1.pco=%d", SELECT_COLOR);
                 nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-               // pc.printf("t6.pco=%d", SELECT_COLOR);
-               // pc.printf("%c%c%c", 0xff, 0xff, 0xff);
                 break;
             case 1:
+                nextion_display.printf("t4.pco=%d", SELECT_COLOR);
+                nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
+                break;
+            case 2:
                 nextion_display.printf("t7.pco=%d", SELECT_COLOR);
                 nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-               // pc.printf("t7.pco=%d\n\r", SELECT_COLOR);
-               // pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);
-                break;
-            case 2:
-                nextion_display.printf("t8.pco=%d", SELECT_COLOR);
-                nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-               // pc.printf("t8.pco=%d\n\r", SELECT_COLOR);
-               // pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);
                 break;
             default:
                 break;  
@@ -131,22 +133,16 @@
 
       switch(parameter_selection_index){
             case 0:
-                nextion_display.printf("t6.pco=%d", NORMAL_COLOR);
+                nextion_display.printf("t1.pco=%d", NORMAL_COLOR);
                 nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-               // pc.printf("t6.pco=%d", NORMAL_COLOR);
-               // pc.printf("%c%c%c", 0xff, 0xff, 0xff);
                 break;
             case 1:
+                nextion_display.printf("t4.pco=%d", NORMAL_COLOR);
+                nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
+                break;
+            case 2:
                 nextion_display.printf("t7.pco=%d", NORMAL_COLOR);
                 nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-              //  pc.printf("t7.pco=%d\n\r", NORMAL_COLOR);
-               // pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);
-                break;
-            case 2:
-                nextion_display.printf("t8.pco=%d", NORMAL_COLOR);
-                nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-               // pc.printf("t8.pco=%d\n\r", NORMAL_COLOR);
-               // pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);
                 break;
             default:
                 break;   
@@ -162,10 +158,8 @@
     volume_setpoint_display = VOLUME_SETPOINT_MINIMUM_VALUE  + VOLUME_SETPOINT_STEP*volume_setpoint_index;
     
     /* Print volume value */
-    nextion_display.printf("t6.txt=\"%d mL\"", volume_setpoint_display);
+    nextion_display.printf("t1.txt=\"%d\"", volume_setpoint_display);
     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-   // pc.printf("t6.txt=\"%d mL\"\n\r", volume_setpoint_display);
-   // pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);
 } 
 
 
@@ -175,10 +169,8 @@
     stepper_parameters_update_flag = 1;
     
     /* Print volume value */
-    nextion_display.printf("t6.txt=\"%d mL\"", volume_setpoint_display);
+    nextion_display.printf("t1.txt=\"%d\"", volume_setpoint_display);
     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-   // pc.printf("t6.txt=\"%d mL\"\n\r", volume_setpoint_display);
-   // pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);
 } 
 
 
@@ -187,10 +179,8 @@
     resp_frequency_display = 10 + resp_frequency_index;
     
     /* Print frequency value */
-    nextion_display.printf("t7.txt=\"%d RPM\"", resp_frequency_display);
+    nextion_display.printf("t4.txt=\"%d\"", resp_frequency_display);
     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-   // pc.printf("t7.txt=\"%d RPM\"\n\r", resp_frequency_display);
-   // pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);
 }  
 
 
@@ -200,11 +190,8 @@
     stepper_parameters_update_flag = 1;
     
     /* Print frequency value */
-    nextion_display.printf("t7.txt=\"%d RPM\"", resp_frequency_display);
+    nextion_display.printf("t4.txt=\"%d\"", resp_frequency_display);
     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-  //  pc.printf("t7.txt=\"%d RPM\"\n\r", resp_frequency_display);
-  //  pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);    
- 
 }  
 
 void I_E_Ratio_Display_Update(void){
@@ -213,10 +200,8 @@
     expiration_time_display = i_e_ratio_display_table[i_e_ratio_index][1];
     
     /* Print i:e ratio value */
-    nextion_display.printf("t8.txt=\"%2.1f/%2.1f\"", inspiration_time_display, expiration_time_display);
-    nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-   // pc.printf("t8.txt=\"%2.1f/%2.1f\"\n\r", inspiration_time_display, expiration_time_display);
-   // pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);   
+    nextion_display.printf("t7.txt=\"%2.1f : %2.1f\"", inspiration_time_display, expiration_time_display);
+    nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff); 
 }  
 
 
@@ -227,23 +212,120 @@
     stepper_parameters_update_flag = 1;
     
     /* Print i:e ratio value */
-    nextion_display.printf("t8.txt=\"%2.1f/%2.1f\"", inspiration_time_display, expiration_time_display);
+    nextion_display.printf("t7.txt=\"%2.1f : %2.1f\"", inspiration_time_display, expiration_time_display);
     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-   // pc.printf("t8.txt=\"%2.1f/%2.1f\"\n\r", inspiration_time_display, expiration_time_display);
-   // pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);
 }  
 
-void Pressure_01_Display_Update(void){
-    nextion_display.printf("t1.txt=\"%.2f mL\"", pressure_01_psi);
+void Volume_Display_Update(void){
+    nextion_display.printf("t11.txt=\"%.2f\"", volume_ml);
+    nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
+}
+
+void Pressure_Display_Update(void){
+    nextion_display.printf("t15.txt=\"%.1f\"", (pressure_02_psi * PSI_TO_CMH2O_CONSTANT));
+    nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
+}
+
+void Flow_Display_Update(void){
+    nextion_display.printf("t14.txt=\"%.2f\"", flow_lpm);
     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
 }
 
-void Pressure_02_Display_Update(void){
-    nextion_display.printf("t2.txt=\"%.2f mL\"", pressure_02_psi);
+
+void Nextion_Plot_Volume_Waveform(void){
+    
+    static uint16_t x_pos_counter = 1, prev_y_pos = (VOLUME_WAVEFORM_Y0 + VOLUME_WAVEFORM_HEIGHT - 1);
+    int16_t x_pos, y_pos;
+    uint16_t volume_scaled_value;
+    
+    //volume_scaled_value = (uint16_t)((volume_ml - 0.5)*VOLUME_WAVEFORM_HEIGHT*2) ;
+    volume_scaled_value = (uint16_t)((volume_ml - 0.5)*2) ;
+    
+    x_pos_counter++;
+    
+    if(x_pos_counter >= VOLUME_WAVEFORM_WIDTH){
+        nextion_display.printf("fill %d,%d,%d,%d,%d",VOLUME_WAVEFORM_X0,VOLUME_WAVEFORM_Y0,VOLUME_WAVEFORM_WIDTH,VOLUME_WAVEFORM_HEIGHT,8591);
+        nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
+        x_pos_counter = 1;
+    }
+    
+    x_pos = VOLUME_WAVEFORM_X0 + x_pos_counter;
+    y_pos = VOLUME_WAVEFORM_Y0 + VOLUME_WAVEFORM_HEIGHT - volume_scaled_value - 1;
+    
+    if(y_pos <= VOLUME_WAVEFORM_Y0){
+        y_pos = VOLUME_WAVEFORM_Y0 + 1;
+    }
+    
+    nextion_display.printf("line %d,%d,%d,%d,%d",(x_pos - 1), prev_y_pos, x_pos, y_pos, GREEN);
     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
+    
+    prev_y_pos = y_pos; 
 }
 
-void Diff_Pressure_Display_Update(void){
-    nextion_display.printf("t0.txt=\"%.2f mL\"", diff_pressure_psi);
+
+void Nextion_Plot_Flow_Waveform(void){
+    
+    static uint16_t x_pos_counter = 1, prev_y_pos = (FLOW_WAVEFORM_Y0 + FLOW_WAVEFORM_HEIGHT - 1);
+    uint16_t x_pos, y_pos;
+    uint16_t flow_scaled_value;
+    
+    flow_scaled_value = 79 + (int16_t)(flow_lpm * FLOW_WAVEFORM_HEIGHT);
+    
+    x_pos_counter++;
+    
+    if(x_pos_counter >= FLOW_WAVEFORM_WIDTH){
+        nextion_display.printf("fill %d,%d,%d,%d,%d",FLOW_WAVEFORM_X0,FLOW_WAVEFORM_Y0,FLOW_WAVEFORM_WIDTH,FLOW_WAVEFORM_HEIGHT,8591);
+        nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
+        x_pos_counter = 1;
+    }
+    
+    x_pos = FLOW_WAVEFORM_X0 + x_pos_counter;
+    y_pos = FLOW_WAVEFORM_Y0 + FLOW_WAVEFORM_HEIGHT - flow_scaled_value - 1;
+    
+    
+    if(y_pos <= FLOW_WAVEFORM_Y0){
+        y_pos = VOLUME_WAVEFORM_Y0 + 1;
+    }else if(y_pos >= (FLOW_WAVEFORM_Y0 + FLOW_WAVEFORM_HEIGHT)){
+        y_pos = VOLUME_WAVEFORM_Y0 + FLOW_WAVEFORM_HEIGHT - 1;
+    
+    }
+    
+    
+    
+    nextion_display.printf("line %d,%d,%d,%d,%d",(x_pos - 1), prev_y_pos, x_pos, y_pos, YELLOW);
     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-}
\ No newline at end of file
+    
+    prev_y_pos = y_pos; 
+}
+
+
+void Nextion_Plot_Pressure_Waveform(void){
+    
+    static uint16_t x_pos_counter = 1, prev_y_pos = (PRESSURE_WAVEFORM_Y0 + PRESSURE_WAVEFORM_HEIGHT - 1);
+    uint16_t x_pos, y_pos;
+    uint16_t pressure_02_scaled_value;
+    
+    pressure_02_scaled_value = (uint16_t)((pressure_02_psi - 0.5)*PRESSURE_WAVEFORM_HEIGHT*2) ;
+    
+    x_pos_counter++;
+    
+    if(x_pos_counter >= PRESSURE_WAVEFORM_WIDTH){
+        nextion_display.printf("fill %d,%d,%d,%d,%d",PRESSURE_WAVEFORM_X0,PRESSURE_WAVEFORM_Y0,PRESSURE_WAVEFORM_WIDTH,PRESSURE_WAVEFORM_HEIGHT,8591);
+        nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
+        x_pos_counter = 1;
+    }
+    
+    x_pos = PRESSURE_WAVEFORM_X0 + x_pos_counter;
+    y_pos = PRESSURE_WAVEFORM_Y0 + PRESSURE_WAVEFORM_HEIGHT - pressure_02_scaled_value - 1;
+    
+    
+    if(y_pos <= PRESSURE_WAVEFORM_Y0){
+        y_pos = PRESSURE_WAVEFORM_Y0 + 1;
+    }
+    
+    nextion_display.printf("line %d,%d,%d,%d,%d",(x_pos - 1), prev_y_pos, x_pos, y_pos, WHITE);
+    nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
+    
+    prev_y_pos = y_pos; 
+}
+
--- a/nextion_interface.h	Mon Apr 20 21:58:36 2020 +0000
+++ b/nextion_interface.h	Thu Apr 23 17:05:36 2020 +0000
@@ -3,6 +3,7 @@
 #define NEXTION_INTERFACE_H_
 
 /* File inclusion */
+#include "mbed.h"
 #include "stdint.h"
 
 /* Object declaration */
@@ -38,11 +39,13 @@
 void I_E_Ratio_Display_Update(void);
 void I_E_Ratio_Fix(void);
 
-void Pressure_01_Display_Update(void);
-void Pressure_02_Display_Update(void);
-void Diff_Pressure_Display_Update(void);
+void Volume_Display_Update(void);
+void Pressure_Display_Update(void);
+void Flow_Display_Update(void);
 
-
+void Nextion_Plot_Volume_Waveform(void);
+void Nextion_Plot_Flow_Waveform(void);
+void Nextion_Plot_Pressure_Waveform(void);
 
 
 #endif
\ No newline at end of file
--- a/pressure_sensors.cpp	Mon Apr 20 21:58:36 2020 +0000
+++ b/pressure_sensors.cpp	Thu Apr 23 17:05:36 2020 +0000
@@ -1,6 +1,7 @@
 
 #include "mbed.h"
 #include "stdint.h"
+#include "math.h"
 #include "project_defines.h"
 #include "pressure_sensors.h"
 
@@ -10,9 +11,11 @@
 I2C pressure_sensor_02(PRESSURE_SENSOR_02_SDA_PIN, PRESSURE_SENSOR_02_SCL_PIN);
 
 /* Global variable definition */
-uint16_t pressure_01_bin, pressure_02_bin, diff_pressure_bin;
-float pressure_01_psi, pressure_02_psi; 
-float diff_pressure_psi;
+int16_t pressure_01_bin, pressure_02_bin;
+float pressure_01_psi, pressure_02_psi;
+float flow_lpm, volume_ml;
+
+//float diff_pressure_psi;
 
 char sensor_read_data_buffer[2];
 
@@ -31,13 +34,47 @@
     pressure_01_bin = ((sensor_read_data_buffer[0] << 8) | sensor_read_data_buffer[1]) & 0x3FFF;
     pressure_sensor_02.read(PRESSURE_SENSOR_02_SRA, sensor_read_data_buffer, 2);
     pressure_02_bin = ((sensor_read_data_buffer[0] << 8) | sensor_read_data_buffer[1]) & 0x3FFF;
-    diff_pressure_bin =  pressure_01_bin - pressure_02_bin;
     
     /* Calculate pressure values in PSI */
     pressure_01_psi = (5.0 * pressure_01_bin) / 16383; 
     pressure_02_psi = (5.0 * pressure_02_bin) / 16383;
-    diff_pressure_psi = (5.0 * diff_pressure_bin) / 16383;
+
     
 } 
 
+void Calculate_Flow_From_Pressure(void){
+    
+    int16_t diff_pressure_bin;
+    float diff_pressure_psi;
+    
+    diff_pressure_bin =  pressure_02_bin - pressure_01_bin;
+    diff_pressure_psi = (5.0 * diff_pressure_bin) / 16383;
+    
+    if(diff_pressure_psi < 0){
+        flow_lpm = -SPIROMETER_SENSOR_CONSTANT * sqrt(-diff_pressure_psi);    
+    }else{
+        flow_lpm = SPIROMETER_SENSOR_CONSTANT * sqrt(diff_pressure_psi);  
+    }    
+}
 
+void Calculate_Volume_From_Flow(void){
+    
+    static float previous_flow_lpm = 0.0;
+    float delta_volume_ml;
+    
+    delta_volume_ml = (PRESSURE_SENSOR_READ_PERIOD_MS * (flow_lpm + previous_flow_lpm)) / 60.0;
+    
+    volume_ml = volume_ml + delta_volume_ml;
+    
+    previous_flow_lpm = flow_lpm;
+    
+}
+
+void Pressure_01_LPF(void){
+
+}
+
+
+void Pressure_02_LPF(void){
+
+}
--- a/pressure_sensors.h	Mon Apr 20 21:58:36 2020 +0000
+++ b/pressure_sensors.h	Thu Apr 23 17:05:36 2020 +0000
@@ -5,14 +5,19 @@
 #include "stdint.h"
 
 /* Global variable declaration */
-extern uint16_t pressure_01_bin, pressure_02_bin, diff_pressure_bin;
-extern float pressure_01_psi, pressure_02_psi; 
-extern float diff_pressure_psi;
+extern int16_t pressure_01_bin, pressure_02_bin;
+extern float pressure_01_psi, pressure_02_psi;
+extern float flow_lpm, volume_ml;
+//extern float diff_pressure_psi;
 
 extern char sensor_read_data_buffer[2]; 
 
 /* Function declaration */
 void Pressure_Sensors_Initialize(void);
 void Pressure_Sensors_Read(void);
+void Calculate_Flow_From_Pressure(void);
+void Calculate_Volume_From_Flow(void);
+void Pressure_01_LPF(void);
+void Pressure_02_LPF(void);
 
 #endif
\ No newline at end of file
--- a/project_defines.h	Mon Apr 20 21:58:36 2020 +0000
+++ b/project_defines.h	Thu Apr 23 17:05:36 2020 +0000
@@ -20,11 +20,10 @@
 #define NEXTION_DISPLAY_RX_PIN                  PC_11
 
 // Pressure sensors
-#define PRESSURE_SENSOR_01_SDA_PIN              PB_9
-#define PRESSURE_SENSOR_01_SCL_PIN              PB_8
-
-#define PRESSURE_SENSOR_02_SDA_PIN              PB_10
-#define PRESSURE_SENSOR_02_SCL_PIN              PB_11
+#define PRESSURE_SENSOR_01_SCL_PIN              PB_10
+#define PRESSURE_SENSOR_01_SDA_PIN              PB_11
+#define PRESSURE_SENSOR_02_SCL_PIN              PB_8
+#define PRESSURE_SENSOR_02_SDA_PIN              PB_9
 
 //Stepper motor
 #define STEPPER_PULSE_PIN                       PC_8
@@ -40,6 +39,8 @@
 #define LIMIT_SW_01                             1
 #define GAS_INPUT_SW                            2
 
+#define DEBOUNCE_PERIOD_MS                      10
+
 /* Defines for the encoder user interface */
 #define ENCODER_THRESHOLD                       8
 
@@ -53,9 +54,11 @@
 #define RESP_FREQUENCY_INDEX_DEFAULT            10
 #define I_E_RATIO_INDEX_DEFAULT                 0
 
+#define ENCODER_READ_PERIOD_MS                  50
+
 /* Defines for the Nextion user interface */
 
-#define NEXTION_DISPLAY_BAUD_RATE               9600
+#define NEXTION_DISPLAY_BAUD_RATE               115200
 
 #define BLACK                                   0
 #define BLUE                                    31
@@ -66,25 +69,10 @@
 #define GRAY                                    33840
 #define WHITE                                   65535
 
-#define NORMAL_COLOR                            BLACK
+#define NORMAL_COLOR                            WHITE
 #define SELECT_COLOR                            BLUE
 #define ADJUST_COLOR                            GREEN
 
-//#define BACKGROUND_COLOR                        Black
-//#define INDICATOR_BOX_LINE_COLOR                Green
-//#define INDICATOR_HEADER_TEXT_COLOR             Yellow
-//#define INDICATOR_VALUE_TEXT_COLOR              White
-//#define INDICATOR_UNITS_TEXT_COLOR              White
-//#define PARAMETER_SELECTION_BOX_COLOR           Yellow
-//#define PARAMETER_ADJUST_TEXT_COLOR             Green 
-
-#define HEADERS_ROW1_Y_POS                      20
-#define HEADERS_ROW2_Y_POS                      180
-#define UNITS_ROW1_Y_POS                        103
-#define UNITS_ROW2_Y_POS                        263
-#define VALUES_ROW1_Y_POS                       60
-#define VALUES_ROW2_Y_POS                       220
-
 #define VOLUME_SETPOINT_VALUE_DEFAULT           500
 #define RESP_FREQUENCY_VALUE_DEFAULT            20
 #define INSPIRATION_TIME_VALUE_DEFAULT          1.0
@@ -96,7 +84,25 @@
 #define AIR_INPUT                               0
 #define OXYGEN_INPUT                            1
 
-#define MAIN_SCREEN_DEFAULT_STATE                       Main_Screen_Cursor_Disable
+#define MAIN_SCREEN_DEFAULT_STATE               Main_Screen_Cursor_Disable
+
+#define VOLUME_WAVEFORM_X0                      200
+#define VOLUME_WAVEFORM_Y0                      3
+#define VOLUME_WAVEFORM_WIDTH                   400
+#define VOLUME_WAVEFORM_HEIGHT                  157
+
+#define FLOW_WAVEFORM_X0                        200
+#define FLOW_WAVEFORM_Y0                        162
+#define FLOW_WAVEFORM_WIDTH                     400
+#define FLOW_WAVEFORM_HEIGHT                    157
+
+#define PRESSURE_WAVEFORM_X0                    200
+#define PRESSURE_WAVEFORM_Y0                    321
+#define PRESSURE_WAVEFORM_WIDTH                 400
+#define PRESSURE_WAVEFORM_HEIGHT                157
+
+#define SENSOR_WAVEFORM_PLOT_PERIOD_MS          25
+#define SENSOR_DISPLAY_PERIOD_MS                100
 
 /* Defines for the pressure sensors */
 #define PRESSURE_SENSOR_I2C_CLK_HZ                      200000
@@ -106,8 +112,16 @@
 #define PRESSURE_SENSOR_02_SWA                          (PRESSURE_SENSOR_02_SLAVE_ADDRESS << 1)
 #define PRESSURE_SENSOR_01_SRA                          (PRESSURE_SENSOR_01_SWA  | 1)
 #define PRESSURE_SENSOR_02_SRA                          (PRESSURE_SENSOR_02_SWA  | 1)
+
+#define VOLUME_DISPLAY_THRESHOLD                        0.01f
+#define FLOW_DISPLAY_THRESHOLD                          0.01f
 #define PRESSURE_DISPLAY_THRESHOLD                      0.01f
 
+#define PSI_TO_CMH2O_CONSTANT                           70.307
+#define SPIROMETER_SENSOR_CONSTANT                      1.0f
+
+#define PRESSURE_SENSOR_READ_PERIOD_MS                  1
+
 /* Defines for the stepper motor control */ 
 #define TO_HOME                                         0
 #define TO_AIRBAG                                       1
@@ -142,6 +156,6 @@
 #define STEPPER_START_BRAKE_PERIOD_US                   (1000000.0/STEPPER_START_BRAKE_FREQUENCY_HZ)
 #define SHORT_RAMP_STEPS                                25
 
-#define TUNING_FACTOR                                   1.25
+#define TUNING_FACTOR                                   1.2
 
 #endif
--- a/ventilator.cpp	Mon Apr 20 21:58:36 2020 +0000
+++ b/ventilator.cpp	Thu Apr 23 17:05:36 2020 +0000
@@ -8,8 +8,12 @@
 #include "encoder_interface.h"
 #include "nextion_interface.h"
 #include "pressure_sensors.h"
+#include "stepper_motor_driver.h"
 #include "ventilator.h"
 
+/* Object definition */
+Ticker ticker_int_1ms; // Ticker interrupt
+
 
 /* Global variable definition */
 
@@ -30,6 +34,16 @@
 uint8_t first_time_in_state = 1;
 
 /* Function definition */
+
+void System_Initialize(void){
+    Buttons_Initialize();
+    Nextion_Interface_Initialize();
+    Pressure_Sensors_Initialize();
+    ticker_int_1ms.attach(&Ticker_ISR, 0.001);
+    Stepper_Driver_Init();
+}
+
+
 void Nextion_Encoder_Interaction(void){
 
  switch(main_screen_state){
@@ -37,6 +51,7 @@
                 if(Get_Button_Press(1 << ENTER_SW)){
                     rotary_encoder.reset();
                     main_screen_state = Main_Screen_Cursor_Enable;
+                    first_time_in_state = 1;
                 }
                 
                 break;  
@@ -63,7 +78,7 @@
             case Main_Screen_Volume_Setpoint_Adjust:
                 if(first_time_in_state){
                     first_time_in_state = 0;
-                    nextion_display.printf("t6.pco=%d", ADJUST_COLOR); // Change font color
+                    nextion_display.printf("t1.pco=%d", ADJUST_COLOR); // Change font color
                     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
                    // pc.printf("t6.pco=%d\n\r", ADJUST_COLOR); // Change font color
                    // pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);
@@ -87,7 +102,7 @@
             case Main_Screen_Resp_Frequency_Adjust:
                 if(first_time_in_state){
                     first_time_in_state = 0;
-                    nextion_display.printf("t7.pco=%d", ADJUST_COLOR); // Change font color
+                    nextion_display.printf("t4.pco=%d", ADJUST_COLOR); // Change font color
                     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
                    // pc.printf("t7.pco=%d\n\r", ADJUST_COLOR); // Change font color
                    // pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);
@@ -112,7 +127,7 @@
             case Main_Screen_I_E_Ratio_Adjust:
                 if(first_time_in_state){
                     first_time_in_state = 0;
-                    nextion_display.printf("t8.pco=%d", ADJUST_COLOR); // Change font color
+                    nextion_display.printf("t7.pco=%d", ADJUST_COLOR); // Change font color
                     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
                     //pc.printf("t8.pco=%d\n\r", ADJUST_COLOR); // Change font color
                    // pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);
@@ -142,54 +157,84 @@
  
  void Nextion_Update_Sensor_Values(void){
     
-    static float previous_pressure_01_psi = 0, previous_pressure_02_psi = 0;
-    static float previous_diff_pressure_psi = 0;
+    static float previous_pressure_02_psi = 0;
+    static float previous_volume_ml = 0;
+    static float previous_flow_lpm = 0;
     float delta;
     
     /* Check if anu value has change in a meningful way */
-    delta = pressure_01_psi - previous_pressure_01_psi;
-    if((delta > PRESSURE_DISPLAY_THRESHOLD) || (delta < -PRESSURE_DISPLAY_THRESHOLD)){
-        Pressure_01_Display_Update();
+    delta = volume_ml - previous_volume_ml;
+    if((delta > VOLUME_DISPLAY_THRESHOLD) || (delta < -PRESSURE_DISPLAY_THRESHOLD)){
+        Volume_Display_Update();
     } 
     
     delta = pressure_02_psi - previous_pressure_02_psi;
     if((delta > PRESSURE_DISPLAY_THRESHOLD) || (delta < -PRESSURE_DISPLAY_THRESHOLD)){
-        Pressure_02_Display_Update();
+        Pressure_Display_Update();
     }
     
-    delta = diff_pressure_psi - previous_diff_pressure_psi;
-    if((delta > PRESSURE_DISPLAY_THRESHOLD) || (delta < -PRESSURE_DISPLAY_THRESHOLD)){
-        Diff_Pressure_Display_Update();
+    delta = flow_lpm - previous_flow_lpm;
+    if((delta > FLOW_DISPLAY_THRESHOLD) || (delta < -PRESSURE_DISPLAY_THRESHOLD)){
+        Flow_Display_Update();
     }    
     
     
     /* Update the previous values */
-    previous_pressure_01_psi = pressure_01_psi;
+    previous_volume_ml = volume_ml;
     previous_pressure_02_psi = pressure_02_psi;
-    previous_diff_pressure_psi = diff_pressure_psi;
- 
+    previous_flow_lpm = flow_lpm;
+
  }
  
  
-  void Nextion_Update_Waveform_Values(void){
- 
-    uint8_t pressure_graph_val;
-    
-    /* Plot pressure 01 in a 0 to 1 PSI scale */
-    pressure_graph_val = (uint8_t)(pressure_01_psi * 128);
-    nextion_display.printf("add 10,0,%d", pressure_graph_val);
-    nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
+void Nextion_Update_Waveform_Values(void){
+    Nextion_Plot_Volume_Waveform();  
+    Nextion_Plot_Pressure_Waveform();  
+    Nextion_Plot_Flow_Waveform(); 
+}
+
+
+// Ticker Interrupt Service Routine
+void Ticker_ISR(void){
+   
+    static uint8_t debounce_conta_1ms = 0;
+    static uint8_t encoder_conta_1ms = 0;
+    static uint8_t sensor_conta_1ms = 0;
+    static uint8_t sensor_waveform_conta_1ms = 0;
+    static uint8_t sensor_display_conta_1ms = 0;
+     
+    debounce_conta_1ms++;
+    encoder_conta_1ms++;
+    sensor_conta_1ms++;
+    sensor_waveform_conta_1ms++;
+    sensor_display_conta_1ms++;
     
-    /* Plot pressure 02 in a 0 to 1 PSI scale */
-    pressure_graph_val = (uint8_t)(pressure_02_psi * 128);
-    nextion_display.printf("add 11,0,%d", pressure_graph_val); 
-    nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff); 
-    
-    /* Plot differential pressure in a -1 PSI to 1 PSI scale */
-    pressure_graph_val = 64 + (uint8_t)(diff_pressure_psi * 64);
-    nextion_display.printf("add 1,0,%d", pressure_graph_val);
-    nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);  
- 
-  }
+    // Read buttons and performs debounce action
+    if(debounce_conta_1ms == DEBOUNCE_PERIOD_MS){
+        debounce_conta_1ms = 0;
+        Button_Debounce();
+    }    
+    // Read encoder and update the proper indexes    
+    if(encoder_conta_1ms == ENCODER_READ_PERIOD_MS){
+        encoder_conta_1ms = 0;
+        Encoder_Read();
+    }        
+    // Indicates if it is time to acquire a new sample from the sensors
+    if(sensor_conta_1ms == PRESSURE_SENSOR_READ_PERIOD_MS){
+        sensor_conta_1ms = 0;
+        read_pressure_sensors_flag = 1;
+    }       
+    // Indicates if it is time to plot the volume, flow and pressure values */   
+    if(sensor_waveform_conta_1ms  == SENSOR_WAVEFORM_PLOT_PERIOD_MS){
+        sensor_waveform_conta_1ms  = 0;
+        pressure_sensor_waveform_update_flag = 1;
+    }    
+    // Indicates if it is time to display the volume, flow and pressure values */   
+    if(sensor_display_conta_1ms  == SENSOR_DISPLAY_PERIOD_MS){
+        sensor_display_conta_1ms  = 0;
+        pressure_sensor_display_update_flag = 1;
+    }               
+        
+}
  
         
\ No newline at end of file
--- a/ventilator.h	Mon Apr 20 21:58:36 2020 +0000
+++ b/ventilator.h	Thu Apr 23 17:05:36 2020 +0000
@@ -16,6 +16,11 @@
     Main_Screen_I_E_Ratio_Adjust = 4
 }Main_Screen_State_t;
 
+
+/* Object declaration */
+extern Ticker ticker_int_1ms;
+
+
 /* Global variable declaration */
 extern volatile uint8_t read_pressure_sensors_flag;
 extern volatile uint8_t pressure_sensor_display_update_flag;
@@ -34,10 +39,11 @@
 
 
 /* Function declaration */
+void System_Initialize(void);
 void Nextion_Encoder_Interaction(void);
 void Nextion_Update_Sensor_Values(void);
 void Nextion_Update_Waveform_Values(void);
-
+void Ticker_ISR(void); // ISR that is called when the timer reaches the set value
 
 /* Inline functions */
 static inline void Encoder_Read(){