Hey,
this is my code to write a string into the Serial. The Arduino should read the string and print it into the Serial, too. It works, but I have line breaks in longer Strings (because of the short delay time). How I can improve this?
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
String string = "";
char letter = 0;
int wait = 0;
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available() > 0) {
wait = 0;
letter = Serial.read();
if (letter != '\n') {
// a character of the string was received
string += letter;
}
else {
// end of string
}
} else {
delay(100);
if(string != ""){
Serial.println(string);
string = "";
}
}
}
Delta_G:
Instead of printing your String whenever it's not equal to an empty string, wait until you've got the whole transmission and only then print it.
So what I have to change? Sry, my english is not the best and I don't exactly understand, what I should change in the code?
You might try the below code which uses a comma "," as the end of string marker.
//zoomkat 3-5-12 simple delimited ',' string
//from serial port input (via serial monitor)
//and print result out serial port
String readString;
void setup() {
Serial.begin(9600);
Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}
void loop() {
//expect a string like wer,qwe rty,123 456,hyre kjhg,
//or like hello world,who are you?,bye!,
if (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
if (c == ',') {
//do stuff
Serial.println(readString); //prints string to serial port out
readString=""; //clears variable for new input
}
else {
readString += c; //makes the string readString
}
}
}