Advertisement
frenky666

ac-condensate-pump-control

Jul 3rd, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // PIN DEFINITIONS
  2.  
  3. #define SENS_WL_1 A0        // water level sensor for tank 1 (analog in pin)
  4. #define SENS_WL_2 A1        // water level sensor for tank 2 (analog in pin)
  5. #define SENS_WL_3 A2        // water level sensor for tank 3 (analog in pin)
  6.  
  7. #define PUMP_1 2            // pump for tank 1 (digital pin, can be PWM)
  8. #define PUMP_2 3            // pump for tank 1 (digital pin, can be PWM)
  9. #define PUMP_3 4            // pump for tank 1 (digital pin, can be PWM)
  10.  
  11. // END: PIN DEFINITIONS
  12.  
  13. //#define DEBUG               // debug - sensor states and motor outputs
  14. //#define DEBUG_V             // verbose debug - analog data
  15.  
  16. #define RES_CNT 3           // number of tanks
  17.  
  18. #define TIME_LOOP 500       // don't run at maximum speed, it is not needed
  19. #define TIME_TRIG 2000      // wait this amount of time after trigger
  20. #define TIME_RUN  30000     // run pump for this amount of millis
  21.  
  22. #define TIME_MAX 14400000   // 4 hours - a "bit" less then 4294967295
  23.  
  24. const unsigned long sensorDebounce = TIME_TRIG;
  25. const unsigned long pumpRunTime = TIME_RUN;
  26.  
  27. int i;
  28. int aread;
  29. bool state;
  30.  
  31. unsigned long millisNow;
  32.  
  33. int            pumps[]            = { PUMP_1   , PUMP_2   , PUMP_3    };
  34. int            sensors[]          = { SENS_WL_1, SENS_WL_2, SENS_WL_3 };
  35. int            sensorLevels[]     = { 500      , 500      , 500       };
  36. bool           sensorStates[]     = { false    , false    , false     };
  37. bool           lastSensorStates[] = { false    , false    , false     };
  38. unsigned long  sensorDebounces[]  = { 0        , 0        , 0         };
  39. unsigned long  pumpTimes[]        = { 0        , 0        , 0         };
  40.  
  41.  
  42. void setup() {
  43.   for (i = 0; i < RES_CNT; i++) {
  44.     pinMode(pumps[i], OUTPUT);
  45.     digitalWrite(pumps[i], LOW);
  46.   }
  47. #ifdef DEBUG
  48.   Serial.begin(9600);
  49. #endif
  50. }
  51.  
  52. void(* restartController) (void) = 0;
  53.  
  54. void loop() {
  55.   // restart controller if it was running for more then 4 hours
  56.   millisNow = millis() + pumpRunTime;
  57.   if (millisNow >= TIME_MAX) {
  58.     restartController();
  59.   }
  60.  
  61.   for (i = 0; i < RES_CNT; i++) {
  62.     // Sensor reading with delay from first detection
  63.     aread = analogRead(sensors[i]);
  64.     state =  aread < sensorLevels[i];
  65.  
  66. #ifdef DEBUG_V
  67.     Serial.print("Sensor ");
  68.     Serial.print(i);
  69.     Serial.print(", value ");
  70.     Serial.print(aread);
  71.     Serial.print("\n");
  72. #endif
  73.  
  74.     if (state != lastSensorStates[i]) {
  75.       sensorDebounces[i] = millisNow;
  76.     }
  77.  
  78.     if ((millisNow - sensorDebounces[i]) > sensorDebounce && state != sensorStates[i]) {
  79.       // sensor was on for at least TIME_TRIG
  80.       sensorStates[i] = state;
  81.  
  82.       if (state && pumpTimes[i] == 0) {
  83.         pumpTimes[i] = millisNow;
  84.       }
  85.     }
  86.  
  87.     lastSensorStates[i] = state;
  88.     // END: Sensor reading
  89.  
  90.     // Pump control
  91.     if (shouldMotorRun(i)) {
  92.       digitalWrite(pumps[i], HIGH);
  93.     }
  94.     else {
  95.       pumpTimes[i] = 0;
  96.       digitalWrite(pumps[i], LOW);
  97.     }
  98.     // END: Pump control
  99.   }
  100.  
  101. #ifdef DEBUG
  102.   sendDebug();
  103. #endif
  104.   delay(TIME_LOOP);
  105. }
  106.  
  107. bool shouldMotorRun(int i) {
  108.   return millisNow < pumpTimes[i] + pumpRunTime;
  109. }
  110.  
  111. #ifdef DEBUG
  112. void sendDebug() {
  113.   Serial.println("S1 M1 S2 M2 S3 M3");
  114.   for (i = 0; i < RES_CNT; i++) {
  115.     Serial.print(' ');
  116.     Serial.print(sensorStates[i] ? '+' : '-');
  117.     Serial.print(' ');
  118.     Serial.print(' ');
  119.     Serial.print(shouldMotorRun(i) ? '+' : '-');
  120.     Serial.print(' ');
  121.   }
  122.   Serial.println("");
  123. }
  124. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement