Pre treatment tank control

Dependencies:   mbed Grove_LCD_RGB_Backlight DebounceIn

Files at this revision

API Documentation at this revision

Comitter:
dattranvnfrr
Date:
Tue Oct 08 16:24:32 2019 +0000
Parent:
0:2f5d939d4a4a
Commit message:
fan on when door open

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 2f5d939d4a4a -r ca7bf48a2f71 main.cpp
--- a/main.cpp	Wed Sep 18 18:13:50 2019 +0000
+++ b/main.cpp	Tue Oct 08 16:24:32 2019 +0000
@@ -1,14 +1,22 @@
 #include "Grove_LCD_RGB_Backlight.h"
 #include "mbed.h"
 #include "DebounceIn.h"
-
+ 
+ 
+#define OUT_OFF    (0)
+#define OUT_ON     (1)
+ 
+#define IN_OFF     (1)
+#define IN_ON      (0)
+ 
+ 
 //input
-
-DebounceIn doorClosed(p5);             //Check door status
+DebounceIn doorClosed(p5);              //Check door status
 DebounceIn cycleStartBtt(p14);          //cycle start button 
-InterruptIn stopBtt(p15);                 //stop button
+DebounceIn stopBtt(p15);                //stop button
+//InterruptIn stopBtt(p15);             //stop button
 //DigitalIn pot(p20);
-
+ 
 //output
 DigitalOut doorInterlock(p6);           //Door interlock
 DigitalOut fanRelay(p7);                //Fan control
@@ -18,224 +26,433 @@
 DigitalOut damperRelay(p13);            //Damper solenoid control
 DigitalOut damperRelayAlt(p16);         //Damper solenoid toggle control                    
 DigitalOut vfd(p17);                    //VFD Start relay
-
+ 
 Grove_LCD_RGB_Backlight rgbLCD(p9, p10); //Timer display
-
-
+ 
+bool cancelPending = false;
+bool damperIsOpen;  
+bool simulateMode = false;
+ 
 I2C i2c(p9, p10);
 //Serial pc(USBTX, USBRX);
-
+ 
+ 
 //extern "C" void mbed_reset();
-
-void damperRotate(int damper)   //damper condition
-{
-    if(damper==0)
+ 
+// Function declarations
+void damperRotate(int damper);
+void fullLineItoA(int m, char* str);
+void countDown(int s);  //countdown in sec
+void fullStop();
+void debugMode();
+ 
+ 
+    
+int main()
+{ 
+    //on power up status
+    cycleStartBtt.mode(PullUp);
+    cycleStartBtt.set_debounce_us(1000);
+    cycleStartBtt.set_samples(100);
+    wait(.1);
+    
+    doorClosed.mode(PullUp);
+    doorClosed.set_debounce_us(1000);
+    doorClosed.set_samples(100);
+    wait(.1);
+    
+    stopBtt.mode(PullUp);
+    stopBtt.set_debounce_us(1000);
+    stopBtt.set_samples(100);
+    wait(.5);
+    
+    rgbLCD.clear();
+    rgbLCD.setRGB(0x00, 0x66, 0xaa);                 //set the color 
+    rgbLCD.locate(0,0);
+    
+    if(cycleStartBtt == IN_ON && stopBtt == IN_ON)
     {
-        damperRelayAlt=1;  //toggle damper relay
+        simulateMode = true; 
+        rgbLCD.print("Simulating....  ");     
         wait(5);
-        damperRelayAlt=0;   //time out to reduce wear   
-    }
-    else //damper==1
-    {
-        damperRelay=1;  //toggle damper relay
-        wait(5);
-        damperRelay=0;   //time out to reduce wear  
     }
-}
-
-void countDown(int s)  //countdown in sec
-{
-   int m=0;
-   char cnt[12];  // make a 'count' string
-   for(m=s; m>0; m--)
-   {
-       sprintf(cnt, "%i   ", m);
-       rgbLCD.locate(0,1);
-       rgbLCD.print(cnt);
-       wait(1); 
-    }
-}
-
-void fullStop()
-{
-    if(stopBtt==0)
+    if(stopBtt == IN_ON)
     {
-    //Display status
-    greenRelay=0;   //green light OFF
-    yellowRelay=0;   //yellow light OFF
-    redRelay=1;   //turn red light ON
+       debugMode();
+    }  
+    
+    //Display startup countdown
+    //Startin up and prep the tank
     rgbLCD.clear();
     rgbLCD.locate(0,0);
-    rgbLCD.print("CYCLE STOPPED.  "); 
-    vfd=0;             //pump off
-    doorInterlock=1;    //door latched
-    wait(2);
-    fanRelay=0;         //fan stops
-    damperRotate(0);    //damperRelay=0;      //damper closed
-
-    
-    //wait 60sec 
-    countDown(10);              
-    doorInterlock=0;    //door unlatched
-    
-    stopBtt.mode(PullUp);
-    wait(.1);
-    NVIC_SystemReset(); //reset system        
-    }
-}
+    rgbLCD.print("Starting up...  ");   
     
