String in arduino

Hi everyone, I'm having some troubles with the "String" function in my Arduino program. The purpose of the program is to process an information sent by the computer (a number between 0 and 255) to control a transistor with "PWM". The problem is : if I use the fonction "char variable = Serial.Read();" The Arduino send the numbers separatly. For example, if I send "255" in the Serial terminal, the Arduino return "2" then "5" then "5" instead of "255". The solution was to use the "String" function, however this function need 1 second to process the information. The computer is sending an information every 200 milliseconds so the Arduino is not as quick as the information is received. The program : void setup() { Serial.begin(9600); }

void loop() { if (Serial.available() > 0) { String test = Serial.readString(); Serial.println(test); } }

For your information, I'm using an Arduino Uno Thanks for your help, Yoann56

Serial.readString has too long a timeout by default

This is an all-too-common question here. A few simple searches should throw up some solutions.

Please remember to use code tags when posting code.

Hi, thanks for your reply, I will have look. :)

Get rid of the String objects and use C strings (note lowercase 's') instead. Your code compiled to 2976 bytes while this code:

void setup() { Serial.begin(9600); }

void loop() {
  char input[7];
  int charsRead;
  
  if (Serial.available() > 0) {
    charsRead = Serial.readBytesUntil('\n', input, sizeof(input) - 1);  // Save room for NULL
    input[charsRead] = NULL;
    Serial.println(input);  
  }
}

compiles to 1654 bytes.