Pages: [1]   Go Down
Author Topic: Why is my variable loosing its value?  (Read 416 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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:
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?
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

We need to see the whole sketch.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 135
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8601
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
float dContR
That would not even compile.

_____
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

'round the world...
Offline Offline
Faraday Member
**
Karma: 42
Posts: 3289
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
   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:
   Serial.println(dContR);// <---- Displays 0
    lcd.println(dContR);// <---- Displays 0
    lcd.print(ContR);// <---- OK! displays the right value

Or

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

?
Logged

Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

Pages: [1]   Go Up
Jump to: