Control Code with I/O and ADC working

Dependencies:   MODSERIAL mbed

Revision:
13:604e6933366f
Parent:
12:1cada1fe4743
Child:
14:69cd53434783
--- a/main.cpp	Mon May 07 03:38:15 2018 +0000
+++ b/main.cpp	Tue May 08 17:08:37 2018 +0000
@@ -17,7 +17,7 @@
 #define MAX_TEMP         65
 #define TEMP_MARGIN      20
 #define HYST_LOW         0.3
-#define HYST_HIGH        1.5
+#define HYST_HIGH        1
 #define SAMPLES          5
 #define I2C_Freq         2000
 #define VALVE            1
@@ -662,15 +662,23 @@
         if(DEBUG3)
             pc.printf("DBG: PROGRAM STARTED \r\n");
 
+        /*float time_sec = t.read();
+        
+        if(time_sec >= 60){
+            t.reset();
+        }
+        
+        pc.printf("Time: %f \r\n", time_sec);*/
+
         for(int chnl = 0; chnl < CHN_COUNT; chnl++){
             float currentTempFront = get_temp(chnl, FRONT_THERM);
-            wait(0.08);
-            float currentTempBack = get_temp(chnl, BACK_THERM);
+            //wait(0.08);
+            
             //float valveCurrent = get_valve_current(chnl, VALVE_FET_AMP);
             //float heaterCurrent = get_heater_current(chnl, HEAT_FET_AMP);
-            float currentTemp = currentTempFront;
+            //float currentTemp = currentTempFront;
             //currentTemp = currentTempFront;
-            currentTemp = currentTempBack;
+            
 
             //check if we received data/need to update TCTF data
             if(dataReceived){
@@ -681,10 +689,15 @@
                 pc.rxBufferFlush();
             }
 
+            float currentTempBack = get_temp(chnl, BACK_THERM);
+            float currentTemp = currentTempBack;
+
             if (chnl == chanSel_SendTemp){
                 //Send temp readings for selected chan to GUI
                 sendTempReadings(currChan, currentTemp);
             }
+            
+            
 
             //CONTROL LOOP:
             if(chnlStatus[chnl].status == 1){
@@ -695,7 +708,8 @@
                 if(((currentTempFront >= MAX_TEMP) && (currentTempBack >= MAX_TEMP)) ||
                    ((currentTempFront == 0) || (currentTempBack == 0))||
                     (abs(currentTempBack - currentTempFront) >= TEMP_MARGIN)||
-                   ((currentTempFront <= MIN_TEMP) && (currentTempBack <= MIN_TEMP))){
+                   ((currentTempFront <= MIN_TEMP) && (currentTempBack <= MIN_TEMP))||
+                    (chnlStatus[chnl].error == 1)){
                     status_led(chnl, STATUS_BAD);
                     sendError(chnl, 1);
                     chnlStatus[chnl].error = 1;
@@ -704,8 +718,18 @@
                 if(chnlStatus[chnl].error == 0){
                     if(currentTemp > ((chnlStatus[chnl].setTemp)+HYST_HIGH)){
                         if(DEBUG3) pc.printf("DBG: [%d] Chiller ON \r\n", chnl);
-                        //Turn chiller on
-                        turn_valve_on(chnl);
+                        //check if the temp. diff. is small and can't wait for an entire period (~4.18) to turn valve off
+                        if(abs(currentTemp - (chnlStatus[chnl].setTemp)) < 5){
+                            turn_valve_on(chnl);
+                            while(currentTemp > ((chnlStatus[chnl].setTemp)+HYST_HIGH)){
+                                currentTemp = get_temp(chnl, BACK_THERM);
+                            }
+                            turn_valve_off(chnl);
+                        }
+                        else{
+                            //Turn chiller on
+                            turn_valve_on(chnl);    
+                        }
                     }
                     else if (currentTemp < ((chnlStatus[chnl].setTemp)-HYST_LOW)){
                         if(DEBUG3) pc.printf("DBG: [%d] Heater ON \r\n", chnl);
@@ -723,6 +747,14 @@
                     }
                 }
             }
+            else{
+                //turn off chiller
+                turn_valve_off(chnl);
+                //turn off heater
+                turn_heater_off(chnl);
+                //turn on green LED status light
+                status_led(chnl, 1);
+            }
         }
     }
 }
\ No newline at end of file