Serial.print() lagging program.

Hi all,

I use Serial.println() a lot when I'm working on my projects because it helps me debug the software. When controlling motors, I am frequently polling potentiometers and writing pwms to motors as well as printing the current data to serial so I can see whats going on.

I've noticed I get a significant improvement in processing speed when I delete all of the serial printouts, which is annoying when I spend time calibrating everything only to notice speeds change when disabling all the serial printouts.

My question is, can I simple comment out the Serial.begin() command at initialisation? Or do I need to delete all of the Serial commands individually? It would be nice to be able to switch the serial on and off by just (un)commenting the command out at the beginning.

Cheers

Do you know the preprocessor commands?

You can use a DEBUG flag like this:

#define DEBUG


void setup() {
#ifdef DEBUG
  Serial.begin(115200);
#endif
}

void loop() {
#ifdef DEBUG
  Serial.println("Starting loop");
#endif

  // here is your motor control code and all other stuff
}

Using a higher baud rate may also help. The Arduino should work happily with 500,000 baud but you may need to use another Serial terminal such as puTTY.

Keep the individual debug messages very short.

There would be nothing to stop you having several #define directives to separate different groups of debug messages - such as

define DEBUG

define DEBUG2

define MOTORBUG

...R

Cheers for the replies.

Judging by your responses, as long as the Serial.begin() is commented out/not defined, then the print commands within the program will not get processed and won't affect processing speed?

I only ask because it will save me lots of time going through the whole program commenting out each individual print message.

Thanks again.

lacanau: Judging by your responses, as long as the Serial.begin() is commented out/not defined, then the print commands within the program will not get processed and won't affect processing speed?

That may or may not be true - I have not tried it myself.

...R

Judging by your responses, as long as the Serial.begin() is commented out/not defined, then the print commands within the program will not get processed and won't affect processing speed?

No! I have no clue how our answers gave you that impression. Did you check my example? The Serial.print() is also wrapped by the preprocessor commands. You have to go through your program once(!) to insert these commands and then you can select using the define if you want debugging output or not.

If the Serial.begin() is commented out all the Serial.print()s are called and need time eventually even more than with the Serial.begin() in place (because the baud rate is not correctly set, so the output may need much longer, depends on the string length to be printed).