Search code examples
c++arduinoesp32

Variable misteriously being set to 204


first time using SO. So, I'm casually coding and I start this global variable, rC and set it to 0. rC is a counter but when it's time for it to do it's own thing it goes wonkers. Here's a bit of the code and the output(I'm programming an Doit ESP32)

DEBUGPRINTLN3("Sampled xC");
DEBUGPRINTLN3(xC);
rC = 0;
Serial.println("rC before increment");
Serial.println(rC);
if(rC == 204) 
{
  Serial.println("WTF");
  rC = 0;
}
rC += 1;
dx = -dx;
xC = -xC;

The if statement was added while I looked for the source of the problem( this bug makes my whole program go crazy as that counter signals a bunch of other things, including it's own reset)

This is the Serial output:

14:48:23.017 -> Sampled xC
14:48:23.017 -> 299
14:48:23.017 -> rC before increment
14:48:23.017 -> 204
14:48:23.017 -> WTF
14:48:23.017 -> xC:
14:48:23.017 -> -299
14:48:23.017 -> HAS REVERSED
14:48:23.017 -> rC INCREMENTED 1
14:48:23.017 -> 1

The "fix" i used is super shitty and it won't work if i change some other variables, but it does the job for this case. Any idea what could be causing it?

In case anyone is wondering: there are only 3 other instances where rC is changed and it's another rC +=1 and a rC = 0(init and a reset later on). There is no while or for anywhere near rC+=1; Other than those, rC is only read, not edited.


Solution

  • The value in your variable was almost certainly put there by your compiler in an attempt to help you realize that it's uninitialized. 0xCC (204 in decimal) is commonly used for automatic / local / "stack" variables. I don't know how your code, that seems to show the variable being initialized, relates to the output that you claim was produced. I suggest you revise your question and ensure that you are posting a minimum, complete, and verifiable example of your problem.