Problem to run all the line without skip

Hi! Can you please help me with my code? I attach a copy below.
I am using arduino uno, reading voltage of the 12V battery, running the coding during its discharging. I have 5 load connected to the battery. So from 12V, it will decreasing, i set when the voltage is at 11V, i want the first load off. After that, the voltage will slightly increase(as 1 load are not connected with the battery), then it start decreasing, reach 11V, second load will off, same condition will be run untill the fifth load off.
When i run this coding, the line under else, all if condition run at the same time. I try to put delay, but the delay just function to delay the print vbatt. i want to put dely, so that it can wait for the battery to increase the voltage after one load off, and then voltage decrease.

int mode = 0;

void setup() {
  Serial.begin(9600);
  pinMode(Load1, OUTPUT);
  pinMode(Load2, OUTPUT);
  pinMode(Load3, OUTPUT);
  pinMode(Load4, OUTPUT);
  pinMode(Load5, OUTPUT);
  pinMode(A0, INPUT);

}

void loop() {

  v = (analogRead(0) * vPow) / 1023.0;
  vbatt = (v / (R2 / (R1 + R2)))+1.26;
  delay(1000);
  
  if (vbatt >= 11.00) {
        digitalWrite(Load1, HIGH);
        digitalWrite(Load2, HIGH);
        digitalWrite(Load3, HIGH);
        digitalWrite(Load4, HIGH);
        digitalWrite(Load5, HIGH);
        Serial.print("vbatt all high =");
        Serial.println(vbatt);
  }
  
  else {
        if (mode=0)
    {   digitalWrite(Load1, LOW);
        delay(1000);
        digitalWrite(Load2, HIGH);
        digitalWrite(Load3, HIGH);
        digitalWrite(Load4, HIGH);
        digitalWrite(Load5, HIGH);
        Serial.print("vbatt load1 low =");
        Serial.println(vbatt);
        mode = 1;
    }
    delay(1000);
        
        if (mode=1)
    {   digitalWrite(Load1, LOW);
        digitalWrite(Load2, LOW);
        delay(5000);
        digitalWrite(Load3, HIGH);
        digitalWrite(Load4, HIGH);
        digitalWrite(Load5, HIGH);
        Serial.print("vbatt load2 low =");
        Serial.println(vbatt);
        mode = 2;
   
    }

    delay(2000);
        if (mode=2)
    {   digitalWrite(Load1, LOW);
        digitalWrite(Load2, LOW);
        digitalWrite(Load3, LOW);
        delay(9000);
        digitalWrite(Load4, HIGH);
        digitalWrite(Load5, HIGH);
        Serial.print("vbatt load3 low  =");
        Serial.println(vbatt);
        mode = 3;
        
    }

    delay(3000);
        if (mode=3)
    {   digitalWrite(Load1, LOW);
        digitalWrite(Load2, LOW);
        digitalWrite(Load3, LOW);
        digitalWrite(Load4, LOW);
        delay(13000);
        digitalWrite(Load5, HIGH);
        Serial.print("vbatt load4 low  =");
        Serial.println(vbatt);
        mode = 4;
    }
    delay(4000);
    
        if (mode=4)
    {   digitalWrite(Load1, LOW);
        digitalWrite(Load2, LOW);
        digitalWrite(Load3, LOW);
        digitalWrite(Load4, LOW);
        digitalWrite(Load5, LOW);
        delay(17000);
        Serial.print("vbatt load5 low  =");
        Serial.println(vbatt);
        mode = 5;
 
    }
    } // end else
} // end void loop

You probably mean this

(mode=0)

to be this

(mode==0)

Same goes for all the other statements.

Thanks marco_c for your reply

(mode==0)

I have edit that in my code… but after the first load off, another 4 load follow to off, too…
Why is it the load not off one by one?

