#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_LINES 2
int tankSol = 12;
int extSol = 11;
int pumpSol = 10;
int returnSol = 9;
int vanSol = 8;
int pumpPower = 4;
int totalColumns = 16;
int totalRows = 2;
int rotaryUp = 7;
int rotaryDown = 6;
int rotarySelect = 5;
int menu = 1;
bool pumpState = false;
int Timer = 0;
int Timer_1 = 0;
int Timer2 = 0;
int Timer3 = 0;
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
String startupMessage = "Van Water Control System V0.1";
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(tankSol, OUTPUT);
pinMode(extSol, OUTPUT);
pinMode(pumpSol, OUTPUT);
pinMode(returnSol, OUTPUT);
pinMode(vanSol, OUTPUT);
pinMode(pumpPower, OUTPUT);
pinMode(rotaryUp, INPUT_PULLUP);
pinMode(rotaryDown, INPUT_PULLUP);
pinMode(rotarySelect, INPUT_PULLUP);
digitalWrite(pumpSol, HIGH); // Turn off the pump on startup
pumpState = false;
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
scrollMessage(0, startupMessage, 50, totalColumns);
delay(750);
updateMenu();
}
void scrollMessage(int row, String message, int delayTime, int totalColumns) {
for (int i = 0; i < totalColumns; i++) {
message = " " + message;
}
message = message + " ";
for (int position = 0; position < message.length(); position++) {
lcd.setCursor(0, row);
lcd.print(message.substring(position, position + totalColumns));
delay(delayTime);
}
}
void updateMenu() {
switch (menu) {
case 0:
menu = 1;
break;
case 1:
lcd.clear();
lcd.print(">Normal Op");
lcd.setCursor(0, 1);
lcd.print(" EXT Fill Tank");
break;
case 2:
lcd.clear();
lcd.print(" Normal Op");
lcd.setCursor(0, 1);
lcd.print(">EXT Fill Tank");
break;
case 3:
lcd.clear();
lcd.print(">Jetwash");
lcd.setCursor(0, 1);
lcd.print(" Hose To Tank");
break;
case 4:
lcd.clear();
lcd.print(" Jetwash");
lcd.setCursor(0, 1);
lcd.print(">Hose To Tank");
break;
case 5:
menu = 4;
break;
}
}
void executeAction() {
switch (menu) {
case 1:
action1();
break;
case 2:
action2();
break;
case 3:
action3();
break;
case 4:
action4();
break;
}
}
void action1() {
lcd.clear();
digitalWrite(tankSol, LOW);
digitalWrite(extSol, LOW);
digitalWrite(pumpSol, LOW);
digitalWrite(returnSol, LOW);
digitalWrite(vanSol, LOW);
digitalWrite(pumpPower, LOW);
lcd.print(">Okay To ");
lcd.setCursor(0, 1);
lcd.print("Power Off");
delay(20000);
}
void action2() {
if (pumpState == true) {
lcd.clear();
lcd.print("Pump On");
}
else if (pumpState == false) {
lcd.clear();
lcd.print("Pump Off");
}
delay(5000);
}
void action3() {
lcd.clear();
lcd.print(">Toggle External Solenoid");
digitalWrite(extSol, HIGH);
Timer2++;
if (Timer2 == 2) {
digitalWrite(extSol, LOW);
Timer2 = 0;
}
else {
digitalWrite(extSol, HIGH);
}
delay(1500);
}
void action4() {
lcd.clear();
lcd.print(">Toggle Return Solenoid");
digitalWrite(returnSol, HIGH);
Timer3++;
if (Timer3 == 2) {
digitalWrite(returnSol, LOW);
Timer3 = 0;
}
else {
digitalWrite(returnSol, HIGH);
}
delay(1500);
}
void loop() {
if (!digitalRead(rotaryDown)) {
menu++;
updateMenu();
delay(100);
while (!digitalRead(rotaryDown));
}
if (!digitalRead(rotaryUp)) {
menu--;
updateMenu();
delay(100);
while (!digitalRead(rotaryUp));
}
if (!digitalRead(rotarySelect)) {
executeAction();
updateMenu();
delay(100);
while (!digitalRead(rotarySelect));
}
}