Hi,
I have a problem and don't know how to solve it. I want to read a string from a terminal this is easy but I want the Arduino to understand the specific characters of it an example:
I want to Adress pin 11 and set the brightness of an LED to 100 and I want the Arduino to understand this statement:
PIN11B100
this is the code I use for reading the Terminal :
while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
input += c; //makes the string readString
delay(2); //slow looping to allow buffer to fill with next character
}
if (input.length() >0) {
Serial.println(input); //so you can see the captured string
n = input.toInt();
}
Forget about the String class, as it is wasteful in most cases. I'm assuming that input[] is a String. Change that to a char array.
int charsRead;
char input[10];
while (Serial.available()) {
charsRead = Serial.readBytesUntil('\n', input, sizeof(input) - 1); //gets bytes until Enter pressed
input[charsRead] = NULL; // This makes it a string
Serial.print(input); // Show it...
}
The readBytesUntil() function collects characters into input[] until it sees the newline character or 9 bytes have been read. You must do that because C strings are terminated with the NULL character, which is the purpose of the second statement in the loop. After the code above, use strtok() to break out the parts of the string, or just do it one character at a time by whatever means you best understand.
Please read Nick Gammon's post at the top of the Forum on the proper way to post code using code tags.