void loop() {

  v = (analogRead(0) * vPow) / 1023.0;
  vbatt = (v / (R2 / (R1 + R2)))+1.26-1.02;
  delay(1000);
  Serial.print("DATA, TIME, ");
  
  if (vbatt >= 11.00) {
        digitalWrite(Load1, HIGH);
        digitalWrite(Load2, HIGH);
        digitalWrite(Load3, HIGH);
        digitalWrite(Load4, HIGH);
        digitalWrite(Load5, HIGH);
        Serial.print("vbatt all high =");
        Serial.println(vbatt);
  }
  
  else {
        if (mode==0)
    {   digitalWrite(Load1, LOW);
        digitalWrite(Load2, HIGH);
        digitalWrite(Load3, HIGH);
        digitalWrite(Load4, HIGH);
        digitalWrite(Load5, HIGH);
        Serial.print("vbatt load1 low =");
        Serial.println(vbatt);
        mode = 1;
    }
        if (mode==1)
        
    {   digitalWrite(Load1, LOW);
        digitalWrite(Load2, LOW);
        digitalWrite(Load3, HIGH);
        digitalWrite(Load4, HIGH);
        digitalWrite(Load5, HIGH);
        Serial.print("vbatt load2 low =");
        Serial.println(vbatt);
        mode = 2;
   
    }
        if (mode==2)
    {   digitalWrite(Load1, LOW);
        digitalWrite(Load2, LOW);
        digitalWrite(Load3, LOW);
        digitalWrite(Load4, HIGH);
        digitalWrite(Load5, HIGH);
        Serial.print("vbatt load3 low  =");
        Serial.println(vbatt);
        mode = 3;
        
    }

    delay(3000);
        if (mode==3)
    {   digitalWrite(Load1, LOW);
        digitalWrite(Load2, LOW);
        digitalWrite(Load3, LOW);
        digitalWrite(Load4, LOW);
        digitalWrite(Load5, HIGH);
        Serial.print("vbatt load4 low  =");
        Serial.println(vbatt);
        mode = 4;
    }
    
        if (mode==4)
    {   digitalWrite(Load1, LOW);
        digitalWrite(Load2, LOW);
        digitalWrite(Load3, LOW);
        digitalWrite(Load4, LOW);
        digitalWrite(Load5, LOW);
        Serial.print("vbatt load5 low  =");
        Serial.println(vbatt);
        mode = 5;
 
    }
    } // end else
} // end void loop

