Lag between arduino and processing serial libraries

Hey all,

I’m working on some basic arduino code with and without Processing for a workshop demonstrating the use of the serial library as well as arduino firmata. I’m running into an issue with the serial library where the library just takes a very long time to respond (5-10 seconds). It takes in and prints out the values I sent from processing but just waaaayy later. I thought It was my code so I switched out and used one of the examples from Processing to check and the same problem persists. I used this setup 3 months ago to run my senior project from college and it worked just fine. So I’m not sure what is different except for the fact that I am on OSX 10.7 instead of 10.6. The Arduino Library for processing, which also depends on the same serial library, works fine however.

Can anyone shed any light on this matter?

here’s some sample code just from the processing example SimpleWrite:

import processing.serial.*;

Serial myPort;  // Create object from Serial class
int val;        // Data received from the serial port

void setup() 
{
  size(200, 200);
  // I know that the first port in the serial list on my mac
  // is always my  FTDI adaptor, so I open Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
}

void draw() {
  background(255);
  if (mouseOverRect() == true) {  // If mouse is over square,
    fill(204);                    // change color and
    myPort.write('H');              // send an H to indicate mouse is over square
  } 
  else {                        // If mouse is not over square,
    fill(0);                      // change color and
    myPort.write('L');              // send an L otherwise
  }
  rect(50, 50, 100, 100);         // Draw a square
}

boolean mouseOverRect() { // Test if mouse is over square
  return ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 50) && (mouseY <= 150));
}


/*
  // Wiring/Arduino code:
 // Read data from the serial and turn ON or OFF a light depending on the value
 
 char val; // Data received from the serial port
 int ledPin = 4; // Set the pin to digital I/O 4
 
 void setup() {
 pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
 Serial.begin(9600); // Start serial communication at 9600 bps
 }
 
 void loop() {
 if (Serial.available()) { // If data is available to read,
 val = Serial.read(); // read it and store it in val
 }
 if (val == 'H') { // If H was received
 digitalWrite(ledPin, HIGH); // turn the LED on
 } else {
 digitalWrite(ledPin, LOW); // Otherwise turn it OFF
 }
 delay(100); // Wait 100 milliseconds for next reading
 }
 
 */

looks like you are sending serial out every loop of Draw() which would fill the serial buffer. You are only reading one byte every 100ms with the arduino so it would take a while to get to the new values. Try modifying the processing sketch to only send a byte when it senses a change of state, like

if (mouseOverRect() != OldMouseOverRect

or something.

10 events a second doesn’t seem THAT much so maybe it’s something else. A lot of serial I/O libraries sometimes buffer output and send it when some threshold is reached like size of buffer or time limit. Maybe that’s whats going on here. For example, some libraries wait until they see an end of line character to flush the buffer.