Dryer timer with analogue input from a dew point sensor.

Dependencies:   mbed TextLCD

Files at this revision

API Documentation at this revision

Comitter:
koosvanderwat
Date:
Sat Jan 23 08:11:57 2021 +0000
Parent:
3:559e8c72212a
Commit message:
Dewpoint sensor range 0 to -100

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 559e8c72212a -r e8ba2070b69e main.cpp
--- a/main.cpp	Fri Nov 29 09:27:44 2019 +0000
+++ b/main.cpp	Sat Jan 23 08:11:57 2021 +0000
@@ -1,3 +1,4 @@
+//Import Libraries
 #include "mbed.h"
 #include "TextLCD.h"
 #include "Watchdog.h"
@@ -6,9 +7,11 @@
 //Dryer Cycle
 int PT = 120; // Purge time in seconds
 int RT = 60; // Repressurisation time in seconds
+int IdleTimeHrs = 12; // cycle activated every x hours to lubricate valves.
+int LCycles = 2; //Number of lubication cycles
 
 //Dew Point Setpoint
-float SetPoint = -3; // Dew Point Set Point
+float SetPoint = -40; // Dew Point Set Point
 
 //Watchdog
 Watchdog wd;
@@ -16,6 +19,7 @@
 //Screen Setup
 TextLCD lcd(D6, D9, D2, D3, D4, D5); // rs, e, d4-d7
 
+//Define clock
 Ticker              timeKeeping;
 
 //Pin Allocation
@@ -29,29 +33,35 @@
 DigitalIn my_Dip1(D12);  //Dip 1
 AnalogIn DP_In(A3);
 
+//Counters
 unsigned long previousMillis = 0;        // will store last time LED was updated
 long OnTime = 1000;                     // milliseconds of on-time
-int secondscounter = 0;
-int Counter = 0;
-int sensorbroken =0;
-
-int LHCT = PT + RT; // LH cycle time
-int LHPT = PT + RT + PT; // LH purge time
-int CT = PT + RT + PT + RT; // Cycle Time
+int secondscounter = 0;                 // counts the seconds of the cycle
+int Counter = 0;                        // used to reset the screen every 10 seconds
+int Cycledryercounter = 0;              // used to cycle the dryer for 5 cycles if the dryer has been idle for 24hrs.
+int Idlecounter = 0;                    // used to accumulate the cummulative idle time.
+int Valuecounter = 0;                   // used to calculate the weighted average of the dewpoint.
+//int IdleTimeSeconds = 180; //idle time in seconds
+int IdleTimeSeconds = IdleTimeHrs * 60 * 60; //idle time in seconds
+int LCycleseconds = LCycles * 360 - 20; // Lubrication cycles in seconds
 
