#include <Wire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// define some values used by the panel and buttons
int lcd_key = 0;
int adc_key_in = 0;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
// read the buttons
int read_LCD_buttons()
{
adc_key_in = analogRead(0); // read the value from the sensor
// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
// For V1.1 us this threshold
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 250) return btnUP;
if (adc_key_in < 450) return btnDOWN;
if (adc_key_in < 650) return btnLEFT;
if (adc_key_in < 850) return btnSELECT;
return btnNONE; // when all others fail, return this...
}
//Charger state on/off
int chargerstate = 0;
float chargertarget = 13.6;
#define ChargerRelay 0
int ChargerRelayState =0;
// number of analog samples to take per reading
#define NUM_SAMPLES 20
int sum = 0; // sum of samples taken
unsigned char sample_count = 0; // current sample number
float voltage = 0.0; // calculated voltage
void setup()
{
lcd.begin(16, 2); // start the library
lcd.setCursor(0,0);
lcd.print("Voltage:"); // print a simple message
pinMode(ChargerRelay, OUTPUT);
}
void loop()
{
// take a number of analog samples and add them up
while (sample_count < NUM_SAMPLES) {
sum += analogRead(A5);
sample_count++;
delay(10);
}
// calculate the voltage
// use 5.0 for a 5.0V ADC reference voltage
// 5.015V is the calibrated reference voltage
voltage = ((float)sum / (float)NUM_SAMPLES * 5.015) / 1024.0;
// send voltage for display on Serial Monitor
// voltage multiplied by 5 when using voltage divider that
// divides by 5. 4.96 is the calibrated voltage divide
// value
lcd.setCursor(8,0);
lcd.print(voltage * 4.85);
lcd.print ("V");
lcd.setCursor(12,1);
lcd.print(chargertarget);
lcd.print("V");
sample_count = 0;
sum = 0;
if ((voltage * 4.85) <= 12.20) {
chargerstate = 1;
digitalWrite(ChargerRelay, HIGH);
} else if ((voltage * 4.85) >= chargertarget) {
chargerstate = 0;
digitalWrite(ChargerRelay, LOW);
}
lcd.setCursor(0,1);
lcd.print("Charger:");
if (chargerstate == 0) {
lcd.setCursor(8,1);
lcd.print("OFF");
} else {
lcd.setCursor(8,1);
lcd.print("ON ");
}
lcd_key = read_LCD_buttons(); // read the buttons
switch (lcd_key) // depending on which button was pushed, we perform an action
{
case btnRIGHT:
{
chargertarget ++;
break;
}
case btnLEFT:
{
chargertarget --;
break;
}
case btnUP:
{
chargerstate = 1;
digitalWrite(ChargerRelay, HIGH);
break;
}
case btnDOWN:
{
chargerstate = 0;
digitalWrite(ChargerRelay, LOW);
break;
}
case btnSELECT:
{
//lcd.print("SELECT");
break;
}
case btnNONE:
{
//lcd.print("NONE ");
break;
}
}
}
Comments
Please log in or sign up to comment.