Your code has several instances of this type of thing

    if (mode == 0)
    {
      digitalWrite(Load1, LOW);
      digitalWrite(Load2, HIGH);
      digitalWrite(Load3, HIGH);
      digitalWrite(Load4, HIGH);
      digitalWrite(Load5, HIGH);
      Serial.print("vbatt load1 low =");
      Serial.println(vbatt);
      mode = 1;
    }
    if (mode == 1)
    {

Shouldn’t the second if be an else if ?

Otherwise once mode equals 1 the condition for the second if will be satisfied and its code block will run. The same thing occurs throughout the code with mode increasing in value and the subsequent if clause being satisfied.

And a small tip, press Ctrl+T in the IDE. Looks better, doesn't it?

septillion:
And a small tip, press Ctrl+T in the IDE. Looks better, doesn't it?

Indeed it does. That is what I did before looking at the code and the snippet that I posted had been Auto Formatted, which showed the problem very clearly especially with Auto Format set to put each { and } on its own line

Thank you UKHeliBob and septillion…
I put “else if” for the second if until the last one(at all load LOW). When i run, the load are off with blink(the load are all lamp). When second load want to off, the first load will on also(before this load1 already off), and then both(load1 and load2) off. What i have to do do if i want the voltage battery can get in to increase its voltage after each load are not connected. Is it i have to use timer or delay?
And then i put the formula in each statement, as the error of the voltage divider are different when each load off. Is it what i am doing is correct? below are the code that i have been edit.

void loop() {

  v = (analogRead(0) * vPow) / 1023.0;
  vbatt = (v / (R2 / (R1 + R2)))+0.24;
  delay(1000);
  Serial.print("DATA, TIME, ");

  if (vbatt >= 11.00) {

    digitalWrite(Load1, HIGH);
    digitalWrite(Load2, HIGH);
    digitalWrite(Load3, HIGH);
    digitalWrite(Load4, HIGH);
    digitalWrite(Load5, HIGH);
    Serial.print("vbatt all high =");
    Serial.println(vbatt);
  }

  else {
    if (mode == 0)
    { 
      
      digitalWrite(Load1, LOW);
      digitalWrite(Load2, HIGH);
      digitalWrite(Load3, HIGH);
      digitalWrite(Load4, HIGH);
      digitalWrite(Load5, HIGH);
      v = (analogRead(0) * vPow) / 1023.0;
      vbatt = (v / (R2 / (R1 + R2)))+0.25;
      Serial.print("vbatt load1 low =");
      Serial.println(vbatt);
      mode = 1;
    }
    else if (mode == 1)

    { 
      digitalWrite(Load1, LOW);
      digitalWrite(Load2, LOW);
      digitalWrite(Load3, HIGH);
      digitalWrite(Load4, HIGH);
      digitalWrite(Load5, HIGH);
      v = (analogRead(0) * vPow) / 1023.0;
      vbatt = (v / (R2 / (R1 + R2)))+0.49;
      Serial.print("vbatt load2 low =");
      Serial.println(vbatt);
      mode = 2;

    }
    else if (mode == 2)
    { 
      digitalWrite(Load1, LOW);
      digitalWrite(Load2, LOW);
      digitalWrite(Load3, LOW);
      digitalWrite(Load4, HIGH);
      digitalWrite(Load5, HIGH);
      v = (analogRead(0) * vPow) / 1023.0;
      vbatt = (v / (R2 / (R1 + R2)))+0.72;
      Serial.print("vbatt load3 low  =");
      Serial.println(vbatt);
      mode = 3;

    }
    else if (mode == 3)
    { 
      digitalWrite(Load1, LOW);
      digitalWrite(Load2, LOW);
      digitalWrite(Load3, LOW);
      digitalWrite(Load4, LOW);
      digitalWrite(Load5, HIGH);
      v = (analogRead(0) * vPow) / 1023.0;
      vbatt = (v / (R2 / (R1 + R2)))+0.92;
      Serial.print("vbatt load4 low  =");
      Serial.println(vbatt);
      mode = 4;
    }

    else if (mode == 4)
    { 
      digitalWrite(Load1, LOW);
      digitalWrite(Load2, LOW);
      digitalWrite(Load3, LOW);
      digitalWrite(Load4, LOW);
      digitalWrite(Load5, LOW);
      v = (analogRead(0) * vPow) / 1023.0;
      vbatt = (v / (R2 / (R1 + R2)))+1.16;
      Serial.print("vbatt load5 low  =");
      Serial.println(vbatt);
      mode = 5;

    }
  } // end else
} // end void loop

@ArduinoTED, please don't use small type. It makes your posts very difficult for older eyes to read - especially on a smaller screen.

...R

Oh thank you @Robin2. Im really2 sorry for that. I will post it with bigger font.

"Thank you UKHeliBob and septillion..

I put "else if" for the second if until the last one(at all load LOW). When i run, the load are off with blink(the load are all lamp). When second load want to off, the first load will on also(before this load1 already off), and then both(load1 and load2) off. What i have to do do if i want the voltage battery can get in to increase its voltage after each load are not connected. Is it i have to use timer or delay?

And then i put the formula in each statement, as the error of the voltage divider are different when each load off. Is it what i am doing is correct? below are the code that i have been edit."

void loop() {

  v = (analogRead(0) * vPow) / 1023.0;
  vbatt = (v / (R2 / (R1 + R2)))+0.24;
  delay(1000);
  Serial.print("DATA, TIME, ");

  if (vbatt >= 11.00) {

    digitalWrite(Load1, HIGH);
    digitalWrite(Load2, HIGH);
    digitalWrite(Load3, HIGH);
    digitalWrite(Load4, HIGH);
    digitalWrite(Load5, HIGH);
    Serial.print("vbatt all high =");
    Serial.println(vbatt);
  }

  else {
    if (mode == 0)
    { 
      
      digitalWrite(Load1, LOW);
      digitalWrite(Load2, HIGH);
      digitalWrite(Load3, HIGH);
      digitalWrite(Load4, HIGH);
      digitalWrite(Load5, HIGH);
      v = (analogRead(0) * vPow) / 1023.0;
      vbatt = (v / (R2 / (R1 + R2)))+0.25;
      Serial.print("vbatt load1 low =");
      Serial.println(vbatt);
      mode = 1;
    }
    else if (mode == 1)

    { 
      digitalWrite(Load1, LOW);
      digitalWrite(Load2, LOW);
      digitalWrite(Load3, HIGH);
      digitalWrite(Load4, HIGH);
      digitalWrite(Load5, HIGH);
      v = (analogRead(0) * vPow) / 1023.0;
      vbatt = (v / (R2 / (R1 + R2)))+0.49;
      Serial.print("vbatt load2 low =");
      Serial.println(vbatt);
      mode = 2;

    }
    else if (mode == 2)
    { 
      digitalWrite(Load1, LOW);
      digitalWrite(Load2, LOW);
      digitalWrite(Load3, LOW);
      digitalWrite(Load4, HIGH);
      digitalWrite(Load5, HIGH);
      v = (analogRead(0) * vPow) / 1023.0;
      vbatt = (v / (R2 / (R1 + R2)))+0.72;
      Serial.print("vbatt load3 low  =");
      Serial.println(vbatt);
      mode = 3;

    }
    else if (mode == 3)
    { 
      digitalWrite(Load1, LOW);
      digitalWrite(Load2, LOW);
      digitalWrite(Load3, LOW);
      digitalWrite(Load4, LOW);
      digitalWrite(Load5, HIGH);
      v = (analogRead(0) * vPow) / 1023.0;
      vbatt = (v / (R2 / (R1 + R2)))+0.92;
      Serial.print("vbatt load4 low  =");
      Serial.println(vbatt);
      mode = 4;
    }

    else if (mode == 4)
    { 
      digitalWrite(Load1, LOW);
      digitalWrite(Load2, LOW);
      digitalWrite(Load3, LOW);
      digitalWrite(Load4, LOW);
      digitalWrite(Load5, LOW);
      v = (analogRead(0) * vPow) / 1023.0;
      vbatt = (v / (R2 / (R1 + R2)))+1.16;
      Serial.print("vbatt load5 low  =");
      Serial.println(vbatt);
      mode = 5;

    }
  } // end else
} // end void loop