History:
Version 1.1
- fix: reduced the debug array from 1000 to 10. Highly recommend you download and use this version instead... or nothing works. Oops.
- fix: renamed a couple of global variables to hopefully remove any sketch clashes
Version 1.0
- example sketch, library, keywords and empty library released
- version, addvar, addpin, dump and debug commands implemented
Example:
The current example program:
#include "Arduino.h"
#include "dbg.h"
//#include "nodbg.h"
#define pin13 13
int charCount = 0;
String inputBuffer;
byte dPin13; // digital pin 13 (the onboard lED)
void setup() {
Serial.begin(9600);
Serial.println("Ready");
pinMode(pin13, INPUT);
DBG_VERSION; // output the version
DBG_ADDVAR("number of characters received", charCount) // add a variable
DBG_ADDVAR(F("serial input buffer"), inputBuffer) // add another variable
DBG_ADDPIN(F("onboard LED"), dPin13) // add a pin
DBG_DUMP; // debug all currently added variables // output all debug variables
}
void loop() {
dPin13 = digitalRead(pin13);
DBG_DEBUG(dPin13); // debug just this variable
}
void serialEvent() {
while (Serial.available()) {
charCount++;
char inChar = Serial.read();
inputBuffer += inChar;
switch (inChar) {
case 'z' : // let's clear the buffer
Serial.println("\nClearing input buffer");
inputBuffer = "";
break;
default : Serial.print(inChar);
}
}
DBG_DEBUG(charCount)
DBG_DEBUG(inputBuffer)
}
The output from the example program, as displayed in Tellurium:
Raw output, showing debug library version:
Parsed variable output, showing variables and pins grouped, and the effect of sending "hello":
Files:
There are 4 files associated with this library:
- dbg.h - the debug library (no .cpp file required) Download: https://dl.dropboxusercontent.com/u/85621331/dbg.h
- nodbg.h - the debug library include you use when you want to remove debugging but not the debugging statements Download: https://dl.dropboxusercontent.com/u/85621331/nodbg.h
- keywords.txt - for use with Arduino IDE Download: https://dl.dropboxusercontent.com/u/85621331/keywords.txt
- debug.ino - the example sketch Download: https://dl.dropboxusercontent.com/u/85621331/debug.ino
Installation:
The dbg and nodbg header files should be placed into corresponding dbg and nodbg directories in your Arduino libraries folder. Place the keywords.txt file in the dbg and nodbg directories also. I have colour coded dbg commands as KEYWORD1 so they stand out a bit.
Commands:
All the commands have been created as #define macros in dbg.h. These same macros have been defined as "empty" macros in nodbg.h, allowing you to switch between the 2 include files and remove all debugging actions quickly and easily, whilst leaving the debugging commands in your code.
Commands that have been (fairly) reliably implemented are:
- DBG_VERSION: print the version of the dbg library to the serial port
- DBG_ADDVAR(descriptive name, variable): associate a descriptive name with a variable and add it to the debug array
- DBG_ADDPIN(descriptive name, variable): associate a descriptive name with a variable, flag it as a pin and add it to the debug array
- DBG_DUMP: send all debug variables to the serial port
- DBG_DEBUG(variable): send the variable to the serial port
NB: If you go poking around, you will notice there are a couple of other commands present in dbg.h, but their correct operation is not guaranteed.
Roadmap: (in random order)
- fix: range checking on debug array
- fix: store debug variables in a linked list instead of an array
- fix: handle [local to function] variable debugging gracefully
- add: sketch flow - sketch actions, function entry & exit, function return values
Background
In my development, I have found debugging incredibly useful in working out what is going on. This is especially true when something goes wrong.
With the Arduino, the serial port is an obvious communication conduit, allowing you to send data back to a serial monitor for viewing / debugging purposes.
It wasn't long before I got sick of doing things like
Serial.print("some variable name: ");
Serial.println(someVariable);
and sought to put together a library that would make the debugging process a little easier.
I had also found the Arduino serial monitor slightly less than optimal for debugging purposes, what with it clearing my sent text, resetting the board only at start up and reluctance to appear when I pressed Ctrl+Shift+M...
Accumulating gross amounts of text in a serial monitor was not as helpful as expected either.
So the goal was to develop a serial monitor that would cooperate with some debugging sent from an Arduino debugging library. The serial monitor has been released. More information here: [Update] Tellurium: serial monitor - Libraries - Arduino Forum
The other thing that is a challenge is the way Arduino sketches work, you are always looping around, so careful placement of debug statements is required to prevent a torrent form the serial port flooding the serial monitor. To this end, the debug library tracks variable values, and only sends them to the serial port when they change. You still need to be mindful of debug placement, but some assistance is provided via this mechanism.
dbg.zip (3.43 KB)