#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
int upButton = 3;
int downButton = 4;
int selectButton = 5;
int voltagePin = A0;
float batteryVoltage = 0;
float lowVoltage = 0;
float highVoltage = 0;
int state = 0;
int LED = A1;
void setup() {
pinMode(upButton, INPUT);
pinMode(downButton, INPUT);
pinMode(selectButton, INPUT);
pinMode(LED, OUTPUT);
lcd.begin(16, 2);
lowVoltage = 0;
highVoltage = 0;
}
void loop() {
batteryVoltage = analogRead(voltagePin) * (24.0 / 1023.0); // voltage divider
if (digitalRead(selectButton) == HIGH) {
state = (state + 1) % 3;
delay(200);
}
if (state == 0) {
lcd.clear();
lcd.print("Battery Voltage:");
lcd.setCursor(0, 1);
lcd.print(batteryVoltage);
delay(500);
} else if (state == 1) {
if ((digitalRead(upButton) == HIGH)&&( lowVoltage < 24.50)) {
lowVoltage += 0.5;
delay(200);
}
if ((digitalRead(downButton) == HIGH)&&( lowVoltage > 0.00)) {
lowVoltage -= 0.5;
delay(200);
}
lcd.clear();
lcd.print("Low Voltage:");
lcd.setCursor(0, 1);
lcd.print(lowVoltage);
} else if (state == 2) {
if ((digitalRead(upButton) == HIGH)&&( highVoltage < 24.50)) {
highVoltage += 0.5;
delay(200);
}
if ((digitalRead(downButton) == HIGH)&&( highVoltage > 0.00)) {
highVoltage -= 0.5;
delay(200);
}
lcd.clear();
lcd.print("High Voltage:");
lcd.setCursor(0, 1);
lcd.print(highVoltage);
}
if(batteryVoltage < lowVoltage)
{
digitalWrite(LED, HIGH);
}
else if(batteryVoltage > highVoltage)
{
digitalWrite(LED, LOW);
}
}