Help required in void loop function

Hey im fimiliar with programming but a newbie with regards to Arduino projects and development. I'm trying to automate the electric generator to on/off using Arduino Uno and a bunch of relays but im kinda stuck in a little problem while programming. Im trying to code the process in such a way that it can readily detect if the generator gets started in between self. Im using 2 220v relays to sense the presence of mains and generator and a opto-isolated relay module to power on and off the generator accordingly. Im giving ON signal to generator for 3 seconds and then waiting for 5 seconds and again for a total of 3 times.After the third time it should not try to power it on again.I have attached the code i have written which pretty much does the job but is acting weird which i'll explain in a bit.

var = variable for FOR loop wapda = mains 240V supply (utility company_ gen = generator 240V supply genon = output pin to power generator on genoff = output pin to power generator off

int var = 3;
int wapda = 7;
int gen = 8;
int genon = 9;
int genoff = 10;

void setup() {
  
  pinMode(wapda, INPUT);
  digitalWrite(wapda, LOW);
  pinMode(gen, INPUT);
  digitalWrite(gen, LOW);
  pinMode(genon, OUTPUT);
  digitalWrite(genon, LOW);
  pinMode(genoff, OUTPUT);
  digitalWrite(genoff, LOW);
}

void loop()
{
         delay(10000);

        if (digitalRead(wapda) == HIGH && digitalRead(gen) == LOW)
        {}


        if (digitalRead(wapda) == LOW && digitalRead(gen) == LOW)
        {
          delay(10000);
        }
        for(var;var>=1;var--)               // First self start
        {
        if (digitalRead(wapda) == LOW && digitalRead(gen) == LOW)
         {
          digitalWrite(genon, HIGH);
          delay(1000);
          }
          else
           {
            digitalWrite(genon, LOW);
           }
           if (digitalRead(wapda) == LOW && digitalRead(gen) == HIGH)
           {
            digitalWrite(genon, LOW);
           } 
         }

         digitalWrite(genon, LOW);  
         var = 3;                       // First self stop



if (digitalRead(wapda) == LOW && digitalRead(gen) == LOW)
        {
          delay(5000);
        }
        for(var;var>=1;var--)               // Second self start
        {
        if (digitalRead(wapda) == LOW && digitalRead(gen) == LOW)
         {
          digitalWrite(genon, HIGH);
          delay(1000);
          }
          else
           {
            digitalWrite(genon, LOW);
           }
           if (digitalRead(wapda) == LOW && digitalRead(gen) == HIGH)
           {
            digitalWrite(genon, LOW);
           } 
         }

         digitalWrite(genon, LOW);  
         var = 3;                       // Second self stop





         if (digitalRead(wapda) == LOW && digitalRead(gen) == LOW)
        {
          delay(5000);
        }
        for(var;var>=1;var--)               // Third self start
        {
        if (digitalRead(wapda) == LOW && digitalRead(gen) == LOW)
         {
          digitalWrite(genon, HIGH);
          delay(1000);
          }
          else
           {
            digitalWrite(genon, LOW);
           }
           if (digitalRead(wapda) == LOW && digitalRead(gen) == HIGH)
           {
            digitalWrite(genon, LOW);
           } 
         }

         digitalWrite(genon, LOW);        // Third self stop
         var=0;                    

         
      if (digitalRead(wapda) == HIGH && digitalRead(gen) == HIGH)
      {
   
      var = 3;
      delay(10000);
      digitalWrite(genoff, HIGH);
      delay(10000);
      digitalWrite(genoff, LOW);
      digitalWrite(genon, LOW);
      }
}

Problem: As I'm using var = 3 in each loop with delay of 1 sec and condition to check in between, i have set the value of var to 3 after first loop to be used in the second and similarly for third. But after third loop i have set its value to 0 so that when the loop starts again, it should not try to power generator on again. But after about 30 seconds, it does it again!! . I don't want it to try again :/ . I'm not really sure how to fix it.

PS. Same problem with the off statement in the end but it does not matter much since generator gets off the very first time

Any help is greatly appreciated. Thanks

You see where you have the exact same code copy-pasted 3 times? That's a good candidate for making a function.

void genStart() {
  for (var; var >= 1; var--)         
  {
    if (digitalRead(wapda) == LOW && digitalRead(gen) == LOW)
    {
      digitalWrite(genon, HIGH);
      delay(1000);
    }
    else
    {
      digitalWrite(genon, LOW);
    }
    if (digitalRead(wapda) == LOW && digitalRead(gen) == HIGH)
    {
      digitalWrite(genon, LOW);
    }
  }

  digitalWrite(genon, LOW);
}

Now you can re-write the rest of the code to use this function. Then the logic sequence will be much easier to read. I would also recommend making a stop function too.

void loop()
{
  delay(10000);

  if (digitalRead(wapda) == HIGH && digitalRead(gen) == LOW)
  {}

  if (digitalRead(wapda) == LOW && digitalRead(gen) == LOW)
  {
    delay(10000);
  }
  genStart();
  var = 3;                       // First self stop

  if (digitalRead(wapda) == LOW && digitalRead(gen) == LOW)
  {
    delay(5000);
  }
  genStart();
  var = 3;                       // Second self stop

  if (digitalRead(wapda) == LOW && digitalRead(gen) == LOW)
  {
    delay(5000);
  }
  genStart();
  var = 0;

  if (digitalRead(wapda) == HIGH && digitalRead(gen) == HIGH)
  {

    var = 3;
    delay(10000);
    digitalWrite(genoff, HIGH);
    delay(10000);
    digitalWrite(genoff, LOW);
    digitalWrite(genon, LOW);
  }
}

It looks like you want to give it three attempts to start, and within each attempt it should try three times.

I would use a new variable which counts the number of attempts. You can leave var to control the three times it tries to start with each attempt. You will not need all the repeated loops.

int attempts = 0;
int wapda = 7;
int gen = 8;
int genon = 9;
int genoff = 10;

void setup() {

  pinMode(wapda, INPUT);
  digitalWrite(wapda, LOW);
  pinMode(gen, INPUT);
  digitalWrite(gen, LOW);
  pinMode(genon, OUTPUT);
  digitalWrite(genon, LOW);
  pinMode(genoff, OUTPUT);
  digitalWrite(genoff, LOW);
}

void loop()
{
  delay(10000);

  if (digitalRead(wapda) == HIGH && digitalRead(gen) == LOW )
  {attempts = 0;}//mains on/gen off clear attempts

   //mains off/ gen off three attempts to start
  if (digitalRead(wapda) == LOW && digitalRead(gen) == LOW && attempts < 3)
  {
    delay(10000);
   
    for (byte var = 3; var >= 1; var--)  //three times within each attempt      
    {
      if (digitalRead(wapda) == LOW && digitalRead(gen) == LOW)
      {
        digitalWrite(genon, HIGH);
        delay(1000);
      }
      else
      {
        digitalWrite(genon, LOW);
      }
      if (digitalRead(wapda) == LOW && digitalRead(gen) == HIGH)
      {
        digitalWrite(genon, LOW);
      }
    }
    attempts++;
    if(attempts ==3)
     {//error condition}
  }

  digitalWrite(genon, LOW);
  
  if (digitalRead(wapda) == HIGH && digitalRead(gen) == HIGH)
  {
    delay(10000);
    digitalWrite(genoff, HIGH);
    delay(10000);
    digitalWrite(genoff, LOW);
    digitalWrite(genon, LOW);
  }
}
pinMode(wapda, INPUT);
  digitalWrite(wapda, LOW);

You are not using input pull-up, right ? And is that why you set the input to low?

if (digitalRead(wapda) == HIGH && digitalRead(gen) == LOW)
        {}

does nothing, so get rid of it...

What is this really doing?

if (digitalRead(wapda) == LOW && digitalRead(gen) == LOW)
         {
          digitalWrite(genon, HIGH);
          delay(1000);
          }
          else
           {
            digitalWrite(genon, LOW);
           }

I suppose it means if I don't have public power service (outage) and the generator is not turned on then turn the generator on and wait 1 sec. Otherwise stop the generator. Do I get that right? You have declared a genoff pin that you don't seem to use besides at the end. What is its purpose?

.After the third time it should not try to power it on again.I have attached the code i have written which pretty much does the job but is acting weird which i'll explain in a bit.

void loop() {

if im in an "error" state
  if the "it's ok, we're good now" button is being pressed
    turn off the "help! error!" light
    then we are no longer in an error state
  otherwise
    do nothing. return from loop
end

// if we got to this point, we are not in an error state

if we are not currently trying to restart, then
  if 240v is on, then
    do nothing, return from loop
  else 
    woah! Make a not of these important facts:
    1 - we are trying to restart
    2 - we have not made any attempts yet
    3 - we have not stared the next attempt yet
  end
end

// if we got to this point, then we are currently trying to restart

// if we are currently trying to restart, 
//  then either we haven started the attempt yet,
//  or we are in the three second 'ON' phase
//  or we are in the five second 'OFF' phase

if we have not started the next attempt yet, then
  increment the 'number of attempts made' variable
  turn the motor on
  note the time at which we turned the motor on
  note that we are now in "ON" phase"

else, if we are in the ON phase
  if it has been less than three seconds since turning the motor on
    then return do nothing and return.
  otherwise
    turn the motor off
    make a note of the time at which we turned the motor off
    note that we are now in the "off" phase
  end

else // we must be in OFF phase
  if it has been less than five seconds since we turned the motor off
    then return do nothing and return.
  otherwise
    if we have mains power
      awesome!
      note that we are no longer attempting to restart, and everyhting is good
    otherwise
    if we have made three attempts so far, then
      note that we are no longer attempting to restart
      note that we are now in "error" state
      turn on the "help! Error!" light
    otherwise
      the phase is now "not started the next attempt yet"
    end
  end
end    



}