Serial monitor Baud rate slows loop down?

Hi, there!

I'm new on arduino and I'm messing around with the most basic stuff... In this case, RGB leds.
I've found a pretty nice code for changing colors on RGB and I decided to make one from scratch to practice - it's different to do it by hart, than just copy/pasting and read it.

After a few hickups, it was as I wanted. Then, I turned on the Serial Monitor and it got so slow... I've realised that increasing the baud rate would speed up things.

My questions are:

1- Is this normal?
2- Is there something on my code that's slowing the program, other than the serial monitor?
3- Is there a way to keep the serial monitor on, while not afecting my program speed?

My code:

const int redPin=9;
const int greenPin=6;
const int bluePin=11;
const int tempo=10;
int redBRI;
int greenBRI;
int blueBRI;

void setup() {

  pinMode(redPin,OUTPUT);
  pinMode(greenPin,OUTPUT);
  pinMode(bluePin,OUTPUT);
  Serial.begin(9600);

}

void loop() {

  redBRI=255;
  greenBRI=0;
  blueBRI=0;

  for(int i=0; i<255; i+=1){
    redBRI-=1;
    greenBRI+=1;

    analogWrite(redPin,redBRI);
    analogWrite(greenPin,greenBRI);
    delay(tempo);

    Serial.print("R: ");Serial.print(redBRI);Serial.print("     ");Serial.print("G: ");Serial.print(greenBRI);Serial.print("     ");Serial.print("B: ");Serial.println(blueBRI);
  }

  redBRI=0;
  greenBRI=255;
  blueBRI=0;

  for(int i=0; i<255; i+=1){
    blueBRI+=1;
    greenBRI-=1;

    analogWrite(bluePin,blueBRI);
    analogWrite(greenPin,greenBRI);
    delay(tempo);

    Serial.print("R: ");Serial.print(redBRI);Serial.print("     ");Serial.print("G: ");Serial.print(greenBRI);Serial.print("     ");Serial.print("B: ");Serial.println(blueBRI);
  }

  redBRI=0;
  greenBRI=0;
  blueBRI=255;

  for(int i=0; i<255; i+=1){
    redBRI+=1;
    blueBRI-=1;

    analogWrite(redPin,redBRI);
    analogWrite(bluePin,blueBRI);
    delay(tempo);

    Serial.print("R: ");Serial.print(redBRI);Serial.print("     ");Serial.print("G: ");Serial.print(greenBRI);Serial.print("     ");Serial.print("B: ");Serial.println(blueBRI);
  }
 
}

Thanks in advance!

hello
If you have a lot of data, you can increase the data transmission speed to 115200, it will be good for you

When you print to Serial, the characters first go into a 64-byte buffer, then to the actual serial hardware. The rate at which the serial hardware can send the characters is determined by the baud rate, which at 9600 baud is 960 characters per second.

When you print something, if there is enough room in the buffer to hold what you are printing, the print function returns almost immediately. If there is not enough room in the buffer, the print function waits for space to become available, which slows down the code. Increasing the baud rate can reduce or eliminate this delay, depending on how much you are printing.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.