Advertisement
hms11

ZoneControlRev2

Sep 24th, 2021
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.44 KB | None | 0 0
  1. // --------------------- ZONE COMMAND HV TACO ---------------------
  2. // ---------------------4 ZONE CONTROL BOARD-------------------
  3. // ------------------------I2C LCD DISPLAY---------------------
  4. // --------------------4 USER INPUT BUTTONS--------------------
  5. // -------------------DESIGNED AND PROGRAMMED BY:--------------
  6. // --------------------------COREY EARL-------------------------
  7. // ----------------REVISION 4.2 (Sept 6th, 2021)----------------
  8. // ----------------All Zones Appear to Function----------------
  9. // ---------------------More Testing Needed--------------------
  10. //----------------Ready for on-site BETA testing---------------
  11.  
  12. //----------------------------FEATURES:-----------------------------
  13. //-----------------Easy to deactivate/activate each zone------------
  14. //----------------Easy trigger point setting per zone---------------
  15. //-----------User Adjustable and Auto-Adjustable Pump Times---------
  16. //----Also able to Operate Without Sensors on a ~24hr Pump Cycle----
  17.  
  18. //--------------------------ISSUES:---------------------------------
  19. //-------EEPROM not working right, more testing needed--------
  20.  
  21. //--------------------------------------------------------
  22.  
  23. // Libraries
  24. #include <EEPROM.h>
  25. #include <LiquidCrystal_I2C.h>
  26. #include <Wire.h>
  27. LiquidCrystal_I2C lcd(0x27, 20, 4); // Set I2C Address and display size
  28.  
  29. // Which zones are active. If a zone is active set as true, if a zone is
  30. // deactived or not needed set to false. Zones are then placed in an array.
  31.  
  32. bool zoneActive[] = {true,true,true,true};
  33. bool zoneManual[] = {false,false,false,false};
  34. bool zoneAuto[] = {true,true,true,true};
  35. bool zoneAutoTimer[] = {false,false,false,false};
  36. bool zonePump[] = {false,false,false,false};
  37. bool zoneManualPump[] = {false,false,false,false};
  38.  
  39. // Counters and bools for progressing through each Zone and Manipulating the LCD menu
  40.  
  41. int zoneNumber = 0; // Which zone the loop is controlling
  42. int menuZone = 0; // Which Zone the menu is viewing/adjusting
  43. int lastMenuZone = 0;
  44.  
  45.  
  46.  
  47. // Sensor trigger points to trigger each zones output on/off.
  48.  
  49. // NOTE: GIKFUN 1.2 Moisture sensors seem to range from ~340(Capacitor submerged
  50. // in water) to ~670(Dry air)
  51. // GIKFUN 2.0 Seem to run this range (~290 wet, ~700 Dry) but responds
  52. // much slower as moisture increases, responds quickly to drying). These
  53. // tests done with factory ~8" jumper harness. Will retest with final
  54. // length harnesses and adjust MiniTaco V1.0 Soil Sensors seem to run from
  55. // ~700 - ~930. - 8" Harness, retest with 7.5' Harness MiniTaco V1.5 Soil
  56. // Sensors seem to run from ~490 - ~890. - 8" Harness, retest with 7.5'
  57. // Harness
  58.  
  59. int triggerHigh[] = {550, 550, 550, 550};
  60. int triggerLow[] = {670, 670, 670, 670};
  61.  
  62. // ---Pin Assignments--
  63.  
  64. // Set Pin Assignments for User Input Buttons
  65.  
  66. const int button1 = 10;
  67. const int button2 = 11;
  68. const int button3 = 12;
  69. const int button4 = 13;
  70.  
  71. // Set Pin Assignments for Zone Status LED's
  72.  
  73. const int zoneLED[] = {6, 7, 8, 9}; // Zone 1-4 pins for LEDs
  74.  
  75. // Set pin assignments for OUT pins
  76.  
  77. const int output[] = {2, 3, 4, 5}; // Zone 1-4 pins for outputs to relays
  78.  
  79. const int sensPin[] = {A0, A1, A2, A3}; // Zone 1-4 Analog ADC pins
  80.  
  81. // --Variables--
  82.  
  83. // Menu Related Variables
  84. bool menuOn = true; // state of the display menu
  85. int optionSelect = 0; // which menu option is selected
  86. int lastOptionSelect = 0;
  87. int lastItemSelect = 0;
  88. int itemSelect = 0;
  89. int buttonState1 = 0; // current state of each button
  90. int buttonState2 = 0;
  91. int buttonState3 = 0;
  92. int buttonState4 = 0;
  93. int lastButtonState1 = 0; // previous state of each button
  94. int lastButtonState2 = 0;
  95. int lastButtonState3 = 0;
  96. int lastButtonState4 = 0;
  97. unsigned long displayTimer = 10000; // timer to automatically turn off the display
  98. unsigned long lastDisplayTimer = 0;
  99.  
  100.  
  101.  
  102. // Delay Variables
  103.  
  104. const int manualDayTimer[] = {86400000, 86400000, 86400000, 86400000}; // delay for once a day manual watering mode
  105. unsigned long lastManualDayTimer[] = {0, 0, 0, 0,};
  106. const int sensorReadDelay[] = {600000, 600000, 600000, 600000}; // delay between sensor readings in millis
  107. unsigned long lastSensorReadTime[] = {0, 0, 0, 0}; // the last time a sensor was read
  108.  
  109. // Pump timers to monitor zone "on" time of each output. 60000 = 1 minutes
  110.  
  111. unsigned long pumpTimer[] = {60000, 60000, 60000, 60000}; //Pump timers in array
  112.  
  113. // Last value of the Pump timers
  114.  
  115. unsigned long lastPumpTimer[] = {0, 0, 0, 0}; // last value of Pump timers in array
  116.  
  117. // Analog sensor reading values sensors 1-4
  118.  
  119. int sensor[] = {0, 0, 0, 0}; // Sensor reading values array
  120.  
  121.  
  122. // EEPROM addresses
  123. const int zone1TrigLowAddress = 0;
  124. const int zone2TrigLowAddress = 1;
  125. const int zone3TrigLowAddress = 2;
  126. const int zone4TrigLowAddress = 3;
  127. const int zone1TrigHighAddress = 4;
  128. const int zone2TrigHighAddress = 5;
  129. const int zone3TrigHighAddress = 6;
  130. const int zone4TrigHighAddress = 7;
  131. const int zone1OnAddress = 8;
  132. const int zone2OnAddress = 9;
  133. const int zone3OnAddress = 10;
  134. const int zone4OnAddress = 11;
  135. const int firstBootAddress = 12; // Address to tell the memory if this is the first time this chip has run.
  136. int firstBootCount;
  137.  
  138.  
  139. void setup() {
  140.  
  141. // firstBootCount = EEPROM.read(firstBootAddress);
  142. // if (firstBootCount == 90) {
  143. // triggerLow[0] = EEPROM.read(zone1TrigLowAddress);
  144. // triggerLow[1] = EEPROM.read(zone2TrigLowAddress);
  145. // triggerLow[2] = EEPROM.read(zone3TrigLowAddress);
  146. // triggerLow[3] = EEPROM.read(zone4TrigLowAddress);
  147. // triggerHigh[0] = EEPROM.read(zone1TrigHighAddress);
  148. // triggerHigh[1] = EEPROM.read(zone2TrigHighAddress);
  149. // triggerHigh[2] = EEPROM.read(zone3TrigHighAddress);
  150. // triggerHigh[3] = EEPROM.read(zone4TrigHighAddress);
  151. // zoneActive[0] = EEPROM.read(zone1OnAddress);
  152. // zoneActive[1] = EEPROM.read(zone2OnAddress);
  153. // zoneActive[2] = EEPROM.read(zone3OnAddress);
  154. // zoneActive[3] = EEPROM.read(zone4OnAddress);
  155. // }
  156. // else if (firstBootCount != 90) {
  157. // EEPROM.write(zone1TrigLowAddress, 800);
  158. // EEPROM.write(zone2TrigLowAddress, 800);
  159. // EEPROM.write(zone3TrigLowAddress, 800);
  160. // EEPROM.write(zone4TrigLowAddress, 800);
  161. // EEPROM.write(zone1TrigHighAddress, 550);
  162. // EEPROM.write(zone2TrigHighAddress, 550);
  163. // EEPROM.write(zone3TrigHighAddress, 550);
  164. // EEPROM.write(zone4TrigHighAddress, 550);
  165. // EEPROM.write(zone1OnAddress, true);
  166. // EEPROM.write(zone2OnAddress, false);
  167. // EEPROM.write(zone3OnAddress, false);
  168. // EEPROM.write(zone4OnAddress, false);
  169. // EEPROM.write(firstBootAddress, 90);
  170. // }
  171.  
  172. // triggerLow[0] = EEPROM.read(zone1TrigLowAddress);
  173. // triggerLow[1] = EEPROM.read(zone2TrigLowAddress);
  174. // triggerLow[2] = EEPROM.read(zone3TrigLowAddress);
  175. // triggerLow[3] = EEPROM.read(zone4TrigLowAddress);
  176. // triggerHigh[0] = EEPROM.read(zone1TrigHighAddress);
  177. // triggerHigh[1] = EEPROM.read(zone2TrigHighAddress);
  178. // triggerHigh[2] = EEPROM.read(zone3TrigHighAddress);
  179. // triggerHigh[3] = EEPROM.read(zone4TrigHighAddress);
  180. // zoneActive[0] = EEPROM.read(zone1OnAddress);
  181. // zoneActive[1] = EEPROM.read(zone2OnAddress);
  182. // zoneActive[2] = EEPROM.read(zone3OnAddress);
  183. // zoneActive[3] = EEPROM.read(zone4OnAddress);
  184.  
  185. lcd.begin();
  186. lcd.clear();
  187. lcd.home();
  188. lcd.print(" --DOROTHY--");
  189. lcd.setCursor(0, 1);
  190. lcd.print(" Irrigation");
  191. lcd.setCursor(0, 2);
  192. lcd.print(" System 2.0");
  193. lcd.setCursor(0, 3);
  194. lcd.print(" LOADING...");
  195. delay(2000);
  196. lcd.clear();
  197. // set LED indicator pins to output.
  198. pinMode(zoneLED[0], OUTPUT);
  199. pinMode(zoneLED[1], OUTPUT);
  200. pinMode(zoneLED[2], OUTPUT);
  201. pinMode(zoneLED[3], OUTPUT);
  202. // set OUT pins to output
  203. pinMode(output[0], OUTPUT);
  204. pinMode(output[1], OUTPUT);
  205. pinMode(output[2], OUTPUT);
  206. pinMode(output[3], OUTPUT);
  207. // set Sensor pins to input
  208. pinMode(sensPin[0], INPUT);
  209. pinMode(sensPin[1], INPUT);
  210. pinMode(sensPin[2], INPUT);
  211. pinMode(sensPin[3], INPUT);
  212. // set Button pins to input
  213. pinMode(button1, INPUT);
  214. pinMode(button2, INPUT);
  215. pinMode(button3, INPUT);
  216. pinMode(button4, INPUT);
  217.  
  218. }
  219.  
  220. void displayMenu() {
  221. if (menuOn) { // if the display is supposed to be on
  222. lcd.display(); // turn the display on
  223. lcd.backlight();
  224.  
  225. switch (optionSelect) {
  226.  
  227. case 0:
  228. lcd.home();
  229. lcd.print(" -ZONE ");
  230. lcd.print(menuZone+1);
  231. lcd.print(" ");
  232. if (zoneActive[menuZone] && !zonePump[menuZone]) {
  233. lcd.print("ACTIVE");
  234. lcd.print("-");
  235. lcd.setCursor(0, 1);
  236. lcd.print("< >");
  237. lcd.setCursor(0, 2);
  238. lcd.print(" Press DOWN To See/");
  239. lcd.setCursor(0, 3);
  240. lcd.print(" Set Details");
  241. }
  242. else if (!zoneActive[menuZone] && !zonePump[menuZone] && !zoneManualPump[menuZone]) {
  243. lcd.print("DISABLED");
  244. lcd.print("-");
  245. lcd.setCursor(0, 1);
  246. lcd.print("< >");
  247. lcd.setCursor(0, 2);
  248. lcd.print(" Press DOWN To See/");
  249. lcd.setCursor(0, 3);
  250. lcd.print(" Set Details");
  251. }
  252. else if (zonePump[menuZone] || zoneManualPump[menuZone]) {
  253. lcd.print("PUMPING");
  254. lcd.print("-");
  255. lcd.setCursor(0, 1);
  256. lcd.print("< >");
  257. lcd.setCursor(0, 2);
  258. lcd.print(" Press DOWN To See/");
  259. lcd.setCursor(0, 3);
  260. lcd.print(" Set Details");
  261. }
  262. break;
  263.  
  264. case 1:
  265. lcd.home();
  266. lcd.print(" -ZONE ");
  267. lcd.print(menuZone+1);
  268. lcd.print(" SETTING-");
  269. lcd.setCursor(0, 1);
  270. lcd.print(" Moisture Level: ");
  271. lcd.print(sensor[menuZone]);
  272. lcd.setCursor(0, 2);
  273. lcd.print(" DRY SOIL SETTING");
  274. lcd.setCursor(0,3);
  275. lcd.print("<");
  276. lcd.setCursor(8, 3);
  277. lcd.print(triggerLow[menuZone]);
  278. lcd.setCursor(19,3);
  279. lcd.print(">");
  280. break;
  281.  
  282. case 2:
  283. lcd.home();
  284. lcd.print(" -ZONE ");
  285. lcd.print(menuZone+1);
  286. lcd.print(" SETTING-");
  287. lcd.setCursor(0, 1);
  288. lcd.print(" Pump Timer: ");
  289. lcd.setCursor(0, 2);
  290. lcd.print("< ");
  291. lcd.print(pumpTimer[menuZone] / 60000);
  292. lcd.print(" >");
  293. lcd.setCursor(0,3);
  294. lcd.print(" Minutes");
  295. break;
  296.  
  297. case 3:
  298. lcd.home();
  299. lcd.print(" -ZONE ");
  300. lcd.print(menuZone+1);
  301. lcd.print(" SETTING-");
  302. lcd.setCursor(0, 1);
  303. lcd.print(" ZONE: ");
  304. if (zoneAuto[menuZone] && !zonePump[menuZone] && !zoneManualPump[menuZone]) {
  305. lcd.print("AUTOMATIC");
  306. lcd.setCursor(0, 2);
  307. lcd.print(" PRESS < OR > TO:");
  308. lcd.setCursor(3, 3);
  309. lcd.print("GO TO MANUAL MODE");
  310. }
  311. else if (zoneManual[menuZone] && !zoneManualPump[menuZone] && !zonePump[menuZone]) {
  312. lcd.print("MANUAL");
  313. lcd.setCursor(0, 2);
  314. lcd.print(" PRESS < OR > TO:");
  315. lcd.setCursor(3, 3);
  316. lcd.print("GO TO AUTO MODE");
  317. }
  318. else if (zoneManualPump[menuZone]) {
  319. lcd.print("PUMPING");
  320. lcd.setCursor(0, 2);
  321. lcd.print(" PRESS < OR > TO:");
  322. lcd.setCursor(3, 3);
  323. lcd.print("GO TO AUTO MODE");
  324. }
  325. else if (zonePump[menuZone]) {
  326. lcd.print("PUMPING");
  327. lcd.setCursor(0, 2);
  328. lcd.print(" PRESS < OR > TO:");
  329. lcd.setCursor(3, 3);
  330. lcd.print("GO TO MANUAL MODE");
  331. }
  332. break;
  333.  
  334. case 4:
  335. lcd.home();
  336. lcd.print(" -ZONE ");
  337. lcd.print(menuZone+1);
  338. lcd.print(" SETTING-");
  339. lcd.setCursor(0, 1);
  340. lcd.print(" ZONE: ");
  341. if (zoneActive[menuZone] && !zonePump[menuZone]) {
  342. lcd.print("ACTIVE");
  343. lcd.setCursor(0, 2);
  344. lcd.print(" PRESS DOWN TO:");
  345. lcd.setCursor(4, 3);
  346. lcd.print("DISABLE");
  347. }
  348. else if (!zoneActive[menuZone] && !zonePump[menuZone] && !zoneManualPump[menuZone]) {
  349. lcd.print("DISABLED");
  350. lcd.setCursor(0, 2);
  351. lcd.print(" PRESS DOWN TO:");
  352. lcd.setCursor(4, 3);
  353. lcd.print("ACTIVATE");
  354. }
  355. else if (zonePump[menuZone] || zoneManualPump[menuZone]) {
  356. lcd.print("PUMPING");
  357. lcd.setCursor(0, 2);
  358. lcd.print(" PRESS DOWN TO:");
  359. lcd.setCursor(4, 3);
  360. lcd.print("DISABLE");
  361. }
  362. break;
  363. }
  364.  
  365. if ((unsigned long)(millis() - lastDisplayTimer) >= displayTimer) {
  366. menuOn = false;
  367. }
  368. } else if (!menuOn) { // if the display is supposed to be off
  369. lcd.noDisplay(); // turn the display off
  370. lcd.noBacklight();
  371. optionSelect = 0; // back to front screen
  372. itemSelect = 0;
  373. menuZone = 0;
  374. }
  375. if ((buttonState1 == 0) && (!menuOn)) {
  376. menuOn = true;
  377. lastDisplayTimer = millis();
  378. // optionSelect = 0; // back to front screen
  379. // menuZone = 0;
  380. // itemSelect = 0;
  381. lastButtonState1 = buttonState1;
  382. }
  383. }
  384.  
  385.  
  386. void readButtons() {
  387. buttonState1 = (digitalRead(button1));
  388. buttonState2 = (digitalRead(button2));
  389. buttonState3 = (digitalRead(button3));
  390. buttonState4 = (digitalRead(button4));
  391. if (buttonState1 != lastButtonState1) {
  392. if (buttonState1 == LOW) { // if the DOWN button has been pressed
  393. lcd.clear();
  394. lastDisplayTimer = millis();
  395. if (optionSelect < 4) { // if we are in the settings change and not passed the last screen.
  396. optionSelect++;
  397. }
  398. else if (optionSelect = 4) {
  399. if (zoneActive[menuZone]) {
  400. zonePump[menuZone] = false;
  401. zoneManualPump[menuZone] = false;
  402. zoneActive[menuZone] = false;
  403. digitalWrite(output[menuZone], LOW);
  404. digitalWrite(zoneLED[menuZone], LOW); // Turn Zone Indicator LED Off
  405. }
  406. else if (zonePump[menuZone]) {
  407. zonePump[menuZone] = false;
  408. zoneManualPump[menuZone] = false;
  409. zoneActive[menuZone] = false;
  410. digitalWrite(output[menuZone], LOW);
  411. digitalWrite(zoneLED[menuZone], LOW); // Turn Zone Indicator LED Off
  412. }
  413. else if (zoneManualPump[menuZone]) {
  414. zonePump[menuZone] = false;
  415. zoneManualPump[menuZone] = false;
  416. zoneActive[menuZone] = false;
  417. digitalWrite(output[menuZone], LOW);
  418. digitalWrite(zoneLED[menuZone], LOW); // Turn Zone Indicator LED Off
  419. }
  420. else if (!zoneActive[menuZone]) {
  421. zonePump[menuZone] = false;
  422. zoneManualPump[menuZone] = false;
  423. zoneActive[menuZone] = true;
  424. lastPumpTimer[menuZone] = millis();
  425. lastManualDayTimer[menuZone] = millis();
  426. digitalWrite(zoneLED[menuZone], HIGH);
  427. }
  428. optionSelect = 0;
  429. }
  430. }
  431. lastButtonState1 = buttonState1;
  432. }
  433. if (buttonState4 != lastButtonState4) {
  434. if (buttonState4 == LOW) { // if the UP button has been pressed
  435. lcd.clear();
  436. lastDisplayTimer = millis();
  437. if ((optionSelect != 0) && (optionSelect != 0)) { // if we are not in the settings change and not passed the first screen.
  438. optionSelect--;
  439. }
  440. }
  441. lastButtonState4 = buttonState4;
  442. }
  443.  
  444. if (optionSelect == 0) { // if we are not adjusting settings
  445.  
  446. if (buttonState2 != lastButtonState2) { // if the right button has been pressed
  447. if (buttonState2 == LOW) { // if the right button has been pressed
  448. lcd.clear();
  449. lastDisplayTimer = millis();
  450. if ((menuZone <= 3) && (menuZone != 3)) { // if we are not past the last menu screen
  451. menuZone++;
  452. }
  453. else if (menuZone == 3) { // if we are at the last menu screen
  454. menuZone = 0;
  455. }
  456. }
  457. lastButtonState2 = buttonState2;
  458. }
  459. if (buttonState3 != lastButtonState3) { // if the left button has been pressed
  460. if (buttonState3 == LOW) { // if the left button has been pressed
  461. lcd.clear();
  462. lastDisplayTimer = millis();
  463. if (menuZone > 0) { // if we are not at the first menu screen
  464. menuZone--;
  465. }
  466. else if (menuZone == 0) {
  467. menuZone = 3;
  468. }
  469. }
  470. lastButtonState3 = buttonState3;
  471. }
  472. }
  473.  
  474. else if (optionSelect == 1) { // if we are adjusting settings
  475. buttonState2 = (digitalRead(button2));
  476. buttonState3 = (digitalRead(button3));
  477.  
  478. if (buttonState2 != lastButtonState2) { // if the right button has been pressed
  479. if (buttonState2 == LOW) { // if the right button has been pressed
  480. lcd.clear();
  481. lastDisplayTimer = millis();
  482. if (triggerLow[menuZone] < 700) {
  483. triggerLow[menuZone] = (triggerLow[menuZone] + 10);
  484. }
  485. else if (triggerLow[menuZone] >= 700) {
  486. triggerLow[menuZone] = 700;
  487. }
  488. }
  489. lastButtonState2 = buttonState2;
  490. }
  491. if (buttonState3 != lastButtonState3) { // if the left button has been pressed
  492. if (buttonState3 == LOW) { // if the left button has been pressed
  493. lcd.clear();
  494. lastDisplayTimer = millis();
  495. if (triggerLow[menuZone] > 600) {
  496. triggerLow[menuZone] = (triggerLow[menuZone] - 10);
  497. }
  498. else if (triggerLow[menuZone] <= 600) {
  499. triggerLow[menuZone] = 600;
  500. }
  501. }
  502. lastButtonState3 = buttonState3;
  503. }
  504. }
  505.  
  506. else if (optionSelect == 2) { // if we are adjusting settings
  507. buttonState2 = (digitalRead(button2));
  508. buttonState3 = (digitalRead(button3));
  509.  
  510. if (buttonState2 != lastButtonState2) { // if the right button has been pressed
  511. if (buttonState2 == LOW) { // if the right button has been pressed
  512. lcd.clear();
  513. lastDisplayTimer = millis();
  514. if (pumpTimer[menuZone] < 600000) {
  515. pumpTimer[menuZone] = (pumpTimer[menuZone] + 60000);
  516. }
  517. else if (pumpTimer[menuZone] >= 600000) {
  518. pumpTimer[menuZone] = 600000;
  519. }
  520. }
  521. lastButtonState2 = buttonState2;
  522. }
  523. if (buttonState3 != lastButtonState3) { // if the left button has been pressed
  524. if (buttonState3 == LOW) { // if the left button has been pressed
  525. lcd.clear();
  526. lastDisplayTimer = millis();
  527. if (pumpTimer[menuZone] > 60000) {
  528. pumpTimer[menuZone] = (pumpTimer[menuZone] - 60000);
  529. }
  530. else if (pumpTimer[menuZone] <= 60000) {
  531. pumpTimer[menuZone] = 60000;
  532. }
  533. }
  534. lastButtonState3 = buttonState3;
  535. }
  536. }
  537.  
  538. else if (optionSelect == 3) { // if we are adjusting settings
  539. buttonState2 = (digitalRead(button2));
  540. buttonState3 = (digitalRead(button3));
  541.  
  542. if (buttonState2 != lastButtonState2 || buttonState3 != lastButtonState3) { // if the right button has been pressed
  543. if (buttonState2 == LOW || buttonState3 == LOW) { // if either button has been pressed
  544. lcd.clear();
  545. lastDisplayTimer = millis();
  546. if (zoneAuto[menuZone] && !zonePump[menuZone] && !zoneManualPump[menuZone]) {
  547. zoneAuto[menuZone] = false;
  548. lastManualDayTimer[menuZone] = millis();
  549. zoneManual[menuZone] = true;
  550. }
  551. else if (zoneManual[menuZone] && !zoneManualPump[menuZone] && !zonePump[menuZone]) {
  552. zoneManual[menuZone] = false;
  553. zoneAuto[menuZone] = true;
  554. }
  555. else if (zoneManualPump[menuZone]) {
  556. zoneManualPump[menuZone] = false;
  557. zonePump[menuZone] = true;
  558. lastManualDayTimer[menuZone] = millis();
  559. zoneManual[menuZone] = false;
  560. zoneAuto[menuZone] = true;
  561. }
  562. else if (zonePump[menuZone]) {
  563. lastManualDayTimer[menuZone] = millis();
  564. zonePump[menuZone] = false;
  565. zoneManualPump[menuZone] = true;
  566. zoneAuto[menuZone] = false;
  567. lastManualDayTimer[menuZone] = millis();
  568. zoneManual[menuZone] = true;
  569. }
  570. }
  571. lastButtonState2 = buttonState2;
  572. lastButtonState3 = buttonState3;
  573. }
  574. }
  575. }
  576.  
  577.  
  578. void settingSave() {
  579. EEPROM.update(zone1OnAddress, zoneActive[0]);
  580. EEPROM.update(zone2OnAddress, zoneActive[1]);
  581. EEPROM.update(zone3OnAddress, zoneActive[2]);
  582. EEPROM.update(zone4OnAddress, zoneActive[3]);
  583. EEPROM.update(zone1TrigLowAddress, triggerLow[0]);
  584. EEPROM.update(zone2TrigLowAddress, triggerLow[1]);
  585. EEPROM.update(zone3TrigLowAddress, triggerLow[2]);
  586. EEPROM.update(zone4TrigLowAddress, triggerLow[3]);
  587. EEPROM.update(zone1TrigHighAddress, triggerHigh[0]);
  588. EEPROM.update(zone2TrigHighAddress, triggerHigh[1]);
  589. EEPROM.update(zone3TrigHighAddress, triggerHigh[2]);
  590. EEPROM.update(zone4TrigHighAddress, triggerHigh[3]);
  591. }
  592.  
  593.  
  594. void zoneControl() {
  595. if (zoneAuto[zoneNumber]) {
  596. if (zoneActive[zoneNumber] && (!zonePump[zoneNumber] && !zoneManualPump[zoneNumber])) {
  597. sensor[zoneNumber] = analogRead(sensPin[zoneNumber]);
  598. digitalWrite(zoneLED[zoneNumber], HIGH); // Turn Zone Indicator LED On
  599. digitalWrite(output[zoneNumber], LOW);
  600. if ((unsigned long)(millis() - lastSensorReadTime[zoneNumber]) >= sensorReadDelay[zoneNumber]) {
  601. lastSensorReadTime[zoneNumber] = millis();
  602.  
  603. // Set the sensor reading levels for the output trigger for sensor
  604. if (sensor[zoneNumber] >= triggerLow[zoneNumber]) {
  605. zonePump[zoneNumber] = true;
  606. digitalWrite(output[zoneNumber], HIGH);
  607. lastPumpTimer[zoneNumber] = millis();
  608. zoneActive[zoneNumber] = false;
  609. }
  610. }
  611. }
  612. }
  613. }
  614.  
  615.  
  616. void zoneControlManual() {
  617. if (zoneManual[zoneNumber]) {
  618. if (zoneActive[zoneNumber] && (!zonePump[zoneNumber] && !zoneManualPump[zoneNumber])) {
  619. digitalWrite(zoneLED[zoneNumber], HIGH); // Turn Zone Indicator LED On
  620. digitalWrite(output[zoneNumber], LOW);
  621. sensor[zoneNumber] = 0;
  622. if ((unsigned long)(millis() - lastManualDayTimer[zoneNumber]) > manualDayTimer[zoneNumber]) {
  623. zoneManualPump[zoneNumber] = true;
  624. digitalWrite(output[zoneNumber], HIGH);
  625. lastPumpTimer[zoneNumber] = millis();
  626. lastManualDayTimer[zoneNumber] = millis();
  627. zoneActive[zoneNumber] = false;
  628. }
  629. }
  630. }
  631. }
  632.  
  633.  
  634. // Loop to progress zoneControl and fault monitor through each zone
  635.  
  636. void zoneLoop() {
  637. for (zoneNumber = 0; zoneNumber < 3; zoneNumber++) {
  638. zoneControl();
  639. zoneControlManual();
  640. zoneControlPump();
  641. }
  642. for (zoneNumber = 3; zoneNumber > 0; zoneNumber = 0) {
  643. zoneControl();
  644. zoneControlManual();
  645. zoneControlPump();
  646. }
  647. }
  648.  
  649.  
  650. void zoneControlPump() {
  651. if (zonePump[zoneNumber]) {
  652. if ((unsigned long)(millis() - lastPumpTimer[zoneNumber]) >= pumpTimer[zoneNumber]) {
  653. if (zoneAutoTimer[zoneNumber]) {
  654. sensor[zoneNumber] = analogRead(sensPin[zoneNumber]);
  655. if (sensor[zoneNumber] >= (triggerLow[zoneNumber] - 10)) {
  656. if (pumpTimer[zoneNumber] <= 360000) {
  657. pumpTimer[zoneNumber] = pumpTimer[zoneNumber] + 60000;
  658. }
  659. else if (pumpTimer[zoneNumber] >= 360000) {
  660. pumpTimer[zoneNumber] = 360000;
  661. }
  662. }
  663. else if (sensor[zoneNumber] <= (triggerLow[zoneNumber] + 10)) {
  664. if (pumpTimer[zoneNumber] > 60000) {
  665. pumpTimer[zoneNumber] = pumpTimer[zoneNumber] - 60000;
  666. }
  667. else if (pumpTimer[zoneNumber] <= 60000) {
  668. pumpTimer[zoneNumber] = 60000;
  669. }
  670. }
  671. }
  672. digitalWrite(output[zoneNumber], LOW);
  673. zoneActive[zoneNumber] = true;
  674. lastSensorReadTime[zoneNumber] = millis();
  675. zonePump[zoneNumber] = false;
  676. zoneManualPump[zoneNumber] = false;
  677. lastManualDayTimer[zoneNumber] = millis();
  678. }
  679. }
  680. else if (zoneManualPump[zoneNumber]) {
  681. if ((unsigned long)(millis() - lastPumpTimer[zoneNumber]) >= pumpTimer[zoneNumber]) {
  682. digitalWrite(output[zoneNumber], LOW);
  683. zoneActive[zoneNumber] = true;
  684. zonePump[zoneNumber] = false;
  685. zoneManualPump[zoneNumber] = false;
  686. lastManualDayTimer[zoneNumber] = millis();
  687. }
  688. }
  689. else if (!zoneManualPump[zoneNumber] && !zonePump[zoneNumber]) {
  690. digitalWrite(output[zoneNumber], LOW);
  691. }
  692. }
  693.  
  694. void loop() {
  695. readButtons();
  696. zoneLoop();
  697. displayMenu();
  698. // settingSave();
  699. }
  700.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement