Go Down

Topic: Communication lag using Arduino Mega 2560 (Read 1 time) previous topic - next topic


That b? That stands for beta.

Try version 1.5.1 (the last production version).


Great observation, but unfortunately that didn't help either.  Any chance it could have to do with switching between boards and the port name changing?  I know that's a long shot but I'm at my wit's end, unless it's just a shortcoming of the 2560 boards, but I would find that hard to believe.

I'm putting my current code below this message again.  Thanks so much for your help!

Code: [Select]

// Arduino
void setup() {
char mybuffer[4];
void loop() {
  if (Serial.available() ) {
    int throw_away1 = Serial.readBytesUntil('\n', mybuffer, 4);

// Processing code
import processing.serial.*;
Serial port;
void setup() {
  port = new Serial(this, Serial.list()[0], 9600);
void draw() {
  delay(100); // works when this is 1000 instead
  String inString = port.readStringUntil('\n');


Feb 15, 2013, 12:05 am Last Edit: Feb 15, 2013, 12:15 am by fcady Reason: 1
To give more details
1) the cutoff of how much lag is required for the Mega to work is between 830 and 840 millis
2) the cutoff is about the same for each Mega
3) when it works near the cutoff, two nulls get printed to the screen and then it's numbers.  when I'm far away from the cutoff, a single null gets printed.
4) following up on some other posts I saw, I tried running the program with the arduino editor closed.  It didn't do anything.


I was able to fix it!  I google around and found this: http://arduino.cc/forum/index.php?topic=50484.0

It sounds like the issue is that the Arduino resets itself very time I start the Processing application, but things get hosed if I start sending data to the Mega too quickly.  The workaround is to put a delay into Processing's setup function:
  frameCount = 1; // required for the delay to work, since frameCount=0 during setup and the delay function requires frameCount >= 1

Now it seems to be working great.  Thank you for your help!


Alternatively, you could have the Arduino send a character when it is ready to receive data. Then, the Processing app would simply wait until is saw data from the Arduino before it started sending.

Interesting find on the frameCount thing. I would have expected delay() to work at all times, not just after the first invocation of draw().

Go Up