Keeping variable value

I have two DHT22 sensors, one for indoor and one for outdoor temperature readings.
I have two ESP8266 boards talking to each other and collating DATA to send to Thingspeak.
Everything works perfectly apart from the following.

I have declared two global variables, float internal_reading; and float external_reading;
In void loop() the indoor DHT22 data is read and displayed using Serial.print.
I then loop out to void read_outdoor() and the exterior DHT22 data is read.
If I Serial.print this value it is correct but when the program returns to void loop() and Serial.print the outdoor variable has reset to 0

I thought that global variables could be changed within the program and the value would be remembered until a reset (power down)?

explaining what you wrote is not as helpful as showing what you wrote

I suspect there's a problem with the code and it is doing what you wrote and not what you thought/hoped it would do. If you post the code we may be able to tell what is happening.

Steve

That is the main critique for having global variables. Anything, anywhere, could look at and alter the value of the variable and it will not be immediately obvious what did it.

Not such an issue with Arduino as we generally write pretty small applications, but if you had a very large codebase, especially with multiple people working on it, then it can be a right pain!

In case it's too subtle

@killzone_kid is asking you to post the code, or a small example that does the same thing wrong.

Hmmm, slowest typing of all. :expressionless:

a7

Oftentimes when this happens, it's because the code intended to set the global actually declares a new variable with the same name.

I have found my problem. The line of code that I had originally in the void read_outdoor() loop is as follows:

float readingInt4 = RECserver.arg("sensor_reading4").toFloat();

I have removed float from the begining and all is good.

readingInt4 = RECserver.arg("sensor_reading4").toFloat();


I have several working sketches (TFT Display, Read external sensor, Read API, Get epoch time etc etc) and I have been putting them all together to form one sketch.
One of the problems with copy and paste :grinning:

Thank you all anyway

Yeah we would have seen that right away you posted the code.

They are called functions, BTW, not voids.

a7

The code is MASSIVE. It would have been a nightmare to find the error that is why I just asked the question about global variables

Am I right in thinking that leaving the 'float' in declared the variable as local?

That’s exactly right. It was a declaration, visible only within the { } containing it.

Here the variable kk is a bit different, again local, visible within the body of the loop:

    for  (int kk = 0; kk < 42; kk++) {
// variable kk visible here, not outside the for statement
    }

HTH

edit: fixed thinko/typo in example

a7

Named variable in my honour, well thank you!