When in debug mode, all if () statements, and int declarations are ignored. String declarations are executed.
If I use Serial.println() I can see these statements are executed.
I have created a demo program that uses conditional compilation to demonstrate the problem. Uncomment or comment out the line #define DEBUG 1 to see the problem.
Here is the code.
#include <Arduino.h>
#include <avr8-stub.h>
//*************************************
// uncomment this line to run in debug mode.
//#define DEBUG 1
//************************************
void setup()
{
#ifdef DEBUG
debug_init();
#else
Serial.begin(9600);
#endif
}
void loop()
{
// put your main code here, to run repeatedly:
#ifdef DEBUG
breakpoint();
#endif
String str = "This is a test";
int x;
x = 1;
if (str != "")
{
x = 1;
#ifndef DEBUG
Serial.println("Checked string if");
#endif
}
if (1 == 1 )
{
int x = 1;
#ifndef DEBUG
Serial.println("Checked X IF");
#endif
}
#ifndef DEBUG
delay(3000);
#endif
}
The code may be so simple that is optimizes these simple assignments and 1 == 1 away with no further thought, especially since without DEBUG the calls to actual work-doing functions like Serial.println() is gone. Add
debug_build_flags = -O0 -g -ggdb3
to the platformio.ini and retry. Does it behave differently?
The original program it came out of was much more complex but did the same thing. What I was trying to do here was simply demonstrate the issue. I guess I was successful.