# Need help with 2 loops with conditions

Zeke Rich used Ask the Experts™
on
Im trying to make a small battery tester.

It is charging up battery fine up to 2kv while reading analog values, then goes into discharging loop and dies there.  I need help with the second loop.

I want to charge and discharge while reading analog values over and over again in endless loop.

``````void loop(){

// read the raw data coming in on analog pin 0:
// Convert the raw data value (0 - 1023) to voltage (0.0V - 5.0V):
float voltage = val * (5.0 / 1024.0);
//convert to actual voltage
int volts = voltage * 10000;

//if the voltage is less than 2,000v keep charging
if (voltage < 0.19){
lcd.setCursor(1,1);
lcd.print("Charging     ");
digitalWrite(chargePin, HIGH);

//print to lcd
lcd.setCursor(1,0);
lcd.print("Volts: ");
lcd.print(volts);
lcd.print("     ");
delay(100);
}

else{
//we reached 2000v, discharge untill fully discharged
while (voltage > 0.01){
lcd.setCursor(1,1);
lcd.print("Discharging    ");
digitalWrite(chargePin, LOW);

//print to lcd
lcd.setCursor(1,0);
lcd.print("Volts: ");
lcd.print(volts);
lcd.print("     ");
delay(100);
}
}

}
``````
Comment
Watch Question

Do more with

EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Hi Zeke Rich,

I'm not sure if I understand correctly, what you're doing there, but IMO the problem obiously is that the 'voltage' doesn't change its value within the while-loop, so if it is entered (if voltage is greater than 0.01) the while loop runs infiniteley coz 'voltage' can't shrink to a value less than 0.01.

I think uess you need to read the info used to calculate 'voltage' within the while-loop.

Hope this helps,

ZOPPO
"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
Sorry, but this code cannot work at all. There is no charging loop, and when discharging you do not read in from the sensor. This so fundamentally wrong you should step back and start all over again. Your task is really simple:

define vars as needed
build an endless loop with while (true)
read in sensor data and convert to real values - use only a single scale like your volts variable
if  voltage < 2kV
display charging text
set charge signal
else
display discharging text
set discharge signal
display current voltage
wait for a few millisecs.
[/list]
Because displaying the voltage is common for charging and discharging, you should run ghat outside of the if. Otherwise you repeat code unnecessarily.
Owner

Commented:
The charging part is working perfectly.  It only needs to go High during charging to trigger a 2kv charge. This part is fine.  The only part i need help with is after "else". Thank you.
"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
You might not have to set the charging signal in the loop, but querying the sensor always has to be done in a loop. Also, the delay of 100 (ms) as in your original code cannot be sufficient to charge the battery.
Do want to have one single charge/discharge cycle, or should that run endless? My suggestion above was based on "endless".
Owner

Commented:
I tried to use while, but it does not work.

``````void loop(){

// read the raw data coming in on analog pin 0:
// Convert the raw data value (0 - 1023) to voltage (0.0V - 5.0V):
float voltage = val * (5.0 / 1024.0);
//convert to actual voltage
int volts = voltage * 10000;

//if the voltage is less than 2,000v keep charging
while (voltage < 0.19){
lcd.setCursor(1,1);
lcd.print("Charging     ");
digitalWrite(chargePin, HIGH);

//print to lcd
lcd.setCursor(1,0);
lcd.print("Volts: ");
lcd.print(volts);
lcd.print("     ");
delay(100);
}

//we reached 2000v, discharge untill fully discharged
while (voltage > 0.01){
lcd.setCursor(1,1);
lcd.print("Discharging    ");
digitalWrite(chargePin, LOW);

//print to lcd
lcd.setCursor(1,0);
lcd.print("Volts: ");
lcd.print(volts);
lcd.print("     ");
delay(100);
}

}
``````
Commented:
Well, still the 'voltage' isn't change in the while-loop, so it never can end.

IMO the while loop should look somehow like this:

while ( ( voltage = analogRead( analogPin ) * (5.0 / 1024.0) ) > 0.01 )
{
...
lcd.print( voltage * 10000 ); // use new 'voltage' to calulate 'volts'
...
}

Regards,

ZOPPO
Owner

Commented:
Thank you experts!

Do more with