I'm using two Arduinos to sent plain text strings to each other using newsoftserial and an RF transceiver.
Each string is perhaps 20-30 characters in length. How do I convert Serial.read()
into a string so I can do if x == "testing statements"
, etc.?
Unlimited string readed
Use string append operator on the serial.read(). It works better than string.concat()
After you are done saving the stream in a string(mystring, in this case), use SubString functions to extract what you are looking for.
Credit for this goes to magma. Great answer, but here it is using c++ style strings instead of c style strings. Some users may find that easier.
This would be way easier:
You can use
Serial.readString()
andSerial.readStringUntil()
to parse strings from Serial on the Arduino.You can also use
Serial.parseInt()
to read integer values from serial.The value to send over serial would be
my string\n5
and the result would bestr = "my string"
andx = 5
From Help with Serial.Read() getting string: