I'm trying to send an array back and forth between Processing and an Arduino UNO. But I think data is being lost along the way. I simply iterate over the array in Processing and send the Values separately. A new set of data is indicated by the character <
. On the Arduino the data gets added to a string and sent back for testing. When I print out the string in processing, some data changed to -1
.
The array contains {1,2,3,4,5,6,7,8}
The Output looks like this:
12345678
12345678
1234-1-1-1-1
12345678
12345-1-1-1
Is there something I'm missing in terms of implementation, or is this simply a hardware related problem (like slightly different clock speeds)? Is there any way to improve this?
Arduino Sketch:
int srr[8];
int content; // Data received from the serial port
void setup() {
Serial.begin(9600); // Start serial communication at 9600 bps
}
void loop() {
String str = "val:";
int test;
if (Serial.available()){ // If data is available to read,
content = Serial.read();
}
if (content == 60){
for(int i=0; i<=7; i++)
{
str+=Serial.read();
}
Serial.println(str);
}
}
Processing
Serial myPort; // Create object from Serial class
int arr[]={1,2,3,4,5,6,7,8}; //The array to send
String str;
void setup()
{
size(200,200); //make our canvas 200 x 200 pixels big
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
}
void draw() {
//write
myPort.write("<");
for (int i =0; i<=7;i++){
myPort.write(arr[i]);
}
//read
if ( myPort.available() > 0){ // If data is available,
str = myPort.readStringUntil('\n'); // read it and store it in val
println(str); //print it out in the console
}
}