variable from mysql set in setup and used in loop

Hi!
I'm making a project to control electronic valves and measure temperature from Arduino + a Raspberry Pi with MySQL on it.
Arduino gets a temperature from MySQL and opens and closes the valves to regulate incoming water flow.
I need to get the desired temperature from MySQL only once (in setup?) and use it in loop to compare the current temperature from the sensor to the desired temperature that has been set in MySQL.
I understand that if I set the MySQL query in setup, I can not use the variable I get in loop.
But the problem is I can't put it in loop because the MySQL query slows down the loop by 2 - 3 sec.
Can anyone help me with this one? :confused:

Make the variable global:
https://www.arduino.cc/reference/en/language/variables/variable-scope--qualifiers/scope/

I can't put it in loop because the MySQL query slows down the loop by 2 - 3 sec.

setup() sounds like the correct place to read the value. but you could put it in loop() and arrange for it to only be read once. It would then be easier to reset the value by reading it again should it become necessary.

pert:
Make the variable global:
https://www.arduino.cc/reference/en/language/variables/variable-scope--qualifiers/scope/

If I do it like this:

int X = 0; // any function will see this variable

void setup() {
X = 5;
}

void loop() {
use X here in a if statement
}

I think, taht in this scenario X will be 5 in setup and 0 in loop.
How do I get loop to see that setup has changed the variable?

UKHeliBob:
setup() sounds like the correct place to read the value. but you could put it in loop() and arrange for it to only be read once. It would then be easier to reset the value by reading it again should it become necessary.

How do i make a function only run once in loop and don't repeat it again and again?

Isn't loop like while(True):

UKHeliBob:
setup() sounds like the correct place to read the value. but you could put it in loop() and arrange for it to only be read once. It would then be easier to reset the value by reading it again should it become necessary.

OK I got what you mean by it.
Could I do it like this:

int X = 0; // any function will see this variable

void setup() {
//make MySQL connection
}

void loop() {
x = query variable from MySQL
while (True) {
if statement that uses X
}
}

x would be assigned from MySQL in the loop once and the While function would take the looping over.
Couldn't it cause any memory problems?

I think, taht in this scenario X will be 5 in setup and 0 in loop.

Then you are wrong. Did you try it ? The clue is in the word global A value assigned to a variable declared as global is available everywhere in the program. If you set it to 5 in setup() then it will be 5 wherever you use it, including in loop()

How do i make a function only run once in loop and don’t repeat it again and again?

Set a boolean variable (either a global or a static local variable in loop(), let’s name it goGetTheTemp), to true. Then, in loop(), if goGetTheTemp is true get the temperature from the database and set goGetTheTemp to false and it will not happen next time through loop()

UKHeliBob:
Then you are wrong. Did you try it ? The clue is in the word global A value assigned to a variable declared as global is available everywhere in the program. If you set it to 5 in setup() then it will be 5 wherever you use it, including in loop()
Set a boolean variable (either a global or a static local variable in loop(), let's name it goGetTheTemp), to true. Then, in loop(), if goGetTheTemp is true get the temperature from the database and set goGetTheTemp to false and it will not happen next time through loop()

Yes, I tried it. But by setting a new value in setuo() I dont see it in loop()

Yes, I tried it. But by setting a new value in setuo() I dont see it in loop()

Post the code you tried