Hi, I'm new on this forum and don't really know where this message belongs, but this folder seemed most appropriate.
Many of us use Serial.print() to debug our code and when everything is running fine we want to disable it. People have come up with various #ifdef DEBUG approaches as some mentioned here : http://arduino.cc/forum/index.php?topic=46900.0. But these all seem tedious and some even go to the extend of creating a library for this which adds more memory constraints. And they don't toggle the Serial.begin() you have in your setup().
I've come up with a much easier way of working. Add the following at the top of your sketch:
#include "Arduino.h" // replace this by "WProgram.h" when your IDE is older then 1.0 #define DEBUG true // flag to turn on/off debugging #define Serial if(DEBUG)Serial
Advantages over the #ifdef DEBUG approaches:
- Now you can keep using Serial.print() statements. So no code changes needed.
- It also turns on/off the Serial.begin() statement.
- Because Serial.begin() is also avoided this means that finally [u]no[/u] Serial code will be referenced and compiled any more. This is a big benefit for SRAM tight sketches. Avoiding a Serial.begin() as well saves 922 bytes in sketch size and 179 bytes SRAM usage!! :astonished:
(measurements done on Arduino0022 with the Blink sketch with default rx_buffer size (http://learn.adafruit.com/arduino-tips-tricks-and-techniques/arduino-hacks) and SRAM measured as described at http://www.designer2k2.at/home/arduino/49-arduino-ram-overflow.html.
Note that the include line on top is needed before the defines otherwise you get a compilation error.