hey, i think the following is an easy question for experienced arduino-users, but i'm failing for hours now and coulnt find any help.
i programmed a little app in processing which should communicate with the arduino via the serial port. in processing i simply write a string using
println("newrfid");
... which seems to work - i can see that also in the serial monitor in arduino. but i'm not able to read this specific part of the input-string but only can output the whole serial input.
i use the following code for creating a string out of the serial.read-input
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
and this is how my loop looks like
void loop(){
readrfid();
if (stringComplete) {
Serial.println(inputString);
if (inputString.substring(0)=="newrfid") {
soundInit();
}
//clear the string:
inputString = "";
stringComplete = false;
}
}
i'm searching for a function which allows me to search the whole input-string for a specific part... i tried indexOf and equals without success.
You might try the below comma , delimited method.
//zoomkat 9-9-10 simple delimited ',' string parse
//from serial port input (via serial monitor)
//and print result out serial port
String readString; // = String(100);
void setup() {
Serial.begin(9600);
}
void loop() {
while (Serial.available()) {
delay(10); //small delay to allow input buffer to fill
if (Serial.available() >0) {
char c = Serial.read(); //gets one byte from serial buffer
if (c == ',') {
break;
} //breaks out of capture loop to print readstring
readString += c;
} //makes the string readString
}
if (readString.length() >0) {
Serial.println(readString); //prints string to serial port out
if(readString.indexOf("woohoo") >=0) {
Serial.println("I found woohoo!");
}
readString=""; //clears variable for new input
}
}