I am newbie in Arduino and writing a program where I want to control the flow by using Serial monitor input (PI controller). I've read that using String() although easier it is slower than using char. But to use char I would need more lines of code to convert the input into a string for comparison and more knowledge to make the program robust. For the control I will enter small strings like Ramp, Increase, Decrease, Lock, Stop and any other inputs will not be used. Generally, there are not going to be much inputs. The strings are basically going to be put into if-statements leading to functions.
if(Serial.available()){
string = Serial.readString()
or some function to read out all the bytes of the string and return a char containing it;
if(string="Ramp") {
do_this();
}
else_if(string="Increase"){
do_that()
}
//If nothing matches, continue with code.
}
Since I need to finish this project soon, I am wondering if it's going to increase the loop time significantly if I use String() or I shouldn't bother learning and using char right now (at a later stage, for sure).
2 Answers 2
Yes, String
has a bit more overhead than plain c-strings (aka char
arrays), though the additional execution time will be way too small for you to notice (especially when you type the commands in by hand).
It will be no problem to use String
in your sketch, though you should do it right and use String.reserve()
to allocate enough memory space in the String
object to hold the longest string, that you have. Also don't just add String
s together. That way you can avoid memory fragmentation (effectively making swiss cheese out of your RAM) and still use String
.
Though when you are not in a hurry anymore, learning how to use c-strings will be worthwhile.
-
And what happens if I use char or byte and send command longer then expected? The rest of the bytes stay in the serial buffer and then on further read I get wrong command or it empties out the buffer once it fills?Ognyan Petkov– Ognyan Petkov2022年08月18日 06:20:21 +00:00Commented Aug 18, 2022 at 6:20
-
What happens then depends on your programming. A common way is to end the string and throw away the rest of the incoming data for this message. That way you can recover from that. Though you have a limited list of commands, so there is a maximal length of commands for youchrisl– chrisl2022年08月18日 07:28:38 +00:00Commented Aug 18, 2022 at 7:28
Using Serial.readString()
will make your program very slow. This is
because readString()
does not know when to stop reading. When the
serial input buffer gets empty, it will wait just in case more
characters may come later. Only when it times out does it return the
string that it could read. The default timeout is one second, so your
program will be unresponsive for a full second after receiving a command
and before acting on it.
You can shorten the timeout using Serial.setTimeout()
, but this may
make your program harder to test interactively by typing commands on a
terminal emulator.
I recommend you add a terminating character (say, '\n'
) to each of
your messages, then replace Serial.readString()
with
Serial.readStringUntil()
. This method will return immediately when it
finds the terminating character. Note that this character will not
appear on the returned string object. readStringUntil()
also obeys a
timeout, but only when it cannot find the terminating character.
Or you can learn the clean way to read Serial on the Arduino. Yes, it is a bit harder, but not a horrible thing to do.
Explore related questions
See similar questions with these tags.
Ramp, Increase, Decrease, Lock, Stop
can be represented by single byte commandsr, i, d, l, s