Help with code beginner

i am trying to make a auto start for my generator the code i have written is my first deployed code so dont make too much fun of me.

int powercall = A2;
int runconfirm = A1;
int val = 0;
int val1 = 0;
int genstart = 9;
int genpower = 10;
int red = 2;
int green = 3;

void setup () 
{

  pinMode(genstart, OUTPUT);
  pinMode(genpower, OUTPUT);
  pinMode(red,OUTPUT);
  pinMode(green, OUTPUT);
  digitalWrite(genstart, HIGH);
  digitalWrite(genpower, HIGH);
  digitalWrite(green,HIGH);
 Serial.begin (9600);
//Setup serial
}

void loop () 
{
     val = analogRead (powercall)
    ;Serial.println(val)
    ;val1 = analogRead (runconfirm)
    ;Serial.println(val1)

    ;if ( (val >= 512) && (val1 < 512) ) {
   digitalWrite(genpower, LOW);
   delay(1000);
  ;digitalWrite(genstart, LOW)
  ;delay(500)
  ;digitalWrite(genstart, HIGH)
  ;delay(500)
  ;digitalWrite(genstart, LOW)
  ;delay(500)
  ;digitalWrite(genstart, HIGH);
  ;delay(10000);

  ;val1 = analogRead (runconfirm)
    ;Serial.println(val1)
  
  ;if (val1 < 512) {
  ;digitalWrite(genstart, LOW)
  ;delay(500)
  ;digitalWrite(genstart, HIGH)
  ;delay(500)
  ;digitalWrite(genstart, LOW)
  ;delay(500)
  ;digitalWrite(genstart, HIGH);
  ;delay(10000);
 }

 ;val1 = analogRead (runconfirm)
    ;Serial.println(val1)
 
   ;if (val1 < 512) {
  ;digitalWrite(genstart, LOW)
  ;delay(500)
  ;digitalWrite(genstart, HIGH)
  ;delay(500)
  ;digitalWrite(genstart, LOW)
  ;delay(500)
  ;digitalWrite(genstart, HIGH);
  ;delay(10000);
  }

  ;val1 = analogRead (runconfirm)
    ;Serial.println(val1)
  
   ;if (val1 < 512) {
  ;digitalWrite(genstart, LOW)
  ;delay(500)
  ;digitalWrite(genstart, HIGH)
  ;delay(500)
  ;digitalWrite(genstart, LOW)
  ;delay(500)
  ;digitalWrite(genstart, HIGH);
  ;delay(10000);
    }

    ;val1 = analogRead (runconfirm)
    ;Serial.println(val1)
    
   ;if (val1 < 512) {
  ;digitalWrite(genpower, HIGH)
  ;digitalWrite(red,HIGH)
  ;digitalWrite(green, LOW)
 ;exit(0)
; } }

;if ( (val <= 512) && (val1 > 512) ) {
   digitalWrite(genpower, HIGH);
}
delay(10000); 
digitalWrite(green, LOW);
delay(1000);
digitalWrite(green,HIGH);
delay(10000);
digitalWrite(green, LOW);
delay(1000);
digitalWrite(green,HIGH);
delay(10000);
digitalWrite(green, LOW);
delay(1000);
digitalWrite(green,HIGH);
}
  

it is running on a nano every and works but has a small issue
a2 powercall is the signal from my inverter that sends 5v when power is needed
a1 runconfirm is 4v adapter plugged into generator
d9 genstart needs 2 presses of the button to start generator
d10 genpower powers up the generator

relay controlled by a2 and a1 is this board

everything works fine but randomly between a minuite or 3 of running the generator dies. if i disconnect d9 genstart wire after it is running the generator has no issues. where should i start troubleshooting?

Generally, it looks like you’re going the right way,

Your semicolons are a bit disconcerting!

Before spending too much time, look at getting rid of the delays, and implementing a state-machine.

I’ve done several auto start gen controllers, and there are a few things that might catch you as it evolves.

A state machine is imperative, along with fail-safe and pre-start warning buzzer or strobes.

Good luck, they’re fun to ‘get right’.

Can you post a schematic that shows all connections, power, ground, power sources and note any wires over 10"/25cm.

Hello mltj74

Welcome to the world's best Arduino forum ever.

To get save you should decouple the genset from the Arduino by using optocouplers.

  • it's not obvious what the above signals mean. from the code it looks like a value > 512 on the powercall pin indicates the need to start the generator and a value < 512 on the runconfirm pin means that the generator did not start

  • it looks like during each iteration of loop() there are 4 attempts to start the generator by toggle the genstart pin twice, setting the genpower pin, presumably ON then OFF during each attempt. but since loop() is executed repeatedly, this is done an infinite # of times

  • each attempt to restart the engine has an 11.5 sec delay, 45 sec total and toggling the green LED on/off another 15 sec

  • shouldn't there be an independent test of the powercall pin to possibly disable the generator and in a separate case check if it is running and if it is not running attempt to restart the generator without turning it off between attempts

  • would make sense that the LEDs indicate there state of the powercall pin and have the other (green) LED indicate the state of the genstart pin which would toggle if attempting to start or remain on if started

  • should code exit(0) on a failed attempt to start