I have in Processing an array with 11 elements. I want to send it via Serial connection to a connected Arduino. But I haven't figured out yet the best way to do it.
It is important, that the array is in the same order when it is recieved.
If you want to write multiple values, and ensure that all the values are received in order, you'll need some start of packet marker and end of packet marker.
You can send the data as numeric data, and receive it the same way, since the values to send are byte-sized ("<",0,14,75,...,99,">"), or you can send it as string data ("<0 14 75 ... 99>").
Depends on whether you are sending as numbers or as a string. If you send as numbers, read the values one byte at a time, in the order sent, when the bytes arrive, following the <. Stop when you get a >.
If you are sending as a string, collect the whole string, starting when you get a <, ending when you get a >. I've posted lots of times how to do that.
Then, use strtok with the string as the first argument, to get the first token. Use atoi to convert the token to a number. Then, in a while loop (while(token){}), call strtok with NULL as the first argument (to keep parsing the same string), and use atoi on each token.
This is basically what you tell me to do. But what I don't understand, how to get now the numbers from my String rohre[] into an array newNumbers[]; the working of the while loop is what I don't understand.
In that example, replace str with your string (rohre).
Where printf is printing a token, you want to use atoi to convert the token to an integer, and store it in the next position in your array, and increment a counter.
int arrIndex = 0;
int arrayOfNums[11];
char *token = strtok (rohre," ");
while (token != NULL)
{
int aNum = atoi(token);
if(arrIndex <= 10)
{
arrayOfNums[arrIndex] = aNum;
arrIndex++;
}
token = strtok (NULL, " ");
}
The code on both ends LOOKS right. We'd need to know what the Arduino actually received, though, to determine whether the failure is in the transmission of the data, the receipt of the data, or the parsing and storing of the data.
Add some Serial.print() and Serial.println() statements to echo rohre, token, aNum, and arrIndex.
Add code in the Processing sketch to respond to, and print, serial data arriving. There is at least one example in the Processing application that shows how to receive and print data.
[edit]I just noticed that your array indices in the Processing application run from 1 to 11. They should run from 0 to 10.[/edit]
[edit]There is nothing in the Arduino code you posted to show that the incoming data is being used. How do you know that the data is being sent/received/parsed/stored incorrectly?[/edit]