Reducing lag between the arduino and a processing sketch on my computer

I'm currently on project #14 of the arduino project book.

I'm trying to control a processing sketch on my laptop using my arduino.This is accomplished by using a potentiometer to control the background of an image.

Arduino code:

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

void loop(){
  Serial.write(analogRead(A0)/4);
}

Processing:

//imports serial library
import processing.serial.*;
//setups the serial object
Serial myPort;
//creates an object for the image
PImage logo;
//variable to store background color
int bgcolor = 0;

void setup(){
  colorMode(HSB,255);
  logo = loadImage("http://arduino.cc/logo.png");
  size(logo.width,logo.height);
  println("Available serial ports");
  println(Serial.list());
  myPort = new Serial(this,Serial.list()[0],9600);
}
//equivalent of arduino's loop function
void draw(){
  if(myPort.available() > 0)
  {
    bgcolor = myPort.read();
    println(bgcolor);
  }

  background(bgcolor,255,255);
  image(logo,0,0);
}

Now, while the code works, and the background color changes as i turn the potentiometer, there is a HUGE lag between turning the potentiometer and seeing the background change color, and the values from the arduino/potentiometer change on the processing's serial monitor.

What i've tried:

  • Changing the speed of Serial communication

I've noticed that when i decrease the speed of Serial communication, e.g around 100, the delay between turning the potentiometer and seeing it change on my laptop decreases to about 1 sec. However, when i decrease the speed of Serial communication even further E.g a value of 1, the delay increases again.

On the flipside, at the standard speed of 9600, the delay is huge,roughyl about 5sec ++ before the changes in the potentiometer show up on the laptop/processing.

Why does decreasing the communication speed(up to a certain point) decrease the time lag,and increasing it increase the time lag.Also,is there anyway i can make it near instantaneous?

Unlike loop() on the Arduino, draw() is NOT called as fast as possible.

As a result, reading serial data in draw() is NOT to be done. You need to create a serialEvent() function, and read serial data in that function.

Decreasing the baud rate is going in the wrong direction.

Sending data continuously, as fast as possible, and reading it slowly (in draw) is what is causing the issue. Slow down the sends (sending only when the value changes, for instance or by adding a delay between sends) and/or picking up the pace on the reads is the real solution.

+1 for a wonderful answer Paul, i've included the serialEvent() function in my code and everything is running perfectly. How would i go about speeding up the speed of the reads? Is it as simple as using the serialEvent() function to speed up the reads, or is there an alternative method?

Set the baud rate to 115200 in arduino sketch in setup() and set the same baud rate in your pc to read incoming data from arduino. Use analog input functions to read potentiometer input in loop(). If you are using Uno, no need to use external voltage reference and you can read input values in the range 0-1023. After receiving input command to read data in serialEvent(), invoke your analog read function in loop() which writes the analog input value read to your pc. Now, you can process based on input control data from your potentiometer. Attempting to process image data or any data intensive process involves latency and may cause huge lags with data errors. Alternate method to Processing is to use our products Arduinotrix Studio (windows application) and Arduinotrix SDK (C++ library for Uno).

Excuse me for posing a -very related- question:

I don’t manage changing the baud rate (It starts showing strange characters on the monitor).

I adjoint the screen of what i’m doing: What’s wrong?

BAUD_RATE.bmp (1.38 MB)