# Confused about "IF" in my code!

I build a charger for my battery, connected to relay and everything work.
but I am confused about using “IF” in my code, I have a rechargeable lithium battery 3.7V

}

if (v2 <= 2.1) { digitalWrite (2,HIGH);

}

else if (v2 >= 3.4) { digitalWrite (2,LOW);

}

So from my code if the battery under 2V the relay will connect the charger.
but what if the charger start charging at 2.1 , if the battery is 2.2 the charger will stop again!!!
This is my full code

int thermistorPin = A1; //analog pin 0
int Relay = 2;
float vPow = 4.7;
float r1 = 50000.0;
float r2 = 4400.0;

void setup() {
Serial.begin(9600);

// Send ANSI terminal codes
Serial.print("\x1B");
Serial.print("[2J");
Serial.print("\x1B");
Serial.println("[H");
// End ANSI terminal codes

Serial.println("--------------------");
Serial.println("DC VOLTMETER");
Serial.print("Maximum Voltage: ");
Serial.print((int)(vPow / (r2 / (r1 + r2))));
Serial.println("V");
Serial.println("--------------------");
Serial.println("");

delay(2000);
}

void loop() {
float v = (analogRead(0) * vPow) / 1024.0;
float v2 = v / (r2 / (r1 + r2));
// Send ANSI terminal codes
Serial.print("\x1B");
Serial.print("");

if (thermistorReading <= 50) { digitalWrite (2,HIGH);

}

else if (thermistorReading >= 51) { digitalWrite (2,LOW);

}

if (v2 <= 2.0) { digitalWrite (2,HIGH);

}

else if (v2 >= 3.2) { digitalWrite (2,LOW);

}

Serial.println(v2);

delay(8000);

}

but what about stop charging when the battery is full!?

That's probably a good idea.

We can't see what type v2 is, nor have you provided a clue as to where the value in it comes from.

sorry i made an update for the main question to be more clear, i build a small circuit “voltmeter” and call it V2 in my code so V2 is the voltage value to my battery

Hi,
No it will not switch pin 2 OFF until v2 >= 3.4.

You can regard each output as a latched output, once you set it HIGH the output will remain HIGH until it is commanded LOW.

Tom.....

Are you sure Tom!
Just think about it more deeply

}

if (v2 <= 2.1) { digitalWrite (2,HIGH);

}

else if (v2 >= 3.4) { digitalWrite (2,LOW);

}

battery under or equal 2.1V so the relay will connect the charger.
but when the battery is 2.2V the relay will stop again and enter in infinity loop!! so my code will be stuck between 2.1V - 2.2V
or i am understand it wrong !

but when the battery is 2.2V the relay will stop again

What is connected to pin 2? Why is that pin controlled by the thermistor reading AND the voltage reading INDEPENDENTLY?

if you want to know how exactly my circuit work, i am still working in my code i know the code not make sense for that i am asking

You need a (series of) compound if statement(s).
If the voltage is low OR the thermistor is low, turn the pin on.
If the voltage is good AND the thermistor is good, do nothing.
If the voltage is high OR the thermistor is high, turn the pin off.

You do NOT want to turn the pin on because the thermistor is low and then immediately turn it off because the voltage is good.

What about making the code like this!

if (thermistorReading <= 50) { digitalWrite (2,HIGH);
}
else (v2 <= 2.0) { digitalWrite (2,HIGH);
}
if (thermistorReading >= 51) { digitalWrite (2,LOW);
}
else (v2 >= 3.2) { digitalWrite (2,LOW);
}

UPDATE
nooo that’s wrong too, your method is better

if (thermistorReading <= 50 || v2 <= 2.0) { digitalWrite (2,HIGH);
}
else if
(thermistorReading >= 51 || v2 >= 3.2 ) { digitalWrite (2,LOW);
}

Solved
and from what Tom said

once you set it HIGH the output will remain HIGH until it is commanded LOW.

I am in the safe side
Thanks PaulS & Tom +1