Hi all,
I'm tryng to receive into arduino multiple integers which I send via serial monitor. There are a lots of topics on how to do that, and so I modify the code I found in "arduino cookbook" and everything works well at 9600 bps.
The series I need to send could be for example the following, with any number separated by a space:
@ 23 45 67 122 33 2 12 34 21 54 3 234 6 54 22 54 6 2 5 7 0 0 0 0 0 34 2
I need to switch to 57600bps (at least) but the same code seems not correctly parse my integers at that speed; sometimes Arduino parse them correctly, sometimes not; If I send for example the same series of integers three times in a second it gave me a completely random resuslts!!!
Here's the code....what is wrong???
const int NUMBER_OF_FIELDS = 27;
int fieldIndex = 0;
int values[NUMBER_OF_FIELDS];
int ricevuto=0;
void setup(){
Serial.begin(57600);
Serial.setTimeout(10);
pinMode(5, OUTPUT);
}
void loop() {
if( Serial.available() && Serial.read()=='@' ) {
digitalWrite(5,HIGH);
for(fieldIndex = 0; fieldIndex<NUMBER_OF_FIELDS; fieldIndex ++)
{
values[fieldIndex] = Serial.parseInt(); // get a numeric value
}
fieldIndex=0;
ricevuto=1;
digitalWrite(5,LOW);
}
if (ricevuto==1){
Serial.print(values[0]);
Serial.print(" ");
Serial.print(values[1]);
Serial.print(" ");
Serial.print(values[2]);
Serial.print(" ");
Serial.print(values[3]);
Serial.print(" ");
Serial.print(values[4]);
Serial.print(" ");
Serial.print(values[5]);
Serial.print(" ");
Serial.print(values[6]);
Serial.print(" ");
Serial.print(values[7]);
Serial.print(" ");
Serial.print(values[8]);
Serial.print(" ");
Serial.print(values[9]);
Serial.print(" ");
Serial.print(values[10]);
Serial.print(" ");
Serial.print(values[11]);
Serial.print(" ");
Serial.print(values[12]);
Serial.print(" ");
Serial.print(values[13]);
Serial.print(" ");
Serial.print(values[14]);
Serial.print(" ");
Serial.print(values[15]);
Serial.print(" ");
Serial.print(values[16]);
Serial.print(" ");
Serial.print(values[17]);
Serial.print(" ");
Serial.print(values[18]);
Serial.print(" ");
Serial.print(values[19]);
Serial.print(" ");
Serial.print(values[20]);
Serial.print(" ");
Serial.print(values[21]);
Serial.print(" ");
Serial.print(values[22]);
Serial.print(" ");
Serial.print(values[23]);
Serial.print(" ");
Serial.print(values[24]);
Serial.print(" ");
Serial.print(values[25]);
Serial.print(" ");
Serial.print(values[26]);
Serial.println();
ricevuto=0;
}
delay(20);
}