Hello people!
I'm developing a system that controls the ph of a water tank for an hydroponic crop with an ESP32 (38 pins version).
My program reads the voltage value of the sensor and calculates pH. For stability, it takes a read from sensor one time per second over sixty seconds and makes an average of those reads.
The program can also calibrate the sensor. For this it takes the voltage read from the sensor one time per second over a minute, takes the average, it does the same with the other calibration solution and makes the calculations to find the calibration values.
This is the code: (it is a short version using just the serial to comunicate, but the real program has a web page and web sockets to comunicate. I chop it down first to see if my problem was in other part of code and second to not make you read +600 lines of code).
The problem that i'm facing is that both calibration == true and calibration == false are still executing even when the other is running. I notice this because when you try to calibrate after a few minutes the program has started all the measures appear instantly. And when return to measuring mode, all measures of pH apear instantly.
I have no clue why this is happening since even inside if (calibrating == true){} there is another if that is not supposed to get executed until I enter a solution number.
I'm sorry for the long post.
Hope you can throw me an idea for where to start because i don't even know how can this be possible. xD
Thanks in advance!
Edit: I changed the code to spare the need of ads and pH sensor, using fixed values. So anyone can load the code and see if the error is reproducible. Also, i recorded a video of the serial monitor, but i can't upload it here. So im uploading it to youtube and posting it here soon.
**Here is the video: Problem with if statements executing on background when they shouldn't - YouTube **
At the start of loop(), you try to read a number from Serial. If you don't send a number within one second, the number you get is zero, which sets calibrating = false;
If you send a 1 for option 1 to set calibrating = true, the next time you get to the top of loop() and have not sent a 1, it will see a zero again.
Yes. I tried. And are ok. That's what trips me off.
The solution variable goes to 0 after calibrating.
And also, calibrating variable changes as expected. i know this because program alternates between measuring and calibrating mode as expected.
The problem seems that despite what mode is in, the program still executes the other mode in background.
It doesn't matter. This program alternates just fine between modes. And also the real program uses websockets to change the calibrating variable, and the problem is the same.
I know that might be something wrong.
But It scapes me what.
AFAIK what happens in an If, It should not be happening in the other if that compares the same variable. It's like it's happening on background.
So we've ruled out "the compiler is broken". I suggest you revisit post # 2, I think you're making passes through the code with oscillations between true and false.
Ok. That's a good start, thanks I'm gonna see what can i do to avoid that. But i think that problem is belong to this particular extrat since my original code is getting the value from a web page via web socket.
hi , I does not know where is problem with calibrating, but testing float for equality is very bad idea, because this condition has very little chance of returning true.
Good to know. I'm gonna try changing It to a int.
But anyways, the sketch seems to correctly detect that variable because when i enter calibration mode and enter the solución number, sketch does what's expected. It's Just makes every read in less than one second. And the same happens when i return to measuring mode: all measurings that happened when in calibrating mode appears at once.
Probably later today I'm gonna upload a video of the serial monitor that shows the problem.
I tried changing that variable to an int, and still the program does the same.
I'm gonna try to change order of if's to see if that helps. And in the meantime i'm uploading a video to illustrate the problem I'm facing.
I discarded that the ESP32 was the problem using another ESP32 from another manufacturer.
I suspected that had nothing to do with the ESP, but worth the try. xD