Hi, I am having the strangest bug. My integer is changing values depending on if I serial.print it earlier in the program. I know the actaul value changes, not just what is printed, because it goes into If statements that it shouldn’t.
description what happens:
I expect _testIterationscounter to be 0 at the start of my testing. I print it in the middle section of code with Serial.println(_testIterationscounter + 1). This returns 16809 (this changes depending on what Type i use int/double/uin32_t).
When debugging, i noticed if i print Serial.println(_testIterationscounter) near the start, it will return 0 like expected, and the rest of the code will respond exactly as it should. WHY does this print statement alter the value?? AND it only works if i use this print very early in the code
Using NANO, there are pumps in the test environment (may introduce some electrical noise)
I saw a similar post where I2C was involved, there are I2C communications in the code but not where this variable is involved
THINGS I’VE TRIED: no success
-Removing every string in my code
-using Volatile integer type
The code where my variable is used in order how it runs
int _testIterationscounter = 0; // in setup Serial.println(_testIterationscounter); //Returns 0 at the start of the program. Removing this line causes next line to return 16809 Serial.println(_testIterationscounter + 1); //Later in the program. Returns 1 if the above statement is used. Returns 16809 if not _testIterationscounter += 1; _testIterationscounter += 1;
SOLUTION FOUND: The array declared right before my variable was being overwritten because my writing loop was 1 too fast.
This does not explain to me why Serial.printing the variable fixed my problem though.
Full code attached
code2.txt (11.8 KB)