DigitalInput Latency

Hi. I am currently working with arduino and Max/Msp communication.
I write simple analog and digital input code on Arduino, and send serial data to Max/Msp via Processing OSC protocol.

But, about 500ms latency happens in every digital inputs.

I use an accelerometer sensor and IR sensor,or four buttons.

Here is the code.
If somebody can solve this problem, could you answer my question :’(?

Here is the code:

Arduino:

int a = {0,0,0,0,0};
int d = {0,0,0,0};

void setup(){
Serial.begin(19200);
pinMode(0,OUTPUT);
pinMode(1,OUTPUT);
pinMode(2,INPUT);
pinMode(3,INPUT);
pinMode(4,INPUT);
pinMode(5,INPUT);
//pinMode(7,INPUT);

}

void loop(){
for(int k = 0; k < 4;k++){
a[k] = analogRead(k);
Serial.print(a[k],DEC);
Serial.print(",");
}

for(int k = 0; k< 4;k++){
d[k] = digitalRead(k+2);
if(k != 3){
Serial.print(d[k],DEC);
Serial.print(",");
}else{
Serial.println(d[k],DEC);
}
}

//d0 = digitalRead(0);

}

Processing:

import processing.serial.;
import oscP5.
;
import netP5.*;

int linefeed = 10;
Serial myPort;
OscP5 oscP5;
NetAddress myRemoteLocation;

void setup() {
println(Serial.list());
frameRate(60);
/* start oscP5, listening for incoming messages at port 12000 */
oscP5 = new OscP5(this,12000);
myRemoteLocation = new NetAddress(“127.0.0.1”,12000);

myPort = new Serial(this, Serial.list()[0],19200);

myPort.bufferUntil(linefeed);
}

void draw(){
}

void serialEvent(Serial myPort){
OscMessage myMessage = new OscMessage("/test");

String myString =
myPort.readStringUntil(linefeed);

if(myString != null) {
myString = trim(myString);

int sensors = int(split(myString, ‘,’));

for (int sensorNum = 0;
sensorNum < sensors.length;
sensorNum++) {
myMessage.add(sensors[sensorNum]); /* add an int to the osc message */
oscP5.send(myMessage, myRemoteLocation);
//print(“Serial” + sensorNum + sensors[sensorNum] + “\t”);
}
println();
}
}

Half-a-second sounds a bit high, but all that serial stuff can't help. Should be more like 7 or 8mS. [EDIT] Oops - should be 11 or 12mS Does the latency decrease if you increase the baud rate?

I tried to change my baud rate, but it can't help. I tested 9600 and 19200, but same latency occured. Hmmmmmmm.....

Nothing I can see in that code would give you that much latency. Are you using windose, that can introduce a large chunk due to the way the serial port is handled.