Advertisement
Guest User

Untitled

a guest
Jan 30th, 2020
33
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define RELAY_PIN 2
  2. #define HUMIDITY_SENSOR_VCC_C1 3
  3. #define HUMIDITY_SENSOR_PIN_C1 A4 // Down sensor
  4. #define HUMIDITY_SENSOR_VCC_C2 4
  5. #define HUMIDITY_SENSOR_PIN_C2 A5 // Up sensor
  6. #define HUMIDITY_TRESHOLD 300
  7.  
  8. static int _g_pump_cycles = 0;
  9. #define MAX_PUMP_CYCLES 10
  10.  
  11. #define LED_SCANNING    LED_BUILTIN // = 13
  12. #define LED_PUMPING     5
  13. #define LED_WARNING     6
  14. #define LED_OK      7
  15.  
  16.  
  17. int _read_humidity_sensor(int __vcc, int __sensor) {
  18.   int __value;
  19.   ::digitalWrite(__vcc, HIGH); // Power up sensor
  20.   ::digitalWrite(LED_SCANNING, HIGH); // Scanning led on to show activity
  21.   ::delay(500); // Wait a little bit until sensor is ready
  22.  
  23.   __value = ::analogRead(__sensor);
  24.  
  25.   ::digitalWrite(__vcc, LOW); // Sensor off
  26.   ::digitalWrite(LED_SCANNING, LOW); // Activity led off
  27.   return 1023 - __value;// Capture range is 0..1023
  28. }
  29.  
  30. // Helpers
  31. void _pump_on() {
  32.     ::digitalWrite(RELAY_PIN, LOW); // Pump on  
  33.     ::digitalWrite(LED_PUMPING, HIGH);  
  34.     _g_pump_cycles++;
  35. }
  36. void _pump_off() {
  37.     ::digitalWrite(RELAY_PIN, HIGH);// Pump off
  38.     ::digitalWrite(LED_PUMPING, LOW);  
  39.     _g_pump_cycles = 0;
  40. }
  41.  
  42. void setup() {
  43.   // Usual pin setup
  44.   ::pinMode(LED_SCANNING, OUTPUT);
  45.   ::pinMode(LED_PUMPING, OUTPUT);
  46.   ::pinMode(LED_WARNING, OUTPUT);
  47.   ::pinMode(LED_OK, OUTPUT);
  48.   ::pinMode(RELAY_PIN, OUTPUT);
  49.   ::pinMode(HUMIDITY_SENSOR_VCC_C1, OUTPUT);
  50.   ::pinMode(HUMIDITY_SENSOR_VCC_C2, OUTPUT);
  51.   // Ensure pump is off
  52.   _pump_off();
  53.   ::delay(2000); // Safety delay
  54. }
  55.  
  56. void loop() {
  57.   // Read both sensors
  58.   bool __c1 = _read_humidity_sensor(HUMIDITY_SENSOR_VCC_C1, HUMIDITY_SENSOR_PIN_C1) > HUMIDITY_TRESHOLD,
  59.        __c2 = _read_humidity_sensor(HUMIDITY_SENSOR_VCC_C2, HUMIDITY_SENSOR_PIN_C2) > HUMIDITY_TRESHOLD;
  60.  
  61.   // Update the green led
  62.   if(!__c1 && !__c2) {
  63.     ::digitalWrite(LED_WARNING, LOW); // Reset warning
  64.     ::digitalWrite(LED_OK, HIGH);
  65.   } else {
  66.     ::digitalWrite(LED_OK, LOW);
  67.   }  
  68.  
  69.   if(__c2 && !__c1) { // Should not happen; go safety mode
  70.     _pump_off();
  71.     ::digitalWrite(LED_WARNING, HIGH);
  72.     ::delay(60*1000); // Wait 1 minute
  73.     return;
  74.   }
  75.  
  76.   if(__c2) { // up sensor on => Pump on
  77.     _pump_on();
  78.   } else {
  79.     if(!__c1) { // down sensor off => Pump off
  80.         _pump_off();
  81.     } else {
  82.         // c2 is off, c1 is on => Pump is working; do nothing except safety check in case of c1 failure
  83.         if(_g_pump_cycles > MAX_PUMP_CYCLES) { // Pumping too much; something is wrong, wait a little bit
  84.             _pump_off();
  85.             ::delay(60*1000); // Wait 1 minute
  86.         } else {
  87.             // Nothing to do
  88.         }
  89.     }
  90.   }
  91.  
  92.   ::delay(5000);// Next loop in 5 secs
  93. }
Advertisement
RAW Paste Data Copied
Advertisement