-int main()
-{
-   
-   //on power up status
-   cycleStartBtt.mode(PullUp);
-   wait(.1);
-   
-   doorClosed.mode(PullUp);
-   wait(.1);
-   
-   stopBtt.mode(PullUp);
-   wait(.1);
-   stopBtt.fall(&fullStop); // attach the address of the flip function to the rising edge
-   
-   
-   //check for closed door before running fan
-   while(doorClosed!=0)
-   {
+    vfd              = OUT_OFF;
+    damperIsOpen = false; // Assert and run the opener once
+    damperRotate(1); //vent opened;
+    if(!simulateMode)
+    {
+        //doorInterlock    = OUT_ON;
+        fanRelay         = OUT_ON;
+    }
+    greenRelay       = OUT_OFF;
+    yellowRelay      = OUT_OFF;
+    redRelay         = OUT_OFF;
+    
+    //start timer for 90sec
+    countDown(90);
+    
+    /*
+    //check for closed door before running fan
+    while(doorClosed!=IN_ON)
+    {
        rgbLCD.clear();
        rgbLCD.locate(0,0);
-       rgbLCD.print("Close the door   ");
-   }
+       rgbLCD.print("Door is opened  ");
+       wait(.25);
+    }
+    */
   
-   //Display startup countdown
-   //Startin up and prep the tank
-   rgbLCD.clear();
-   rgbLCD.setRGB(0x00, 0x66, 0xaa);                 //set the color 
-   rgbLCD.print("Starting up...  ");   
-    
-   vfd=0;
-   damperRotate(1); //vent opened;
-   doorInterlock=1;
-   fanRelay=1;
-   greenRelay=0;
-   yellowRelay=0;
-   redRelay=0;
-   
-   //start timer for 90sec
-   countDown(5);
+    //Tank is ready
+    rgbLCD.locate(0,0);
+    rgbLCD.print("Tank is ready!  ");
+    greenRelay       = OUT_ON;       //green light on
+    doorInterlock    = OUT_OFF;      //interlock unlatched, ready to load material into the tank
+    fanRelay         = OUT_OFF;      //fan off
     
-   //Tank is ready
-   rgbLCD.locate(0,0);
-   rgbLCD.print("Tank is ready!  ");
-   greenRelay=1;   //green light on
+    cancelPending = false;
    
-   //doorInterlock=0; //interlock unlatched, ready to load material into the tank
-   fanRelay=0;      //fan off
-   
-   while(1)
-   {
-                
-        if(doorClosed==0)    //door closed
+    while(1)
+    {    
+        if(cancelPending)
         {
-            rgbLCD.locate(0,1);
-            rgbLCD.print("Door closed      ");
-            greenRelay=1;   //green light stays on
-            yellowRelay=0;  //yellow light off
-            redRelay=0;     //red light off
+            cancelPending = false;
+            
+            //Display status
+            greenRelay      = OUT_OFF;  //green light OFF
+            yellowRelay     = OUT_OFF;  //yellow light OFF
+            redRelay        = OUT_ON;   //turn red light ON
+            rgbLCD.clear();
+            rgbLCD.locate(0,0);
+            rgbLCD.print("CYCLE STOPPED.  "); 
+            vfd             = OUT_OFF;  //pump off
+            if(doorClosed == IN_ON  && !simulateMode)
+            {
+                doorInterlock = OUT_ON; //door latched
+                damperRotate(1);    //damper opened
+                fanRelay    = OUT_ON; //fan on
+            }
             
-            doorInterlock=0; //door unlocked
-            fanRelay=0;            
-                            
-            
-            //cycle start button pressed - wash cycle starts
-            if(cycleStartBtt==0)    
+            //wait 60sec 
+            countDown(60);              
+            doorInterlock=OUT_OFF;      //door unlatched
+            damperRotate(0);            //damper closed
+            fanRelay        = OUT_OFF;  //fan stops
+            rgbLCD.locate(0,0);
+            rgbLCD.print("Tank is ready!  ");
+        }
+    
+        if(doorClosed==IN_ON)    //door closed
+        {
+            /*
+             *  Get set up for the wash, and wait for the start button
+             */ 
+            rgbLCD.locate(0,1);
+            rgbLCD.print("Door closed     ");
+            greenRelay      = OUT_ON;       //green light stays on
+            yellowRelay     = OUT_OFF;      //yellow light off
+            redRelay        = OUT_OFF;      //red light off           
+            doorInterlock   = OUT_OFF;      //door unlocked
+            fanRelay        = OUT_OFF;                            
+            while(cycleStartBtt != IN_ON && !(doorClosed == IN_OFF || cancelPending))
+            {
+                if(stopBtt == IN_ON)
+                {
+                    cancelPending = true;
+                    continue;    
+                }
+                wait(.25);
+            }
+            if(doorClosed == IN_OFF || cancelPending)
             {
-                
-                //Display status
-                rgbLCD.clear();
-                rgbLCD.locate(0,0);
-                rgbLCD.print("CYCLE STARTED!  ");     
-                greenRelay=0;       //green light off
-                yellowRelay=1;      //yellow light on                           
-                doorInterlock=1;    //door locked
-                fanRelay=0;         //fan stop
-                damperRotate(0);     //vent close
-                vfd=1;           //vfd for pump up to speed
-                
-                //Display wash cycle countdown
-                //start timer for 2min   
-                countDown(7);
-                
-                //finished washing
-                vfd=0;            //pump off
-                //Display status
-                rgbLCD.clear();
-                rgbLCD.locate(0,0);
-                rgbLCD.print("CYCLE FINISHED! ");           
-                countDown(5);
-                
-                //drip dry after washing
-                //Display status
-                rgbLCD.clear();
-                rgbLCD.locate(0,0);
-                rgbLCD.print("DRIP DRYING!    ");                
-                                
-                //start timer for 5min  
-                countDown(10);
-
-                //Display status
-                rgbLCD.clear();
-                rgbLCD.locate(0,0);
-                rgbLCD.print("AIR CYCLING!    ");                                 
-                damperRotate(1);    //vent opened
-                fanRelay=1;         //fan on
-                
-                //Display air cycling countdown
-                //start timer for 2min
-                countDown(10);  
-                
-                //finished cycling air             
-                greenRelay=1;       //green light on
-                yellowRelay=0;      //yellow light off
-                doorInterlock=0;    //door unlocked
-                fanRelay=0;         //fan off
-                damperRotate(0);    //damperRelay=0;      //vent closed
-                
-                //Tank is ready
-                rgbLCD.clear();
-                rgbLCD.locate(0,0);
-                rgbLCD.print("Tank is ready!  ");
+                continue;
+            }
+            
+                        
+            /*
+             * Cycle start button pressed - wash for 2 minutes
+             */
+            rgbLCD.clear();
+            rgbLCD.locate(0,0);
+            rgbLCD.print("CYCLE STARTED!  ");     
+            greenRelay      = OUT_OFF;  //green light off
+            yellowRelay     = OUT_ON;   //yellow light on   
+            fanRelay        = OUT_OFF;  //fan stop                        
+            //damperRotate(0);            //vent already close
+            
+            if(!simulateMode)
+            {
+                doorInterlock   = OUT_ON;   //door locked
+                vfd             = OUT_ON;   //vfd for pump up to speed
+            }
+            countDown(120);             //start timer for 2min
+            if(doorClosed == IN_OFF || cancelPending)
+            {
+                continue;
+            }
+            
+            
+            /*
+             * Finished washing - turn off the pump
+             */
+            vfd = OUT_OFF;              //pump off
+            rgbLCD.clear();
+            rgbLCD.locate(0,0);
+            rgbLCD.print("CYCLE FINISHED! ");           
+            wait(1);
+            if(doorClosed == IN_OFF || cancelPending)
+            {
+                continue;
             }
- /*           
-            //Stop button is hit
-            if(stopBtt==1)
+            
+            
+            /*
+             * Drip dry after washing for 5 minutes
+             */
+            rgbLCD.clear();
+            rgbLCD.locate(0,0);
+            rgbLCD.print("DRIP DRYING!    ");                
+            countDown(300);
+            if(doorClosed == IN_OFF || cancelPending)
             {
-                //Display status
-                rgbLCD.locate(0,0);
-                rgbLCD.print("CYCLE STOPPED.  "); 
-                vfd=0;             //pump off
-                doorInterlock=1;    //door latched
-                wait(2);
-                fanRelay=0;         //fan stops
-                damperRotate(0);    //damperRelay=0;      //damper closed
-                
-                //wait 60sec 
-                countDown(10);              
-                doorInterlock=0;    //door unlatched
-            }  
-*/
+                continue;
+            }
+    
+    
+            /*
+             * Change the air out for 2 minutes
+             */
+            rgbLCD.clear();
+            rgbLCD.locate(0,0);
+            rgbLCD.print("AIR CYCLING!    ");                                 
+            damperRotate(1);            //vent opened
+            if(!simulateMode)
+            {
+                fanRelay = OUT_ON;          //fan on
+            }
+            countDown(120);  
+            if(doorClosed == IN_OFF || cancelPending)
+            {
+                continue;
+            }            
+            
+            
+            /*
+             * Entire cycle is done, close up and go back to waiting
+             */             
+            greenRelay      = OUT_ON;       //green light on
+            yellowRelay     = OUT_OFF;      //yellow light off
+            doorInterlock   = OUT_OFF;      //door unlocked
+            fanRelay        = OUT_OFF;      //fan off
+            damperRotate(0);               //vent closed
+            rgbLCD.clear();
+            rgbLCD.locate(0,0);
+            rgbLCD.print("Tank is ready!  ");
         }
         else                    //door open
         {
             rgbLCD.locate(0,1);
-            rgbLCD.print("Door opened     ");
-            greenRelay=0;   //green light OFF
-            redRelay=1;   //turn red light ON
-            yellowRelay=0;
-            fanRelay=0;         //fan stops
-            damperRotate(1);    //damperRelay=1;      //damper closed            
-
+            rgbLCD.print("Door open       ");
+            greenRelay  = OUT_OFF;  //green light OFF
+            redRelay    = OUT_ON;   //turn red light ON
+            yellowRelay = OUT_OFF;
+            damperRotate(1);        //damper opened
+            fanRelay    = OUT_ON;  //fan on                  
         }
             
-   }
+        wait(1);
+    }
+}
+ 
+ 
+ 
+ 
+void damperRotate(int damper)   //damper condition
+{
+    if(damper==0 && damperIsOpen == true && !simulateMode)
+    {
+        damperRelayAlt=OUT_ON;  //toggle damper relay
+        rgbLCD.locate(0,1);
+        rgbLCD.print("Closing damper  ");
+        wait(30);
+        damperRelayAlt=OUT_OFF; //time out to reduce wear   
+        damperIsOpen = false;
+    }
+    else if(damper == 1 && damperIsOpen == false && !simulateMode)
+    {
+        damperRelay=OUT_ON;     //toggle damper relay
+        rgbLCD.locate(0,1);
+        rgbLCD.print("Opening damper  ");
+        wait(30);
+        damperRelay=OUT_OFF;    //time out to reduce wear  
+        damperIsOpen = true;
+    }
+}
+ 
+void fullLineItoA(int m, char* str)
+{
+    if(str == NULL)
+    {
+        return;
+    }
+    
+    // Empty the array to 16 white spaces
+    for(int j=0; j < 16; j++)
+    {
+        str[j] = ' ';   
+    }
+    str[16] = 0;
+ 
+    // Put our counter in it, with the least-significant digit in col 4.
+    int i = 3;       
+    do
+    {
+        str[i] = m%10 + '0';
+        m /= 10;
+        i--;
+    }while(m > 0 && i >= 0);
+}
+ 
+void countDown(int s)  //countdown in sec
+{
+    int m=0;
+    char count[12];  // make a 'count' string
+ 
+    for(m=s; m>0; m--)
+    {
+        if(stopBtt == IN_ON)
+        {
+           cancelPending = true;
+           break;
+        }
+ 
+        fullLineItoA(m, count);
+ 
+        rgbLCD.locate(0,1);
+        rgbLCD.print(count);
+        
+        wait(1); 
+    }
+}
+ 
+ 
+void debugMode()
+{
+    char displayLine1[17];
+    char displayLine2[17];
+    unsigned int count = 0;
+ 
+    rgbLCD.clear();
+    rgbLCD.locate(0,0);
+    //damperRotate(1);
+    
+    do
+    {
+        strcpy(displayLine1, "Door= ");
+        if(doorClosed == 1)
+        {
+            strcat(displayLine1, "1");
+        }
+        else
+        {
+            strcat(displayLine1, "0");
+        }
+    
+        strcat(displayLine1, ",Start=");
+        if(cycleStartBtt == 1)
+        {
+            strcat(displayLine1, "1 ");
+            if(stopBtt == 0)
+            {
+                fanRelay    = OUT_OFF;
+            }
+        }
+        else
+        {
+            strcat(displayLine1, "0 ");
+            fanRelay    = OUT_ON;
+        }
+    
+        
+        strcpy(displayLine2, "Stop= ");
+        if(stopBtt == 1)
+        {
+            strcat(displayLine2, "1 ");
+        }
+        else
+        {
+            strcat(displayLine2, "0 ");
+            
+        }
+               
+        // Every 2s, change the outputs
+        if((count&7) == 0)
+        {
+            doorInterlock   = OUT_OFF;      //Door interlock
+            //fanRelay        = OUT_ON;      //Fan control
+            redRelay        = OUT_OFF;      //Red light 
+            yellowRelay     = OUT_OFF;      //Yellow light 
+            greenRelay      = OUT_OFF;      //Green light 
+            damperRelay     = OUT_OFF;      //Damper solenoid control
+            damperRelayAlt  = OUT_OFF;      //Damper solenoid toggle control                    
+            vfd             = OUT_OFF;      //VFD Start relay
+            
+            switch((count>>3)%8)
+            {
+            case 0:
+                doorInterlock   = OUT_ON;      //Door interlock
+                strcat(displayLine2, " Lock   ");
+                break;
+            case 1:
+                //fanRelay        = OUT_ON;      //Fan control
+                //strcat(displayLine2, " Fan    ");
+                break;
+            case 2:
+                redRelay        = OUT_ON;      //Red light 
+                strcat(displayLine2, " Red    ");
+                break;
+            case 3:
+                yellowRelay     = OUT_ON;      //Yellow light 
+                strcat(displayLine2, " Yellow ");
+                break;
+            case 4:
+                greenRelay      = OUT_ON;      //Green light 
+                strcat(displayLine2, " Green  ");
+                break;
+            case 5:
+                //damperRelayAlt  = OUT_ON;      //Damper solenoid toggle control - closed                 
+                //strcat(displayLine2, " Damper1");
+                //wait(30);
+                break;
+            case 6:
+                //damperRelay     = OUT_ON;      //Damper solenoid control - open                    
+                //strcat(displayLine2, " Damper2");
+                //wait(30);
+                break;
+            case 7:
+                //vfd             = OUT_ON;      //VFD Start relay
+                //strcat(displayLine2, " VFD    ");
+                break;
+            }
+        }
+        
+        rgbLCD.locate(0,0);
+        rgbLCD.print(displayLine1);
+        rgbLCD.locate(0,1);
+        rgbLCD.print(displayLine2);
+        
+        wait(0.25);
+        count++;
+    }
+    while(cycleStartBtt == IN_OFF || stopBtt == IN_OFF || doorClosed == IN_OFF);    
 }
\ No newline at end of file