generator auto start

hi all, ive written this code and just wanted some advice if there would be easier ways, problems etc.

Ill post code and a brief description and any Q's ill try and answer, thanks in advance for your help..

its a backup generator for a off grid system, 48volt...

auto_gen_start.ino (3.07 KB)

Hi,

Interesting project.

This line here appears it may not be doing what you expect:
analogRead(genHV);
** if (genHV, HIGH) {**
In the analogRead() genHV refers correctly to the A0 pin being tested, but in the next line the pin number (still A0) is used again rather than the value of the reading on that pin. Were you looking perhaps to test if the genHV pin was above a certain value? That would look something like this:
if(analogRead(genHV)>= somespecifiedvalue) {

Since each of the 3 attempts to start the generator are the same, rather than having 3 functions defined which are essentially the same, you could simplify this into one function, Estart() and call that as many times as you need, up to 3 as you have here for example. This can be done with a loop structure. Something like this perhaps:

// in loop()

  if (voltage <= batL); {
    for (int tryCount = 0; tryCount < 3 && analogRead(genHV) < somespecifiedvalue; tryCount++) {
      Estart();
   }

    if(analogRead(genHV) < somespecifiedvalue) {  // generator didn't start after 3 tries
       // do the error stuff presently in try3
    } else {
       digitalWrite(genok, true);
    }

Remember to add the test and function call for shutdowngen() to loop() now as Try2 and try3 functions would no longer be present.

Estart would then be simplified to

void Estart () {
  digitalWrite (solPin, HIGH);
  analogWrite(genstart, 50);
  delay (1000);
  digitalWrite(genstart, HIGH);
  delay (500);
  digitalWrite (solPin, LOW);
  digitalWrite (genstart, LOW);
  delay (500);
}

The loop part of the code repeats infinitely, so anything that needs to be monitored can be placed in there, and the functions called for the generator to be started and stopped as required.

Structuring it so all the repeated tests are in loop() also means you won't need to call another function at the end of each of your functions...when they conclude control will return to loop() which will make the code easier to follow and debug.

Hope this is useful to you. All the best with your project,
Geoff

Hi,
Just looking through your sketch, you aren't monitoring oil pressure, the generator motor should have a pressure switch.
This will tell you if the engine is running or a mechanical failure or lack of oil has caused oil pressure drop, which would damage the engine.

Tom.... :slight_smile:

thanks so much for your replys...

Geoff thx, that's exactly what I ment about shorterning code, ill do some more debugging with your suggestions...

tom, again thx for your reply, the oil pressure is externally monitored (gen itself), the arduino only needs to handle start, stop, etc. it checks the HV line and tells whether everything is kosher from that...

simple bridge rectifier and a very high value resistor divider through an opto is all im going to need...if that makes sense.

hows this (I implemented your suggestions Geoff somewhat) I want the programme to pretty much stop while charging until the batteries have charged... hence "while" command... what yous think??

again thx in advance..

auto_gen_start.ino (2.53 KB)

noneyabussiness:
I want the programme to pretty much stop while charging until the batteries have charged... hence "while" command... what yous think??

again thx in advance..

That logic where it turns the generator off is not present in the version of the code you've included here.

ive added it at the end of "Estart"

void Estart () {
digitalWrite (solPin, HIGH);
analogWrite(genstart, 50);
delay (1000);
digitalWrite(genstart, HIGH);
delay (500);
digitalWrite (solPin, LOW);
digitalWrite (genstart, LOW);
delay (30000);
digitalWrite (chargeLED, HIGH);
digitalWrite (chargePIN, HIGH);
delay (100);
while (analogRead (batV) >= batH) {
return shutdowngen () ;
}
}

is that correct or do you think there is a easier way, I pretty much want while the generator is charging the batteries the arduino to hold until bat voltage reaches the upper threshold... hence why I put it in "while" it is in Estart loop... make sense??

and again thx so much for your guidance...

I'd put everything that needs be done repeatedly, like that measurement, in loop(). Keep functions like Estart pure to their purpose so you will find it easier to isolate issues when debugging.

Cheers ! Geoff

hows this?? better / worse?? thx again in advance...

auto_gen_start.ino (2.69 KB)

If it's doing what you need it to, this latest version is far simpler than the first you presented.

All the best with your project,
Geoff