SOLVED - Numbers change depending if I print them or not

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
-Swapping Arduino

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)

Print statements don’t change a value, but if you think that is happening, something else is probably wrong.

Since you are using Strings, all bets are off. Strings cause memory problems and program crashes with standard Arduinos. For starters, rewrite the program to remove all the Strings.

Always post ALL your code. What you call the “full code” can’t be all of it.

jremington: Print statements don't change a value, but if you think that is happening, something else is probably wrong.

Since you are using Strings, all bets are off. Strings cause memory problems and program crashes with standard Arduinos. For starters, rewrite the program to remove all the Strings.

Always post ALL your code. What you call the "full code" can't be all of it.

Yes i thought about strings after, i removed the String(integer) lines. Didn't change anything. Also i couldn't post the full code. Attached it as txt

Please post the code that has ALL the Strings removed, and describe the problems you are having with that code.

Toss all the versions with Strings in the trash bin.

jremington: Please post the code that has ALL the Strings removed, and describe the problems you are having with that code.

Toss all the versions with Strings in the trash bin.

I have removed every single print with a string and any string data, new code is attached. the value is still changing

mittemitte:
new code is attached.

It’s not

…R

Problems such as this are often caused by either exceeding the bounds of an array, or having a function return a pointer to a variable that is local to the function.

david_2018: Problems such as this are often caused by either exceeding the bounds of an array, or having a function return a pointer to a variable that is local to the function.

And the variable is declared right next to an array - data. Still plenty of String objects in the code too.

david_2018: Problems such as this are often caused by either exceeding the bounds of an array, or having a function return a pointer to a variable that is local to the function.

THANK YOU =)

I think the problem was that the array delcared right before it was being overwritten by one space. And i guess this was changing the variable that was delcared right after? It still doesn't explain to me why Serial.printing the variable early in the program was fixing the problem though.

mittemitte: It still doesn't explain to me why Serial.printing the variable early in the program was fixing the problem though.

When you step on memory that you don't own, the resultant behavior is undefined. You are seeing a manifestation of what "undefined" means.

I think the problem was that the array delcared right before it was being overwritten by one space

The location of the declaration statement is NOT the problem.

The problem is that you declared the array to be too small.