Serial makes Core crash. Hard fault error

Hi!

I don’t really know how to explain what is happening to my core because it makes no sense for me. I hope someone can figure out what could be happening :frowning:

I’ve been developing a Spark Core app and in order to debug the code I connected the core to my PC via Serial Port .

bool DEBUG_SERIAL = true;
if(DEBUG_SERIAL) Serial.print("\r\n\r\nERROR......\r\n\r\n");

And every time I use a Serial instruction I put this initial ‘if’ to disable all Serial communication at once.

In addition, I’ve put the following code in the setup() part to start Serial communication when I press a key.

 if(DEBUG_SERIAL) { 
   Serial.begin(115200);
   while (!Serial.available()) { 
     SPARK_WLAN_Loop();
   }
  }

Here comes the nonsense: when DEBUG_SERIAL is true , everything works PERFECT, but when is false the core crashes with hard fault (RGB RED flashing one time) and behaves very strangely (sometimes crashes at one point of the code, sometimes at another, with not a clear pattern).

I also tried to set DEBUG_SERIAL to true but change the while (!Serial.available()) with a delay of 4 seconds so that the code executes itself without me having to press a key, then perhaps works better but it also crashes at some point with no sense.

I know I am not giving much information but I have no idea what could it be.

Thank you guys.

@lia, I can’t quite tell what the problem is because I don’t have your entire code. However, most folks use conditional compiling for debugging since it stops the “extra” code from compiling when not enabled. It goes something like this:

#define DEBUG_SERIAL   1

#if (DEBUG_SERIAL == 1)
   Serial.begin(115200);
   while (!Serial.available()) { 
     SPARK_WLAN_Loop();
   }
#endif

You can try that or post your code so I can get the bigger picture of what you are doing. :smile: