Looking for advice/assistance as to if my coding for this project is correct. Project is an auto start/stop of a 12v alternator gas engine powered generator only if or when a battery bank is depleted below what solar panels can supply. See attached photos of layout and coding I have done.
int Choke = 2;
int Fuel = 3;
int Alt = 4;
int Start = 5;
int Throttle = 6;
const int V = A0;
const int A = A1;
float vout = 0.0;
float vin = 0.0;
int v = 0;
int a = 0;
int mVperAmp = 20;//20mV per Amp for 100A ACS758
int ACSoffset = 2500; // 2500 is offset for bi-directional ACS758
int RawValue= 0;
double Voltage = 0;
double Amps = 0;
// Use values shown below depending on system voltage
float R1 = 4700.0; // measured resistance of R1 on YOUR board for 12v-allows 20.6v max
float R2 = 1500.0; // measured resistance of R2 on YOUR board for 12v
//float R1 = 15000.0; // measured resistance of R3 on YOUR board for 24v- allows 39v max
//float R2 = 2200.0; // measured resistance of R4 on YOUR board for 24v
void setup() {
Serial.begin(9600);
//Intiates Serial communications
pinMode(Choke, OUTPUT);
pinMode(Fuel, OUTPUT);
pinMode(Alt, OUTPUT);
pinMode(Start, OUTPUT);
pinMode(Throttle, OUTPUT);
digitalWrite(Choke, LOW);//Open choke
digitalWrite(Fuel, LOW);//Close fuel valve
digitalWrite(Start, LOW);//starter off
digitalWrite(Alt, LOW);//alternator off
digitalWrite(Throttle, LOW);//throttle at low RPM
}
void loop() {
// read the value at analog input
v = analogRead(V);//read voltage from voltage divider
a = analogRead(A);//read amperage from ACS758
Voltage = (a / 1023.0) * 5000; // Gets you mV
Amps = ((Voltage - ACSoffset) / mVperAmp);
vout = (v / 1023.0) * 5000; // Gets you mV note:5000 is +5v of your system- change to your actual board voltage in millivolts
delay(1000);
vin = (vout / (R2/(R1+R2)));
if (vin < 0.09) {
vin=0.0;//statement to quash undesired reading !
}
// Modify code to match your system voltage, 12.2v for on and 14.4v off for 12v system and 24.4v on and 28.8 off for 24v system
if (vin <= 12.2){
digitalWrite(Fuel, HIGH);//open fuel valve
delay(5000);//pause to allow gas to fill carb bowl
digitalWrite(Throttle, HIGH);//set RPM to operating speed
delay(2000);
digitalWrite(Choke, HIGH);// apply choke
delay(3000);
digitalWrite(Start, HIGH);// attempt to start engine
delay(5000);// crank for only 5 secconds
digitalWrite(Start, LOW);//stop trying to start engine
delay(5000);//allow 5 second warmup with choke on
digitalWrite(Choke, LOW);// open choke for normal operation
digitalWrite(Alt, HIGH);// activate field of alternator
}
// else statement prevents stopping if voltage is in ok status but Amps from alternator is still charging at over a 2A rate.
else {
Amps >= 2.0;
}
if (vin >=14.4){
digitalWrite(Alt,LOW);//turn alternator field off
digitalWrite(Fuel,LOW);//turn fuel valve off
digitalWrite(Throttle,LOW);//reduce RPM for cooldown till carb is empty
delay(300000); // 5 minute wait before rechecking status
}
}