Advertisement
DrAungWinHtut

waterlevel_control.ino

Nov 5th, 2024
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 10.78 KB | None | 0 0
  1.  
  2. #include <EEPROM.h>
  3.  
  4. const unsigned long debounceDelay = 50;
  5.  
  6. const int waterLevelSensorLow = 0;
  7. const int waterLevelSensorMedium = 1;
  8. const int waterLevelSensorHigh = 2;
  9.  
  10. const int btnPause = 3;
  11. int btnPauseState;
  12. int lastBtnPauseState = LOW;
  13. unsigned long lastBtnPauseDebounceTime = 0;
  14.  
  15. const int btnAutoManual = 4;
  16. int btnAutoManualState;
  17. int lastBtnAutoManualState = LOW;
  18. unsigned long lastBtnAutoManualDebounceTime = 0;
  19.  
  20. const int btnSemiAuto =5;
  21. int btnSemiAutoState;
  22. int lastSemiAutoState = LOW;
  23. unsigned long lastBtnSemiAutoDebounceTime = 0;
  24.  
  25. const int ledWaterLevelLow = 6;
  26. const int ledWaterLevelMedium = 7;
  27. const int ledWaterLevelHigh = 8;
  28.  
  29. const int ledAuto = 9;
  30. const int ledManual = 10;
  31. const int ledPause = 13;
  32. const int ledSemiAuto = A0;
  33.  
  34. const int waterMotor = A2;
  35. const int buzzer = A3;
  36.  
  37. const int sumpTankLowSensor = A4;
  38. const int ledSumpTankLow = A5;
  39.  
  40. const int ledRunning = 12;
  41. const int ledStandby = 11;
  42. int standbyLedBrightness = 0;
  43. int standbyLedFadeAmount = 2;
  44.  
  45. const int ledTimer = A1;
  46.  
  47. bool isPaused = false;
  48. bool isAuto = true;
  49. bool isSemiAuto = false;
  50.  
  51. bool isPauseLedOn = false;
  52. bool isRunningLedOn = false;
  53. bool isTimerLedOn = false;
  54.  
  55. const long ledBlinkingTimeInMilliSeconds = 200;
  56. long lastPauseLedChangedTime = 0;
  57. long lastRunningLedChangedTime = 0;
  58. long lastTimerLedChangedTime = 0;
  59.  
  60. //Water Level 0 = Empty, 1 = LOW, 2 = MEDIUM, 3 = HIGH
  61. int waterLevel = -1;
  62. int lastWaterLevel = -1;
  63.  
  64. bool runWaterMotor = false;
  65. bool isWaterMotorRunning = false;
  66.  
  67. bool isSumpTankLow = false;
  68.  
  69. long motorStarTime = 0;
  70. //5 minutes
  71. const long motorMaxRunTimeInMilliseconds = 30000;
  72.  
  73. void setup() {
  74.  
  75.   Serial.begin(9600);
  76.  
  77.   pinMode(btnPause, INPUT);
  78.   pinMode(btnAutoManual, INPUT);
  79.   pinMode(btnSemiAuto, INPUT);
  80.  
  81.   pinMode(ledPause, OUTPUT);
  82.   pinMode(ledAuto, OUTPUT);
  83.   pinMode(ledManual, OUTPUT);
  84.   pinMode(ledSemiAuto, OUTPUT);
  85.   pinMode(ledWaterLevelLow, OUTPUT);
  86.   pinMode(ledWaterLevelMedium, OUTPUT);
  87.   pinMode(ledWaterLevelHigh, OUTPUT);
  88.  
  89.   pinMode(waterLevelSensorHigh, INPUT);
  90.   pinMode(waterLevelSensorMedium, INPUT);
  91.   pinMode(waterLevelSensorLow, INPUT);
  92.  
  93.   pinMode(waterMotor, OUTPUT);
  94.   pinMode(buzzer, OUTPUT);
  95.  
  96.   pinMode(sumpTankLowSensor, INPUT);
  97.   pinMode(ledSumpTankLow, OUTPUT);
  98.  
  99.   pinMode(ledRunning, OUTPUT);
  100.   pinMode(ledStandby, OUTPUT);
  101.  
  102.   pinMode(ledTimer, OUTPUT);
  103.  
  104.   //readFromEEPROM();
  105.  
  106.   buzzerBeep();
  107.   buzzerBeep();
  108. }
  109.  
  110. void readFromEEPROM() {
  111.  
  112.   bool isEEPROMUpdated;
  113.   EEPROM.get(EEPROM.length() - 1, isEEPROMUpdated);
  114.  
  115.   if (!isEEPROMUpdated){
  116.     return;
  117.   }
  118.  
  119.   int eeAddress = 0;
  120.  
  121.   EEPROM.get(eeAddress, isPaused);
  122.   eeAddress += sizeof(bool);
  123.    
  124.   EEPROM.get(eeAddress, isAuto);
  125.   eeAddress += sizeof(bool);
  126.  
  127.   EEPROM.get(eeAddress, isSemiAuto);
  128.   eeAddress += sizeof(bool);
  129.  
  130.   EEPROM.get(eeAddress, lastWaterLevel);
  131.   eeAddress += sizeof(int);
  132.  
  133.   EEPROM.get(eeAddress, runWaterMotor);
  134.   eeAddress += sizeof(bool);
  135.  
  136.   EEPROM.get(eeAddress, isWaterMotorRunning);
  137.   eeAddress += sizeof(bool);
  138.  
  139.   EEPROM.get(eeAddress, motorStarTime);
  140.   eeAddress += sizeof(long);
  141. }
  142.  
  143. void loop() {
  144.  
  145.   updateSumpTankWaterStatus();
  146.   updateWaterLevel();
  147.   updateButtonStatuses();
  148.   handleWaterMotor();
  149.   updateEEPROM();
  150. }
  151.  
  152. void updateSumpTankWaterStatus() {
  153.   int status = digitalRead(sumpTankLowSensor);
  154.  
  155.   isSumpTankLow = !status;
  156.   digitalWrite(ledSumpTankLow, isSumpTankLow);
  157. }
  158.  
  159. void updateWaterLevel() {
  160.   setWaterLevel();
  161.   handleWaterLevelLed();
  162. }
  163.  
  164. void setWaterLevel() {
  165.  
  166.   int low = digitalRead(waterLevelSensorLow);
  167.   int medium = digitalRead(waterLevelSensorMedium);
  168.   int high = digitalRead(waterLevelSensorHigh);
  169.  
  170.   int calculatedWaterLevel;
  171.  
  172.   if (low == LOW && medium == LOW && high == LOW) {
  173.     calculatedWaterLevel = 3;
  174.   } else if (low == LOW && medium == LOW && high == HIGH) {
  175.     calculatedWaterLevel = 2;
  176.   } else if (low == LOW && medium == HIGH && high == HIGH) {
  177.     calculatedWaterLevel = 1;
  178.   } else if (low == HIGH && medium == HIGH && high == HIGH) {
  179.     calculatedWaterLevel = 0;
  180.   } else {
  181.     calculatedWaterLevel = -1;
  182.   }
  183.  
  184.   if(waterLevel != calculatedWaterLevel) {
  185.    
  186.     if(lastWaterLevel == -1) {
  187.       lastWaterLevel = calculatedWaterLevel;
  188.     } else {
  189.       lastWaterLevel = waterLevel;
  190.     }
  191.    
  192.     waterLevel = calculatedWaterLevel;
  193.   }
  194. }
  195.  
  196. void handleWaterLevelLed() {
  197.    
  198.   if (waterLevel == 1) {
  199.    
  200.     digitalWrite(ledWaterLevelHigh, LOW);
  201.     digitalWrite(ledWaterLevelMedium, LOW);
  202.     digitalWrite(ledWaterLevelLow, HIGH);
  203.    
  204.   } else if (waterLevel == 2) {
  205.    
  206.     digitalWrite(ledWaterLevelHigh, LOW);
  207.     digitalWrite(ledWaterLevelMedium, HIGH);
  208.     digitalWrite(ledWaterLevelLow, HIGH);
  209.    
  210.   } else if (waterLevel == 3) {
  211.    
  212.     digitalWrite(ledWaterLevelHigh, HIGH);
  213.     digitalWrite(ledWaterLevelMedium, HIGH);
  214.     digitalWrite(ledWaterLevelLow, HIGH);
  215.  
  216.   } else {
  217.  
  218.     digitalWrite(ledWaterLevelHigh, LOW);
  219.     digitalWrite(ledWaterLevelMedium, LOW);
  220.     digitalWrite(ledWaterLevelLow, LOW);
  221.   }
  222. }
  223.  
  224. void updateButtonStatuses() {
  225.   setAutoStatus();
  226.   setPauseStatus();
  227.   setSemiAutoStatus();
  228.  
  229.   handleAutoManualLeds();
  230.   handlePauseLed();
  231.   handleSemiAutoLed();
  232. }
  233.  
  234. void setAutoStatus() {
  235.  
  236.   int reading = digitalRead(btnAutoManual);
  237.  
  238.   if (reading != lastBtnAutoManualState) {
  239.     lastBtnAutoManualDebounceTime = millis();
  240.   }
  241.  
  242.   if ((millis() - lastBtnAutoManualDebounceTime) > debounceDelay) {
  243.     if (reading != btnAutoManualState) {
  244.       btnAutoManualState = reading;
  245.  
  246.       if (btnAutoManualState == HIGH) {
  247.         isAuto = !isAuto;
  248.         buzzerBeep();
  249.       }
  250.     }
  251.   }
  252.   lastBtnAutoManualState = reading;
  253. }
  254.  
  255. void handleAutoManualLeds() {
  256.  
  257.   bool isAutoLedOn = isAuto && !isPaused;
  258.   digitalWrite(ledAuto, isAutoLedOn);
  259.  
  260.   bool isManualLedOn = !isAuto && !isPaused;
  261.   digitalWrite(ledManual, isManualLedOn);
  262. }
  263.  
  264. void setPauseStatus() {
  265.  
  266.   int reading = digitalRead(btnPause);
  267.  
  268.   if (reading != lastBtnPauseState) {
  269.     lastBtnPauseDebounceTime = millis();
  270.   }
  271.  
  272.   if ((millis() - lastBtnPauseDebounceTime) > debounceDelay) {
  273.     if (reading != btnPauseState) {
  274.       btnPauseState = reading;
  275.  
  276.       if (btnPauseState == HIGH) {
  277.         isPaused = !isPaused;
  278.       }
  279.     }
  280.   }
  281.   lastBtnPauseState = reading;
  282. }
  283.  
  284. void handlePauseLed() {
  285.    
  286.   if(isPaused) {
  287.    
  288.     long timeElapsedAfterLastChange = millis() - lastPauseLedChangedTime;
  289.    
  290.     if(timeElapsedAfterLastChange > ledBlinkingTimeInMilliSeconds) {
  291.      
  292.       isPauseLedOn = !isPauseLedOn;
  293.       digitalWrite(ledPause, isPauseLedOn);
  294.       lastPauseLedChangedTime = millis();
  295.     }
  296.   } else {
  297.  
  298.     digitalWrite(ledPause, LOW);
  299.     isPauseLedOn = false;
  300.   }
  301.  
  302.   if(isPauseLedOn) {
  303.     buzzerBeep();
  304.   }
  305. }
  306.  
  307. void setSemiAutoStatus() {
  308.  
  309.   int reading = digitalRead(btnSemiAuto);
  310.  
  311.   if (reading != lastSemiAutoState) {
  312.     lastBtnSemiAutoDebounceTime = millis();
  313.   }
  314.  
  315.   if ((millis() - lastBtnSemiAutoDebounceTime) > debounceDelay) {
  316.     if (reading != btnSemiAutoState) {
  317.       btnSemiAutoState = reading;
  318.  
  319.       if (btnSemiAutoState == HIGH) {
  320.         isSemiAuto = !isSemiAuto && (waterLevel < 3 && waterLevel > 0);
  321.         buzzerBeep();
  322.       }
  323.     }
  324.   }
  325.   lastSemiAutoState = reading;
  326.  
  327.   if (waterLevel == 3) {
  328.     isSemiAuto = false;
  329.   }
  330. }
  331.  
  332. void handleSemiAutoLed() {
  333.   digitalWrite(ledSemiAuto, isSemiAuto && isAuto && !isPaused);
  334. }
  335.  
  336. void handleWaterMotor() {
  337.   setWaterMotorRunStatus();
  338.  
  339.   if(isWaterMotorRunning != runWaterMotor) {
  340.    
  341.     if(runWaterMotor) {
  342.       buzzerBeep();
  343.       buzzerBeep();
  344.       buzzerBeep();
  345.      
  346.       motorStarTime = millis();
  347.     }
  348.    
  349.     digitalWrite(waterMotor, runWaterMotor);
  350.     isWaterMotorRunning = runWaterMotor;
  351.    
  352.     if(!runWaterMotor) {
  353.       buzzerBeep();
  354.       buzzerBeep();
  355.       buzzerBeep();
  356.      
  357.       motorStarTime = 0;
  358.     }
  359.   }
  360.  
  361.   handleRunningLed();
  362.   handleSandbyLed();
  363.   handleTimerLed();
  364. }
  365.  
  366. void setWaterMotorRunStatus() {
  367.  
  368.   bool isWaterLevelLow = waterLevel == 0;
  369.   bool isWaterLevelLowToHigh = waterLevel < 3 && waterLevel > 0;
  370.   bool isTankFillingUp = isWaterLevelLowToHigh && waterLevel > lastWaterLevel;
  371.   bool runMotorAutoMode = isAuto && (isWaterLevelLow || isTankFillingUp);
  372.   bool runMotorSemiAutoMode = isSemiAuto && (isWaterLevelLowToHigh || isTankFillingUp);
  373.   bool isTimerUp = isAuto && (millis() - motorStarTime) >= motorMaxRunTimeInMilliseconds;
  374.  
  375.  
  376.   runWaterMotor = (runMotorAutoMode || runMotorSemiAutoMode || !isAuto) && !isPaused && !isSumpTankLow && !isTimerUp;
  377. }
  378.  
  379. void handleRunningLed() {
  380.    
  381.   if(isWaterMotorRunning) {
  382.    
  383.     long timeElapsedAfterLastChange = millis() - lastRunningLedChangedTime;
  384.    
  385.     if(timeElapsedAfterLastChange > ledBlinkingTimeInMilliSeconds) {
  386.      
  387.       isRunningLedOn = !isRunningLedOn;
  388.       digitalWrite(ledRunning, isRunningLedOn);
  389.       lastRunningLedChangedTime = millis();
  390.     }
  391.   } else {
  392.  
  393.     digitalWrite(ledRunning, LOW);
  394.     isRunningLedOn = false;
  395.   }
  396.  
  397.   if(isRunningLedOn) {
  398.     buzzerBeep();
  399.   }
  400. }
  401.  
  402. void handleSandbyLed() {
  403.  
  404.   if(!isWaterMotorRunning && (isAuto || isSemiAuto) && !isPaused) {      
  405.    
  406.     analogWrite(ledStandby, standbyLedBrightness);
  407.     standbyLedBrightness = standbyLedBrightness + standbyLedFadeAmount;
  408.  
  409.     if (standbyLedBrightness <= 0 || standbyLedBrightness >= 255) {
  410.         standbyLedFadeAmount = -standbyLedFadeAmount;
  411.     }
  412.    
  413.     delay(30);
  414.    
  415.   } else {
  416.     digitalWrite(ledStandby, LOW);
  417.   }
  418. }
  419.  
  420. void handleTimerLed() {
  421.  
  422.   if(isWaterMotorRunning && motorStarTime > 0 && isAuto){
  423.    
  424.     long timeElapsedAfterLastChange = millis() - lastTimerLedChangedTime;
  425.    
  426.     if(timeElapsedAfterLastChange > ledBlinkingTimeInMilliSeconds) {
  427.      
  428.       isTimerLedOn = !isTimerLedOn;
  429.       digitalWrite(ledTimer, isTimerLedOn);
  430.       lastTimerLedChangedTime = millis();
  431.     }
  432.   } else {
  433.      digitalWrite(ledTimer, LOW);
  434.      isTimerLedOn = false;
  435.   }
  436. }
  437.  
  438. void buzzerBeep() {
  439.   const int buzzerTime = 250;
  440.  
  441.   digitalWrite(buzzer, HIGH);
  442.   delay(buzzerTime);
  443.   digitalWrite(buzzer, LOW);
  444.   delay(buzzerTime);
  445. }
  446.  
  447. void updateEEPROM() {
  448.  
  449.   int eeAddress = 0;
  450.  
  451.   EEPROM.update(eeAddress, isPaused);
  452.   eeAddress += sizeof(bool);
  453.    
  454.   EEPROM.update(eeAddress, isAuto);
  455.   eeAddress += sizeof(bool);
  456.  
  457.   EEPROM.update(eeAddress, isSemiAuto);
  458.   eeAddress += sizeof(bool);
  459.  
  460.   EEPROM.update(eeAddress, lastWaterLevel);
  461.   eeAddress += sizeof(int);
  462.  
  463.   EEPROM.update(eeAddress, runWaterMotor);
  464.   eeAddress += sizeof(bool);
  465.  
  466.   EEPROM.update(eeAddress, isWaterMotorRunning);
  467.   eeAddress += sizeof(bool);
  468.  
  469.   EEPROM.update(eeAddress, motorStarTime);
  470.   eeAddress += sizeof(long);
  471.  
  472.   EEPROM.update(EEPROM.length() - 1, true);
  473. }
  474.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement