So I decided to make some more advance "talking" with my arduino and I found a piece of code which I transform into this:
char inData[20]; // Allocate some space for the string
char inChar; // Where to store the character read
byte index = 0; // Index into array; where to store the character
String reads;
int words = LOW;
void setup(){
Serial.begin(9600);
}
void loop()
{
while(Serial.available() > 0)
{
if(index < 19) // One less than the size of the array
{
inChar = Serial.read(); // Read a character
inData[index] = inChar; // Store it
index++; // Increment where to write next
inData[index] = '\0'; // Null terminate the string
}
words = HIGH;
}
for(int i=0; i<=index; i++)
{
reads += inData[i];
inData[i] = 0;
}
delay(10);
if(words)
{
Serial.println(reads);
words = LOW;
reads = "";
}
index = 0;
}
the thing is that in the serial monitor the words that I wrote and send to the arduino get all scattered on the serial monitor separated by spaces like this:
test test
test test t
est
something like that, how can I make it so that they come like:
test
test
test
...
???
You need to wait for the string to finish by detecting a string terminated like a CR.
Only when the string is all in should you attempt to print it out.
You are thinking that if one byte of the string has arrive then they all have. They have not.
So after looking at that code I made some litle modfications to my and here it is:
char inData[20]; // Allocate some space for the string
char inChar; // Where to store the character read
byte index = 0; // Index into array; where to store the character
String reads;
int words = LOW;
void setup(){
Serial.begin(9600);
}
void loop()
{
while(Serial.available() > 0)
{
if(index < 19) // One less than the size of the array
{
inChar = Serial.read(); // Read a character
inData[index] = inChar; // Store it
index++; // Increment where to write next
inData[index] = '\0'; // Null terminate the string
}
words = HIGH;
}
for(int i=0; i<=index && inChar=='\n'; i++)
{
reads += inData[i];
inData[i] = 0;
}
delay(10);
if(words && inChar=='\n')
{
Serial.println(reads);
words = LOW;
reads = "";
}
index = 0;
}
Now the first word is on the right place but the second gets again scattered.
How can I fix that?
You're reading the data into a string (a null terminated array of chars). Why are you then creating a String (a memory fragmenting object) out of what you read? Why not just print the char array?
Pseudo code:
if there is a character to read
read it
if the character is a newline
do something with the string
clear the buffer
reset the index
else
put the character into the buffer
increment the index
null terminate the buffer
end if
end if
The idea of using a terminating character is, that while there is at least one character available you read it and, if it is not the terminator and you have not read as many characters as the target array will hold, you put it in the array, advance the index, put in the '\0' and read the next character then do the same thing again. When you get the terminating character or have read the maximum number of characters then you print the string in the target array.
There is no need to move what you have read into the target array (a C style, null terminated string, lowercase s) to a String (uppercase S) before you print it.
pedroply:
I moved it into a String object because I want to use functions that only strings have like reads.indexOf() or things like that.
Don't.
There is a problem with the memory allocation and strings on the compiler used for the arduino. All those functions can be done by hand if you want.
Sorry for not being able to reply earlier but I have made some improvements :
This is because the error is yours not the compiler's.
I already suspected that
PaulS:
Might I suggest that you print stuff intelligently?
Serial.print("reads: [");
Serial.print(reads);
Serial.println("]");
will convey a LOT more information than
Serial.println(reads);
It just might even provide a clue or two.
I already suspected that the problem was in the string, more precisely at the point of converting the char into the string and I made this little modification:
if(index < 19 && inChar!='\n') // One less than the size of the array -/ added the "&& inChar!='\n'" so that it stops when it reages the last char.
{
inChar = Serial.read(); // Read a character
inData[index] = inChar; // Store it
index++; // Increment where to write next
inData[index] = '\0'; // Null terminate the string
}
But it wouldnt work becouse at the first time the code is running, the inChar is nothing so here is my last code:
char inData[50]; // Allocate some space for the string
char inChar = 'c'; // Where to store the character read
byte index = 0; // Index into array; where to store the character
String reads;
int words = LOW;
void setup(){
Serial.begin(9600);
}
void loop()
{
while(Serial.available() > 0)
{
if(index < 49 && inChar!='\n') // One less than the size of the array
{
inChar = Serial.read(); // Read a character
inData[index] = inChar; // Store it
index++; // Increment where to write next
//inData[index] = '\0'; // Null terminate the string
}
words = HIGH;
}
for(int i=0; i<=index && inChar=='\n' && inData[i]!='\n'; i++)
{
reads += inData[i];
inData[i] = 0;
}
delay(10);
if(words && inChar=='\n')
{
Serial.print("reads: [");
Serial.print(reads);
Serial.println("]");
words = LOW;
reads = "";
}
index = 0;
inChar = 'c'; // make it so that it is different from '\n'
}
It got some errors like you type "test" and it only returns "est" or "arduino" and it returns "duino" but it works.