Go Down

Topic: if(..) with unexpected behavior (Read 491 times) previous topic - next topic

brice3010

Mar 21, 2017, 06:04 pm Last Edit: Mar 21, 2017, 06:11 pm by brice3010
In a PSU program the display of voltage, current and 2 temperatures are done on a 16x2 LCD.
There is a condition that if a short circuit occurs the second line text (current and temperature 2) is replaced by "kortsluiting" (short circuit).
In a loop, voltage, current and 2 temperatures are read x+2 times, minimum and maximum values are deleted and the average is taken. During this loop there is constant monitoring of voltage and current for short circuit conditions.
If this occurs two flags are set "true": warning and OC. If these do not occur line 2 should display current and temperature 2.

However, on startup with no short circuit condition present the second line remains set at the startup text "Test".

The first line properly displays voltage and temperature 1.

I tested the presence of short circuit detection by setting or deleting LCD backlight depending on the output voltage. Short circuit is simulated by getting over or under the 10V threshold (int V1).

1. when no short circuit is detected the backlight stays on but the second line (current and temperature 2) never appears, "Test" (the startup line) always remains in place
2. as soon as a short circuit is simulated the backlight goes out, the short circuit  message ("kortsluiting") appears
3. when the short circuit condition is removed, the backlight goes back on but the short circuit message remains.

So even when the "if" condition for proper line 2 display is valid (no short circuit and backlight on again) this line does not get displayed.

Any help will be greatly appreciated; my book of tricks is exhausted.

The short circuit detection code:

Code: [Select]

 if (voltageValue/1000 < V1 && currentValue/1000 < C1)   // shortcircuit
       {
         OC = true;
         lcd.setCursor (0, 1);
         lcd.print("kortsluiting    ");
         warning = true;
         lcd.noBacklight();
        }
    else                                 // no shortcircuit;
        {
          OC = false;
          warning = false;
          lcd.backlight();
         }


The display code:
Code: [Select]

    dtostrf(voltageDisplay, 5, 2, outvolt); // display output voltage
    dtostrf(currentDisplay, 5, 2, outcurr); //add 2 to x because no removal minimum and maximum values from current
    dtostrf(temp1Value, 3, 0, outtemp1);    //display darlington temp
    dtostrf(temp2Value, 3, 0, outtemp2);    //display heatsink temp
    lcd.home ();
    lcd.print("U=");
    lcd.print(outvolt);
    lcd.print("V");
    lcd.print(" T1=");
    lcd.print(outtemp2); // cooling fin temp on line 1
    lcd.print("C");
    lcd.setCursor (0, 1); // go to start of 2nd line
   if (warning = false)
   {
    lcd.print("I=");
    lcd.print(outcurr);
    lcd.print("A");
    lcd.print(" T2=");
    lcd.print(outtemp1);
    lcd.print("C");
   }


And the full program (I disabled anything not having to do with this issue using /* and */, otherwise too much clutter) is in attachment.



GrooveFlotilla

Some people are like Slinkies.

Not really good for anything, but they bring a smile to your face when pushed down the stairs.

brice3010

Code: [Select]
   if (warning = false)Oops
Please give me some more info? My brain is utterly short circuited now when I look at the code..

brice3010


septillion

Indeed ;) = for assignment, == for comparing.

That's why most check bools like if(warning) or if(!warning).


And uhm, going with the fixed point part:
Code: [Select]
if (voltageValue/1000 < V1 && currentValue/1000 < C1)   // shortcircuit

is a bit awkward. Besides the still silly varaible names, you also have the problem that V1 and C1 can only set a threshold in whole increments of 1000. If you don't want that, give V1 and C1 (after giving them a better name and putting them in an array ;) ) the same unit as voltage and current.
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

brice3010

Indeed ;) = for assignment, == for comparing.

That's why most check bools like if(warning) or if(!warning).


And uhm, going with the fixed point part:
Code: [Select]
if (voltageValue/1000 < V1 && currentValue/1000 < C1)   // shortcircuit

is a bit awkward. Besides the still silly varaible names, you also have the problem that V1 and C1 can only set a threshold in whole increments of 1000. If you don't want that, give V1 and C1 (after giving them a better name and putting them in an array ;) ) the same unit as voltage and current.
I know, that will be fixed after I get my warning messages displayed properly (right now I have an issue there), I have not forgotten your previous help items from a previous thread.


Go Up