Serial data from Arduino to Processing does not update

I have an arduino test wether two wires touch, this works. The data is stored in an array for each pin and given either a 1 or 0 if the wire connects to another. The Serial.print is used to send the data, the serial monitor shows the data is correct. However in processing where this data is used it seems to not update. If I touch the wires before starting processing it knows they touch, but when I release the wires processing says they still touch. Same happens if they don’t touch at the start. This really frustrates me, because the data is sent correctly. And I have to deliver this project in 5 hours.

Arduino code:

void sendArrayData() {
  for (int i = 0; i < 12; i++) {
    Serial.print(",");
    Serial.print(i);
    Serial.print(",");
    if (i < 11) {
      Serial.print(partsConnected[i]);
    } else {
      Serial.println(partsConnected[i]);
    }
  }

}

Processing code:

void SerialEvent() {
  myString = myPort.readStringUntil('\n');

  if (myString != null) {
    myString = trim(myString);
    println(myString);
    sensors = int(split(myString, ','));
  }
}

Example data when two wires touch for the first time after the program has run for a little second already.

The data sent by Arduino example if first two wires connect: ,0,1,1,1,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,10,0,11,0

The data recieved by Processing: ,0,0,1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,10,0,11,0

Is it necessary to transmit the element number along with the 0 or 1? Arrays have automatic indices. Also not sure you need the leading comma. For whatever it’s worth, the following seems to work on my system.

Arduino:

int pins[12] = {0,0,0,1,0,0,0,0,0,0,0,0,};

void setup() {
 Serial.begin(9600);
}

void loop() {
  for (byte i = 0; i < 12; i = i + 1) {  
   Serial.print(pins[i]);
   if(i < 11) {
    Serial.print(","); // To avoid trailing comma
   }  
  }
  Serial.print("\n");
  delay(10);
}

Processing:

Serial port;
String dataStr;
String[] state = new String[12];

while (port.available() > 0) {
    dataStr = port.readStringUntil(10); // line feed == 10
    if (dataStr != null) {
      println(dataStr);
      state = split(dataStr, ',');
    }
  }

printArray(state);
1 Like

Hello,

This is what your code (with some added code) is doing:

String [] sensors0;
int [] sensors1;

void setup() 
  {
  String s = ",0,0,1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,10,0,11,0";
  //https://www.arduino.cc/reference/en/language/functions/communication/serial/println/  
  String ard = s + "\r\n";
  print(ard);
  
  //myString = myPort.readStringUntil('\n');
  String myString = ard;

  if (myString != null) 
    {
    myString = trim(myString);
    println(myString);
    sensors0 = (split(myString, ','));
    printArray(sensors0);
    sensors1 = int(split(myString, ','));
    printArray(sensors1);
    }
    
  println(int(""));
  }    
Console output:
,0,0,1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,10,0,11,0
,0,0,1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,10,0,11,0
[0] ""
[1] "0"
[2] "0"
[3] "1"
[4] "0"
[5] "2"
[6] "0"
[7] "3"
[8] "0"
[9] "4"
[10] "0"
[11] "5"
[12] "0"
[13] "6"
[14] "0"
[15] "7"
[16] "0"
[17] "8"
[18] "0"
[19] "9"
[20] "0"
[21] "10"
[22] "0"
[23] "11"
[24] "0"
[0] 0
[1] 0
[2] 0
[3] 1
[4] 0
[5] 2
[6] 0
[7] 3
[8] 0
[9] 4
[10] 0
[11] 5
[12] 0
[13] 6
[14] 0
[15] 7
[16] 0
[17] 8
[18] 0
[19] 9
[20] 0
[21] 10
[22] 0
[23] 11
[24] 0
0

:)

Was this an academic or homework assignment?

It was an academic assignment. I luckily found the issue before the deadline. I added a while loop in the SerialEvent() which was true as long as myPort.available() > 0. This worked, but caused a lot of framedrops, obviously. However it was good enough.

I just realised there were reactions on this post, I did not get any notifications. So thanks everyone for your help, I truly appreciate it!

1 Like