-float RHValue = 0;
-float TempValue = 0;
-float A = 0;
-float DewPoint = 0;
-float Value1 = 0;
-float Value2 = 0;
-float Value3 = 0;
-
-int Valuecounter = 0;
+//Variables
+int LHCT = PT + RT; // LH cycle time
+int RHPT = PT + RT + PT; // RH purge time
+int CT = PT + RT + PT + RT; // Cycle Time
+float RHValue = 0; //RH from dew point sensor
+float TempValue = 0; //Temp from dew point sensor
+float DewPoint = 0; //Calculated dew point
+float Value1 = 0; //first sensor reading
+float Value2 = 0; //second sensor reading
+float Value3 = 0; //third sensor reading
 
 int main()
 {
 
+//This piece of code runs once at start-up:
+//________________________________________
+
     my_RHMV = 1;//0
     my_LHMV = 0;//1
     my_RHPV = 0;//1
@@ -76,83 +86,33 @@
     millisStart();
 
     while(1) {
-        unsigned long currentMillis = millis(); // Get current time
 
-        if(my_Dip2 ==0) {
-            if(my_Dip1 ==0) {
-                SetPoint = -3;
-            }
-            if(my_Dip1 ==1) {
-                SetPoint = -20;
-            }
-        }
-        if(my_Dip2 ==1) {
-            if(my_Dip1 ==0) {
-                SetPoint = -40;
-            }
-            if(my_Dip1 ==1) {
-                SetPoint = -60;
-            }
-        }
-
-        if ((currentMillis - previousMillis) >= OnTime) {
-            previousMillis = currentMillis;
-            wd.Service();
-            my_led = !my_led;
-
-            secondscounter = secondscounter + 1;
-            Counter = Counter + 1;
-
-            if(Counter == 10) {
-                lcd.cls();
-                lcd.printf("Dew Point:");
-                lcd.locate(0,1);
-                lcd.printf("Set Point:");
-                Counter = 0;
-            }
+//This piece of code runs every millisecond for ever:
+//___________________________________________________
 
 
-            if(Valuecounter ==0) {
-                Value1 = DP_In.read()*100 - 80;
-            }
-            if(Valuecounter ==1) {
-                Value2 = DP_In.read()*100 - 80;
-            }
-            if(Valuecounter ==2) {
-                Value3 = DP_In.read()*100 - 80;
-                Valuecounter=0;
-            }
+        unsigned long currentMillis = millis(); // Get current time
 
-            Valuecounter = Valuecounter + 1;
-
-            DewPoint = (Value1+Value2+Value3)/3;
-
-            //DewPoint = DP_In.read()*100.0f;
+        if(Cycledryercounter >= 1) {
+            SetPoint = -110;
 
-            lcd.locate(10,0);
-            lcd.printf("      ");
-            lcd.locate(10,0);
-            lcd.printf("%3.1f%", DewPoint);
-            lcd.locate(10,1);
-            lcd.printf("      ");
-            lcd.locate(10,1);
-            lcd.printf("%3.1f%", SetPoint);
-
-
-            if ((DewPoint <  SetPoint)  && (secondscounter == LHCT)) {
-                secondscounter = secondscounter - 1;
+        } else {
+            if(my_Dip2 ==0) {
+                if(my_Dip1 ==0) {
+                    SetPoint = -20;
+                }
+                if(my_Dip1 ==1) {
+                    SetPoint = -40;
+                }
             }
-
-            if ((DewPoint <  SetPoint)  && (secondscounter == CT)) {
-                secondscounter = secondscounter - 1;
+            if(my_Dip2 ==1) {
+                if(my_Dip1 ==0) {
+                    SetPoint = -60;
+                }
+                if(my_Dip1 ==1) {
+                    SetPoint = -80;
+                }
             }
-
-            //} else {
-            //    lcd.locate(0,0);
-            //    lcd.printf("    Standard    ");
-            //    lcd.locate(0,1);
-            //    lcd.printf("     Cycle      ");
-            //}
         }
 
         if (secondscounter < PT) {
@@ -171,14 +131,14 @@
 
         }
 
-        if ((secondscounter > LHCT) && (secondscounter < LHPT)) {
+        if ((secondscounter > LHCT) && (secondscounter < RHPT)) {
             my_RHMV = 0;//1
             my_LHMV = 1;//0
             my_RHPV = 0;//1
             my_LHPV = 1;//0
             my_RV = 0;//1
         }
-        if (secondscounter >= LHPT && (secondscounter < CT)) {
+        if (secondscounter >= RHPT && (secondscounter < CT)) {
             my_RHMV = 0;//1
             my_LHMV = 1;//0
             my_RHPV = 0;//1
@@ -186,8 +146,91 @@
             my_RV = 1;//0
         }
 
-        if (secondscounter == (CT + 1)) {
-            secondscounter = 0;
-        }
-    } // While(1)
-} //Void Main
+//This piece of code runs every second for ever:
+//______________________________________________
+
+        if ((currentMillis - previousMillis) >= OnTime) {
+            previousMillis = currentMillis;
+            wd.Service();
+            my_led = !my_led;
+
+            secondscounter = secondscounter + 1;
+            Counter = Counter + 1;
+
+            if(Cycledryercounter >= 1) {
+                Cycledryercounter = Cycledryercounter + 1;
+            }
+
+            if(Counter == 10) {
+                lcd.cls();
+                lcd.printf("Dew Point:");
+                lcd.locate(0,1);
+                lcd.printf("Set Point:");
+                Counter = 0;
+            }
+
+           // if(Valuecounter ==0) {
+              //  Value1 = DP_In.read()*100 - 80;
+            //}
+
+            //if(Valuecounter ==1) {
+            //    Value2 = DP_In.read()*100 - 80;
+            //}
+
+            //if(Valuecounter ==2) {
+            //    Value3 = DP_In.read()*100 - 80;
+            //}
+
+            //Valuecounter = Valuecounter +1;
+
+            DewPoint = DP_In.read()*100 - 100;
+
+            lcd.locate(10,0);
+            lcd.printf("      ");
+            lcd.locate(10,0);
+            lcd.printf("%3.1f%", DewPoint);
+            lcd.locate(10,1);
+            lcd.printf("      ");
+            lcd.locate(10,1);
+            lcd.printf("%3.1f%", SetPoint);
+
+
+//Delay cycle if the Dewpoint is lower than the setpoint:
+//_______________________________________________________
+
+            if ((DewPoint <  SetPoint)  && (secondscounter == LHCT)) {
+                secondscounter = secondscounter - 1;
+                Idlecounter = Idlecounter + 1;
+            }
+
+            if ((DewPoint <  SetPoint)  && (secondscounter == CT)) {
+                secondscounter = secondscounter - 1;
+                Idlecounter = Idlecounter + 1;
+            }
+
+            if (DewPoint >=  SetPoint) {
+                Idlecounter = 0;
+            };
+
+//Set Counters:
+//____________
+
+            if(Idlecounter == IdleTimeSeconds) {
+                Cycledryercounter = 1;
+            }
+
+            if(Cycledryercounter == LCycleseconds) {
+                Cycledryercounter = 0;
+            }
+
+            if(Valuecounter == 2) {
+                Valuecounter = 0;
+            }
+
+            if (secondscounter == (CT + 1)) {
+                secondscounter = 0;
+            }
+
+        } // Loop for every second
+    } // While(1) - Every millisecond
+} //Void Main - Once