I'm currently on project #14 of the arduinoArduino project book.
I'm trying to control a processing sketch on my laptop using my arduinoArduino.This 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 [b]HUGE[/b]huge lag between turning the potentiometer and seeing the background change color, and the values from the arduinoArduino/potentiometer change on the processing's serial monitor.
What i'veI've tried:
- Changing the speed of Serial communication
I've noticed that when iI 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 secsecond. However, when iI decrease the speed of Serial communication even further E, e.g . a value of 1, the delay increases again.
On the flipsideflip side, at the standard speed of 9600, the delay is huge,roughyl about roughly 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 and increasing it increase the time lag.Also? Also,is is there anyway iI can make it near instantaneous?