how to execute commands from serial port faster

hey all, I've made a program in QT which changes LEDs brightness connected to arduino uno .
when I change values so fast arduino can't handle that and changes values randomly so I fixed that by making it parse the serial input from latest input it gets so if it got "gre122 gre033 blu111" it will trim the text and then execute "blu111" and that fixed the problem, but it still kind of slow so the changes doesn't applied instantly. I've tried to use higher baud rate but it didn't help
sketch source : GitHub - Lehkeda/Arduino_control_LEDs_brightness_from_pc
serial control tool : GitHub - Lehkeda/Serial_control_tool

you probably require a protocol to control the flow of information, e.g.

  1. the transmitter sends data and then waits for an acknowledgement
  2. the receiver receives the data, processes it and when ready for the next data sends an acknowledgement
    in the simplest case the acknowledgement could be a single character
    this ensures that the transmiter and receiver operate at maximum rate without the transmitter overrunning the receiver
    a more complex protocol could fill a ring buffer, when getting full the receiver sends a command to the transmitter to suspend transmission, as it empties the receiver tells the transmitter to resume transmission

the command "gre122 gre033 blu111" is very long, any reason why?
remember even at 115200baud you can only send approximatley 11000 characters/second

the rate at which the LCD can receive commands may be limited - how is it interfaced?
also LCDs tend to be very slow devices compared to the speed processors run at

Post your code here so it makes it easy for us to help you.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

You're calling Serial.readString(), which reads characters from the serial port one at a time until there are no more characters for (by default) 1 second. That means your application can send "gre122" but your Arduino sketch won't process that command until one second later.

I'd follow Robin2's advice and receive each byte of data yourself until you have a complete command.

horace:
the command "gre122 gre033 blu111" is very long, any reason why?
remember even at 115200baud you can only send approximatley 11000 characters/second

the app on my pc tells the the board to change the brightness of the specific LED so it sends the name/color of the led and its brightness value as following "gre145" = change brightness of green LED to 145. on the app there's a slider for each LED when you change its value it sends the new value to the board as I mentioned earlier. hence if I changed the values so fast the app sends all the new value at once like "gre111 blu154 red099" so the board can't handle that.

here's a screenshot of the app

Robin2:
Post your code here so it makes it easy for us to help you.

that's the part responsible for parsing serial input

void loop() { // run over and over
 String raw_input = Serial.readString(); // get serial input
 raw_input.trim(); // clear all spaces,new lines from raw serial input 
 int str_len = raw_input.length(); // get lenth of raw input 

  // remove all unnecessary text from raw input
  // data length must be 6 charaters length
  raw_input.remove(0,(str_len-6)); 
 
 /* contains color name + value
 *  I only use 3 letters to represent color name 
 *  Ex. red=red ,gre=green ,blu=blue
 *  and use number its max value is 255 
 *  Ex. gre123 -> color name is green and its value is 123
 */
 String all_color=raw_input;
 
 String color = all_color; // contains color name only 
 color.remove(3);
 if(str_len != 0 ){
   all_color.remove(0,3); // keep only color value 
   if(!(all_color.toInt() > 255 )){
      if(color.equals("red")){
           turnRed(all_color.toInt());
     }else if(color.equals("blu")){
           turnBlue(all_color.toInt());
     }else if(color.equals("gre")){
           turnGreen(all_color.toInt());
     }
   }
}

christop:
You're calling Serial.readString(), which reads characters from the serial port one at a time until there are no more characters for (by default) 1 second. That means your application can send "gre122" but your Arduino sketch won't process that command until one second later.

can I change that 1 second time ?

you can change the timeout with

horace:
you can change the timeout with
Serial.setTimeout() - Arduino Reference

Thanks for help. now the board respond faster to changes.

I have one more question. how can I clean serial input after each command is sent to the board, should I use make my app do flush from it side or do flush from the board ?

The Arduino Serial.flush() command is for use when the Arduino is sending data - it causes it to wait until all the data has been sent.

Have you studied the examples in my link in Reply #2? With them there should be no need for any flushing.

...R