Arduino debugging and tracing

arduino m0 samd21 debug trace

Pre-requisites

Combing #ifdef with a header file and ArduinoTrace library to setup toggling on and off for debugging Arduino code.

Code

Download code
#define DEBUG  // Comment to turn off debug statements
#define ARDUINOTRACE_SERIAL SerialUSB
#include <ArduinoTrace.h>
#include "DebugUtils.h"

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);

  #ifdef DEBUG
  SerialUSB.begin(9600);
  while (!SerialUSB) { }
  delay(100);
  #endif

  DEBUG_PRINT("Start blinking");
}

void loop() {
  DEBUG_PRINT("HIGH");
  digitalWrite(LED_BUILTIN, HIGH);
  delay(500);

  DEBUG_PRINT("LOW");
  digitalWrite(LED_BUILTIN, LOW);
  delay(500);

  DEBUG_TRACE();
}

References