Need help with setting up a code to run a generator

The issue is in the first if statement, it is not bypassing even with the key in the off position (not connected to ground). Any help would be much appreciated, I am new to coding so be gentle with me! I have tried using input_pullup and digital write high for KeyOn and KeyStart

#define ON 7               // relay 1 is Gen on/off switch
#define CHK 6              // relay 2 is choke switch
#define START 5            // relay 3 is Gen starter
#define relay4 4           // relay 4 unused

#define KeyON 8            // Pin 8 is high when key is on
#define KeyStart 9         // Pin 9 is High to Run start sequence
#define KeyTurn 10         //Turns LED on when KeyStart is read by program

#define BattVolt A0        // battery voltage at terminals
#define GenVolt A1         // Gen stator voltage

//Floats for ADC and Input voltage
float adcgen_voltage = 0.0;
float adcbatt_voltage = 0.0;
float gen_voltage = 0.0;
float batt_voltage = 0.0;

//Floats for resistor values (in ohms) for generator stator voltage and battery voltage
float R1gen = 30000.0; 
float R1batt = 30000.0;
float R2gen = 7500.0;
float R2batt = 7500.0;

//Float for reference voltage
float refgen_voltage = 5.0;
float refbatt_voltage = 5.0;

int adcgen_value = 0;
int adcbatt_value = 0;
int START_Attempt = 0;
int START_Time = 0;
int Counter = 0;

unsigned long start_time = 0;
const unsigned long batt_check = 43200000;  //Check battery voltage every 12 hours


void setup() {

  Serial.begin(9600);                 // setup serial monitor to display information
  pinMode(ON, OUTPUT);                // connected to relay 1 Gen on/off
  pinMode(CHK, OUTPUT);               // connected to relay 2 Choke on/off
  pinMode(START, OUTPUT);             // connected to relay 3 Gen starter
  pinMode(relay4, OUTPUT);            // connected to relay 4
  pinMode(GenVolt,INPUT);             // Input for battery voltage
  pinMode(BattVolt,INPUT);            // Input for Generator stator voltage
  pinMode(KeyON,INPUT);               //Turn Generator on with key
  pinMode(KeyStart,INPUT);            //Start generator with key
  pinMode(KeyTurn,OUTPUT);            // turn LED on when program sees key in start position

// Make sure all inputs and outputs are set to correct values
  digitalWrite(ON,LOW);
  digitalWrite(CHK,LOW);
  digitalWrite(START,LOW);
  digitalWrite(KeyON,HIGH);
  digitalWrite(KeyStart,HIGH);
  digitalWrite(KeyTurn,LOW);


}

void loop() {
//Generator voltage readings
  adcgen_value = analogRead(GenVolt);

  adcgen_voltage = (adcgen_value * refgen_voltage )/ 1024.0;

  gen_voltage = adcgen_voltage / (R2gen / (R1gen + R2gen));
  
  //Battery voltage readings
    adcbatt_value = analogRead(BattVolt);

  adcbatt_voltage = (adcbatt_value * refbatt_voltage )/ 1024.0;

  batt_voltage = adcbatt_voltage / (R2batt / (R1batt + R2batt));

    if((digitalRead(KeyON == LOW)) && (digitalRead(KeyStart == LOW)) && ((gen_voltage) < 12))
  {
       for(START_Attempt = 0; START_Attempt <= 3; START_Attempt++) //Try to start a max of 4 times
    {
      digitalWrite(KeyTurn,HIGH);         //Activate LED when start program sees key Ingnition turned
      digitalWrite(ON,HIGH);             //Turn generator switch on
      delay(5000);                       //To allow the user to see that the code picked up the momentary switch
      digitalWrite(START,HIGH);          //Activate generator starter
      digitalWrite(CHK,HIGH);            //Turn choke on

      delay(2500);                       //Crank starter for 2.5 sec
      digitalWrite(START,LOW);           //Turn starter off
      delay(1500);                       //Leave Choke of an additional 1.5 sec
      digitalWrite(CHK,LOW);             //Turn choke off
      delay(3000);                       //Delay 3 second before trying the restart loop again

         if((gen_voltage) > 12)          //If there is voltage detected at stator then stop loop
      {
        break;
      }
        delay(50); 
    }
  }
        
        if((digitalRead(KeyON == HIGH)) && ((gen_voltage) > 12) && (digitalRead(KeyTurn == HIGH)))
        {
          delay(300000);                    //Cool down generator for 5 minutes
          digitalWrite(ON,LOW);             //Turn off generator with key
          digitalWrite(KeyTurn,LOW);        //Turn off LED KeyStart indicator light
          
          START_Attempt = 0;                //Reset start attempts
          Counter = 0;                      //Reset counter
          start_time = millis();            //Reset timer
        }


  unsigned long current_time = millis();
  if((current_time - start_time > batt_check) || (Counter > 28) && (gen_voltage < 12))
  {
  if(((batt_voltage) < 12) || (Counter > 28) && ((gen_voltage) < 12))    //If battery voltage drops below 12v or doesnt start in approximatly 2 weeks
  {
    if(((batt_voltage) < 12) || (Counter > 28) && ((gen_voltage) < 12))  //If less than 4 attempts to start or generator doesnt detect 12v from stator 
    {
    for(START_Attempt = 0; START_Attempt <= 3; START_Attempt++)          //Try to start a max of 4 times
    { 
      digitalWrite(ON,HIGH);             //Turn generator switch on
      delay(2000);
      digitalWrite(START,HIGH);          //Activate generator starter
      digitalWrite(CHK,HIGH);            //Turn choke on

      delay(2500);                       //Crank starter for 2.5 sec
      digitalWrite(START,LOW);           //Turn starter off
      delay(1500);                       //Leave Choke of an additional 1.5 sec
      digitalWrite(CHK,LOW);             //Turn choke off
      delay(5000);                       //Delay 7 second before trying to restart

         if(((gen_voltage) > 12))        //If there is voltage detected at stator then stop loop
      {
        break;
      }
        delay(500);
    }

  }
  else
  {
    if((gen_voltage) > 12)              //If generator has started reset start attempts and counter
    {
      int START_Attempt=0;                  //Reset start attempts
      Counter=0;                        //Reset counter
      start_time = current_time;        //Reset timer
    
    digitalWrite(START,LOW);
    digitalWrite(CHK,LOW);
    delay (1200000);                    //Run generator for 20 min to recharge battery or exercise
    digitalWrite(ON,LOW);               //Turn Generator off
    }
  } 
  } 
Counter++;                              //Exercise Approximatly every 2 weeks
start_time = millis();                  //Reset counter


  }
}

Why would you write to an input? Please post a complete wiring diagram.

Far too many unnecessary brackets. Try writing like this, it's more readable:

 if(digitalRead(KeyON) == LOW and digitalRead(KeyStart) == LOW and gen_voltage < 12)

See how the gratuitous complexity hid your bracketing errors? :slight_smile:

I’ve written a few genset starters.
You’ve included some good ideas, but you MUST get past using delay().

A state machine is the only way to go, and wherever possible use closed-loop control techniques.
You can’t assume something has happened just because you told it to.

Where you can, consider safety interlocks or pre-start warnings (buzzer etc) to ensure operators or bystanders are safe from moving parts or electric shock.

Trying to activate the arduino internal pullup resistor

That is an obsolete and non-portable method. Use the INPUT_PULLUP option of pinMode().

Thanks, the only coding experience I have is MatLab and that was just enough to get through school? I will try to upload a wiring diagram tonight

An easy thing to remember - generally single variables never need brackets around them... just as in math, you would never need something like (1)+(1).

As brackets are required in many places, littering your code with them makes it hard to read the ones that are necessary and meaningful.

I hope that is "gentle" enough.

Thanks alot, I should have guessed it would be something as simple as a bracket issue. The code is working as it should now and all brackets have been cleaned up. I appreciate the help, This is my first Arduino code so I am sure it is clunky, going to spend a lot of time testing it before I implement it

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.