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

How are you determining that the wires actually touch?

If you are simply connecting them to two inputs and digitalRead()ing them inside loop() then you might get erroneous results because of the contact bouncing - these need to be debounce.

Or you could set up one pin as interrupt and the other as a 5V output so that whenever they touch an interrupt is generated.

All pins are first set to INPUT_PULLUP in setup then in loop() I check wether they touch with the first for loop, second loop is to reset the data sent to processing.

  for (int i = 0; i < 12; i++) {
    pinMode(i, OUTPUT);
    digitalWrite(i, LOW);
    for (int x = 0; x < 12; x++) {
      if (i != x) {
        if (digitalRead(x) == LOW) {
          partsConnected[x] = 1;
        }
      }
    }
    pinMode(i, INPUT_PULLUP);
  }
  sendArrayData();
  for (int i = 0; i < 12; i++) {
    partsConnected[i] = 0;
  }

Which board are you using?

Are you trying to find out is if any of the wires connected to pins 0 to 12 are touching each other, right?

A teensy, I'm indeed trying to find out if any of the 12 wires touch eachother.

I'm aware the method used to check wether the wires touch is a bit rough, however the results are good enough. The Serial monitor shows the data is okay, it is however not correctly transferred to processing

The first 4 numbers seem to have a mismatch. By any chance are the pins for those also the Tx/Rx pins...?

Yes pin 0, 1, 7, 8, 9, 10 are Tx/Rx pins, I'm not sure about pin 5.

Does this affect the results?

Yes it will affect the results - when Tx touches Rx it sets up a loopback...any data on Tx will be reflected in Rx.

Do you think this may be the cause for the problems with the send data? Because in the Serial monitor the data looks good and updates well.

Why does this board have so many serial comm pins? Does it support multiple serial ports?

That said, any data you write to the Tx pin (wire) will get caught by the Rx pin (wire) if it happens to touch the Tx one...

This is the board advised to me by school.
I will change the pins and check the result

Yes...use the pins 14 to 23 and see. Do not use the Tx/Rx pins for anything other than serial comm...

If nothing works, just try leaving a small delay (few milliseconds) between the lines you are trying to print.

Good Luck!

When switching pins, the program still works as before. The right pins are set to be connected by Arduino but not in processing unless they touched from the start (when the program was started).

What would leaving a small delay do exactly?

I'm not familiar with Processing but the above line looks suspicious (e.g. if it was C/C++). Does the situation improve when you remove it?

Finally figured it out!!!

I needed a while loop in the processing code to check the port in SerialEvent(). I'm not really sure why, but I think it has something to do with the fact that it now always tries to read the data as long as myPort.available() is more then 0.

Thanks everyone for your help!!

Sensors is an array with no length yet, the string is split wherever there is an ','. I know the data send are only numbers so I can directly say int() to make the split strings into int

1 Like

Thanks, learned something.