Hi,
Im trying to send a word to my arduino perform an action based on that word.
is there any way I can use "Serial.read()" into a string?
Hi,
Im trying to send a word to my arduino perform an action based on that word.
is there any way I can use "Serial.read()" into a string?
Not directly. Personally, I'd avoid using String because of the variable memory usage and memory fragmentation that it causes. Since you know what words you are expecting, you can use a fixed length buffer to read the serial data into, taking care not to let the buffer overflow if you receive unrecognised data.
Simple code to capture characters sent from the serial monitor into a string.
// zoomkat 7-30-11 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later
String readString;
void setup() {
Serial.begin(9600);
Serial.println("serial test 0021"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
delay(1); //delay to allow byte to arrive in input buffer
char c = Serial.read();
readString += c;
}
if (readString.length() >0) {
Serial.println(readString);
readString="";
}
}