Go Down

Topic: Why is my variable loosing its value? (Read 548 times) previous topic - next topic

jaydie


Why is my variable loosing its value?
I'm working with this code, I think there is still something wrong with the formula but anyway It's not about the formula its about the variable ContR. By principle I should be able to use it as lcd.print(ContR) on the last line of my program but for some reason it won't work so I just make a work around by copying its value to another variable.
Code: [Select]
int test;
    float dRes;
    float RefV=5.00;
    float ContR;
    float dContR
    test=analogRead(A0);
    dRes=map(test, 0, 1023, 0, 6000);//Map A/D digital data. 6V max
    dRes=(dRes/1000);//To down scale reading to proper value

    if (RefV>dRes){//Do only if there was a voltage drop
    dRes=(RefV-dRes);//Get the voltage drop
    ContR=(dRes/(RefV/10000));//R1=V1/(V2/R2))
    dContR=ContR; //copying the value to another variable (my work arround so it would work with my LCD Display)
    Serial.println(ContR); //<---- OK! displays the right value
    }

//more code here but not relevant, ContR variable was never used again only the dRes variable was reused here.

    Serial.println(ContR);// <---- Displays 0
    lcd.println(ContR);// <---- Displays 0
    lcd.print(dContR);// <---- OK! displays the right value

Could this be a bug?

dxw00d


xolroc

I have no idea why your code was working, and this probably has nothing to do with the problem, but
Code: [Select]
float dContR
Should be
Code: [Select]
float dContR;

Graynomad

Code: [Select]
float dContR
That would not even compile.

_____
Rob
Rob Gray aka the GRAYnomad www.robgray.com

bubulindo

Code: [Select]
   Serial.println(ContR);// <---- Displays 0
    lcd.println(ContR);// <---- Displays 0
    lcd.print(dContR);// <---- OK! displays the right value


Have you tried using the same instructions for both variables? Or maybe change the order you use them?

Like

Code: [Select]
   Serial.println(dContR);// <---- Displays 0
    lcd.println(dContR);// <---- Displays 0
    lcd.print(ContR);// <---- OK! displays the right value


Or

Code: [Select]
   Serial.println(ContR);// <---- Displays 0
    lcd.print(ContR);// <---- Displays 0
    lcd.print(dContR);// <---- OK! displays the right value


?
This... is a hobby.

Go Up