#define outputA 6 #define outputB 7 #define buttonPin 8 #define lightPin 13 #define waterLPin 10 #include int waterPin = A0; int pinValue, pinValueVolt; float waterPer; int counter = 0; int aState; int aLastState; int bState; int bLastState; int buttonState; int lastButtonState; int hourTen = 0; int hourOne = 0; int minTen = 0; int minOne = 0; int hourTenL = 1; int hourOneL = 0; int minTenL = 0; int minOneL = 0; int minWaterTen = 8; int minWaterOne = 2; int minWater = (minWaterTen * 10) + minWaterOne; int timeScale = 1; int timePassed = 0; long unsigned int hourSec = ((hourTenL * 10) + hourOneL) * 60; long unsigned int lightOnSec = (hourSec + ((minTenL * 10) + minOneL)) * 60; long unsigned int scaledLightOnSec; long unsigned int scaledLightOffSec; unsigned long secDay; unsigned long secLeft; long unsigned int preTime; long unsigned int currTime; bool flipOn = true; char menuList[][10] = {"View", "Water", "Light", "Flip"}; const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; LiquidCrystal lcd(rs, en, d4, d5, d6, d7); int subTimeMenu(int pos, int val, int condVal = 0); int adjustHourTen(int val); int adjustHourOne(int val, int condVal); int adjustMinTen(int val); int adjustMinOne(int val); void setup() { // put your setup code here, to run once: pinMode(outputA, INPUT_PULLUP); pinMode(outputB, INPUT_PULLUP); pinMode(buttonPin, INPUT_PULLUP); pinMode(lightPin, OUTPUT); pinMode(waterLPin, OUTPUT); hourSec = ((hourTenL * 10) + hourOneL) * 60; lightOnSec = (hourSec + ((minTenL * 10) + minOneL)) * 60; Serial.begin(9600); aLastState = digitalRead(outputA); lcd.begin(16, 2); lcd.clear(); lcd.setCursor(0, 0); lcd.print(menuList[0]); } void loop() { // put your main code here, to run repeatedly: updateInputVals(); delay(50); secDay = 86400 / timeScale; scaledLightOnSec = lightOnSec / timeScale; scaledLightOffSec = secDay - scaledLightOnSec; pinValue = analogRead(waterPin); pinValueVolt = (5.0 / 1023) * pinValue; waterPer = 100 - (((pinValue - 200) / (float)(700)) * 100); Serial.print("Water Per: "); Serial.print(waterPer); currTime = millis(); if (currTime - preTime >= 1000) { preTime = currTime; timePassed++; } if (waterPer < minWater) { digitalWrite(waterLPin, HIGH); } else { digitalWrite(waterLPin, LOW); } if (!secLeft) { flipOn =!flipOn; toggleLight(); timePassed = 0; } if (flipOn) { Serial.print(" Light off "); secLeft = secDay - scaledLightOffSec - timePassed; } else { Serial.print(" Light on "); secLeft = secDay - scaledLightOnSec - timePassed; } if (!counter) { lcd.setCursor(0, 0); lcd.print("Moisture: "); lcd.print(waterPer); lcd.setCursor(0, 1); lcd.print("Time: "); float hours = secLeft / (float)3600; Serial.print("Hours left: "); Serial.println(hours); if (hours <= 9) { lcd.print("0"); } lcd.print(hours); if (flipOn) { lcd.print(" on "); } else { lcd.print(" off"); } } if (!aState && aState != aLastState) { counter++; if (counter > 3) { counter = 0; } lcd.clear(); lcd.setCursor(0, 0); lcd.print(menuList[counter]); } if (!bState && bState != bLastState) { counter--; if (counter < 0) { counter = 3; } lcd.clear(); lcd.setCursor(0, 0); lcd.print(menuList[counter]); } if (!buttonState && buttonState != lastButtonState) { switch (counter) { case 0: toggleLight(); break; case 1: buttonState = 2; lastButtonState = 0; waterMenu(); lcd.print(menuList[counter]); break; case 2: buttonState = 2; lastButtonState = 0; timeMenu(); lcd.print(menuList[counter]); break; case 3: toggleLight(); flipOn = !flipOn; timePassed = 0; counter = 0; break; } } updatePreInputVals(); } void waterMenu() { lcd.clear(); lcd.setCursor(0, 0); lcd.blink(); lcd.print("Min Per: "); lcd.print(minWaterTen); lcd.print(minWaterOne); lcd.print(" x"); int posI[] = {9, 10, 12}; int pos = 0; int loop = 1; while (loop) { updateInputVals(); bool a = checkInput(aState, aLastState); bool b = checkInput(bState, bLastState); bool button = checkInput(buttonState, lastButtonState); lcd.setCursor(9, 0); lcd.print(minWaterTen); lcd.print(minWaterOne); lcd.print(" x"); lcd.setCursor(posI[pos], 0); delay(50); if (a) { pos++; if (pos > 2) { pos = 0; } } if (b) { pos--; if (pos < 0) { pos = 2; } } if (button) { switch (pos) { case 0: buttonState = 2; lastButtonState = 0; minWaterTen = adjustMinOne(minWaterTen, posI[pos]); pos++; break; case 1: buttonState = 2; lastButtonState = 0; minWaterOne = adjustMinOne(minWaterOne, posI[pos]); pos++; break; case 2: loop = 0; minWater = (minWaterTen * 10) + minWaterOne; delay(150); lcd.noBlink(); lcd.clear(); break; } } updatePreInputVals(); } } void toggleLight() { digitalWrite(lightPin, LOW); delay(50); digitalWrite(lightPin, HIGH); delay(50); digitalWrite(lightPin, LOW); } void timeMenu() { lcd.setCursor(0, 0); lcd.print("Time On: "); lcd.blink(); int loop = 1; int posI[] = {9, 10, 12, 13, 15}; int pos = 0; while (loop) { updateInputVals(); bool a = checkInput(aState, aLastState); bool b = checkInput(bState, bLastState); bool button = checkInput(buttonState, lastButtonState); lcd.setCursor(9, 0); lcd.print(hourTenL); lcd.print(hourOneL); lcd.print(":"); lcd.print(minTenL); lcd.print(minOneL); lcd.print(" x"); lcd.setCursor(posI[pos], 0); delay(50); if (a) { pos++; if (pos > 4) { pos = 0; } } if (b) { pos--; if (pos < 0) { pos = 4; } } if (button) { switch (pos) { case 0: buttonState = 2; lastButtonState = 0; hourTenL = adjustHourTen(hourTenL, posI[pos]); pos++; break; case 1: buttonState = 2; lastButtonState = 0; hourOneL = adjustHourOne(hourOneL, hourTenL, posI[pos]); pos++; break; case 2: buttonState = 2; lastButtonState = 0; minTenL = adjustMinTen(minTenL, posI[pos]); pos++; break; case 3: buttonState = 2; lastButtonState = 0; minOneL = adjustMinOne(minOneL, posI[pos]); pos++; break; case 4: loop = 0; hourSec = ((hourTenL * 10) + hourOneL) * 60; lightOnSec = (hourSec + ((minTenL * 10) + minOneL)) * 60; timePassed =0; if (!lightOnSec) { lightOnSec = 1; } delay(50); lcd.noBlink(); lcd.clear(); break; } } updatePreInputVals(); } }\ int adjustHourTen(int val, int col) { while(1) { updateInputVals(); delay(20); bool a = checkInput(aState, aLastState); bool b = checkInput(bState, bLastState); bool button = checkInput(buttonState, lastButtonState); if (a) { val++; if (val > 2) { val = 0; } lcd.setCursor(col, 0); lcd.print(val); lcd.setCursor(col, 0); } if (b) { val--; if (val < 0) { val = 2; } lcd.setCursor(col, 0); lcd.print(val); lcd.setCursor(col, 0); } if (button) { return val; } updatePreInputVals(); } } int adjustHourOne(int val, int hr, int col) { while(1) { updateInputVals(); delay(20); bool a = checkInput(aState, aLastState); bool b = checkInput(bState, bLastState); bool button = checkInput(buttonState, lastButtonState); int max = 9; if (hr == 2) { max = 3; } if (a) { val++; if (val > max) { val = 0; } lcd.setCursor(col, 0); lcd.print(val); lcd.setCursor(col, 0); } if (b) { val--; if (val < 0) { val = max; } lcd.setCursor(col, 0); lcd.print(val); lcd.setCursor(col, 0); } if (button) { return val; } updatePreInputVals(); } } int adjustMinTen(int val, int col) { while(1) { updateInputVals(); delay(20); bool a = checkInput(aState, aLastState); bool b = checkInput(bState, bLastState); bool button = checkInput(buttonState, lastButtonState); if (a) { val++; if (val > 5) { val = 0; } lcd.setCursor(col, 0); lcd.print(val); lcd.setCursor(col, 0); } if (b) { val--; if (val < 0) { val = 5; } lcd.setCursor(col, 0); lcd.print(val); lcd.setCursor(col, 0); } if (button) { return val; } updatePreInputVals(); } } int adjustMinOne(int val, int col) { while(1) { updateInputVals(); delay(20); bool a = checkInput(aState, aLastState); bool b = checkInput(bState, bLastState); bool button = checkInput(buttonState, lastButtonState); if (a) { val++; if (val > 9) { val = 0; } lcd.setCursor(col, 0); lcd.print(val); lcd.setCursor(col, 0); } if (b) { val--; if (val < 0) { val = 9; } lcd.setCursor(col, 0); lcd.print(val); lcd.setCursor(col, 0); } if (button) { return val; } updatePreInputVals(); } } void updateInputVals() { buttonState = digitalRead(buttonPin); aState = digitalRead(outputA); bState = digitalRead(outputB); } void updatePreInputVals() { lastButtonState = buttonState; aLastState = aState; bLastState = bState; } bool checkInput(int currVal, int preVal) { if (!currVal && currVal != preVal) { return true; } else { return false; } }