Tuesday, April 12, 2011

Arduino Debug

One of the great benefits of an Arduino is that of the serial debugger. It is very easy to print out items to the serial console. However, if you go overboard with printing to the serial console (such as printing execution step info in a loop or constant polling of an input), you may incur problems when trying to upload a new sketch (especially on Unix) due to the serial interface being overrun. There are multiple fixes, including some patches in the bootloader (which didn't work for me), racing a sketch upload during a board reset, etc, but I found a simple way to enable or disable the printing easily.

First of all, instead of using a #define DEBUG [0|1], I instead declare a DEBUGPIN variable or #define then create a simple function:

int DEBUGPIN=2;
int DEBUG(){
return(digitalRead(DEBUGPIN));
}

Now, where ever I want to display debug info, I do something like:
if(DEBUG()){
Serial.print("potSpeedVal: ");
Serial.println(potSpeedVal, DEC);
}

Now, if I want to output to the serial console, I simply put on a jumper on the defined DEBUGPIN. If I no longer need the debug output, I remove the jumper. This way, the sketch doesn't need to continuously try to output serial console data when it isn't needed.

0 comments: