I'm observing an IF statement violation...what I'm going to show is extremely strange. Let me describe the situation. All the variables below are GLOBAL and declared "volatile unsigned long". All the variables can be changed only in an interrupt routine (except G_Hall_counts_previous....which doesn't matter). The below code runs in the main loop. It should only execute if G_delta_HALL_time>120.
This is the print out from the serial monitor:
**** MAIN LOOP *****
G_Hall_counts=348
G_delta_HALL_time=0
How is it possible this was printed by the Serial monitor when G_delta_HALL_time=0 !!!!!! G_delta_HALL_time needs to be >120 to execute the code after the IF.
Extremely unlikely. It is almost certainly a bug, or misunderstanding on your part about what is supposed to happen.
You did not provide full code, only a snippet. So your question can't be answered. Please post either the complete sketch, or better, an MRE (minimal reproducible example) that demonstrates the problem.
Trust me, the impossible is not happening.
By the way, you are not allowed to call Serial functions with interrupts disabled. That is because Serial is interrupt driven.
Turn the interrupts back on, and maybe the code will work.
If G_Hall_counts is being modified by an interrupt routine, the correct way to protect that variable for printing is to make a copy with the interrupts off, turn the interrupts back on and print the copy.
Don't use Serial.print() within a "no interrupts" group because this itself uses interrupts. The ESP32 is a 32 bit processor so actions on unsigned long variables are anyway atomic.
Edit
On the ESP32 the F() macro does nothing useful.
Did you get any compiler warnings ?
Thanks for your input. I'm not familiar with "mutex/semaphore" or how to "flush" my print statements? Please elaborate on how to modify my code. I'm programming using Arduino.
It is possible that the value of G_delta_HALL_time changed in the interrupt routine after the if statement was evaluated but before the Serial.println statements were executed in the main loop. This is because the variable is declared as "volatile", which tells the compiler that the variable can be modified by an external source (e.g. an interrupt) and therefore should not be optimized by the compiler.
To verify this, you can add a print statement in the interrupt routine to check the value of G_delta_HALL_time when it changes. Alternatively, you can add a delay before the Serial.println statements in the main loop to ensure that enough time has passed for the interrupt routine to complete and for any changes to G_delta_HALL_time to be reflected.
As previously stated, I'm using ESP32 Wrover Module. The full code isn't needed. The problem is well defined by my post. You would not be able to run my code as the interrupt is driven by a HALL sensor which detects rotation of the system I'm developing. G_delta_HALL_time is modified in the interrupt routine. My best guess as to the problem is that, for whatever reason, Serial.println(G_delta_HALL_time); does not correctly print G_delta_HALL_time....I don't know why? G_HALL_counts prints correctly and it is also modified in the interrupt routine.
As @6v6gt pointed out, on the ESP32 there is no problem with integers being corrupted by interrupt/main access conflict, so you don't need to turn the interrupts off at all.