I have a solar panel charging a battery and several loads attached to it. I would like to disconnect the load after the voltage drops to and remains below a low value for a period of 3 minutes. If the voltage rises above the low value during the 3 minutes , the timer would be reset.
The load would not connect again until the battery remained above a high value for 4 minutes.
This is the code for 1 relay . I am planning to have a total of 5 relays all with different voltage limits. I'm not sure if this is the correct way to accomplish what I want .
/*-----( Declare Constants )-----*/
//set pins
const int voltageIn = A0;
const int relay1 = 2;
//set voltage range of relay
const int Off_VoltageRelay1 = 24.3;
const int On_VoltageRelay1 = 28.0;
/*-----( Declare objects )-----*/
// set the LCD address to 0x27 for a 16 chars 2 line display or 0x3F
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
/*-----( Declare Variables )-----*/
int val1 = 0;
void setup()
{
pinMode(relay1, OUTPUT);
lcd.begin(16, 2); // initialize the lcd for 16 chars 2 lines, turn on backlight
// ------- Quick 3 blinks of backlight -------------
for (int i = 0; i < 3; i++)
{
lcd.backlight();
delay(250);
lcd.noBacklight();
delay(250);
}
lcd.backlight(); // finish with backlight on
//-------- Write characters on the display ------------------
// NOTE: Cursor Position: (CHAR, LINE) start at 0
lcd.setCursor(0, 0);
lcd.print("Battery Voltage");
delay(1000);
lcd.setCursor(0, 1);
lcd.print("Load Controller");
delay(8000);
lcd.clear();
}/*--(end setup )---*/
void loop()
{
{
// read the input on analog pin 0:
int inputVoltage = analogRead(voltageIn);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 40V):
float voltage = inputVoltage * (40 / 1023.0);
//if voltage is below Lo limit turn relay OFF
//if voltage is above Hi limit turn relay ON
if (voltage < Off_VoltageRelay1)
delay (60000);
if (voltage < Off_VoltageRelay1)
delay (60000);
if (voltage < Off_VoltageRelay1)
delay (60000);
if (voltage < Off_VoltageRelay1)
digitalWrite (relay1, LOW);
if (voltage > On_VoltageRelay1)
delay (60000);
if (voltage > On_VoltageRelay1)
delay (60000);
if (voltage > On_VoltageRelay1)
delay (60000);
if (voltage > On_VoltageRelay1)
delay (60000);
if (voltage > On_VoltageRelay1)
digitalWrite (relay1, HIGH);