Go Down

Topic: Slow rendering in Processing (Read 314 times) previous topic - next topic

giulio_

Hi everyone, I'm approaching for the first time at the Arduino/Processing communication, with the project n.15 of the Arduino Project Book (for those who know that), basically it reads an input from a potentiometer, and outputs its value as the background color of the Processing sketch window.

I have a very simple question, as I run the sketch it takes a very long time before the colour changes, several seconds, maybe minutes, after I finished rotating the potentiometer, it is normal? Should I change something?

I tried to change the bauds of the serial communication and it's the same.
Thank you all





Here's the code

PROCESSING
Code: [Select]

import processing.serial.*;
Serial myPort;

PImage logo;

int bgcolor = 0;

void setup() {
  colorMode(HSB, 255);

  logo = loadImage("http://arduino.cc/logo.png");
  size(250, 200);

  println("Available serial ports: ");
  println(Serial.list());

  myPort = new Serial(this, Serial.list()[1], 9600);
}

void draw() {
  if (myPort.available() > 0) {
    bgcolor = myPort.read();
    println(bgcolor);
  }
  
  background(bgcolor, 255, 255);
  imageMode(CENTER);
  image(logo, width/2, height/2);
}









ARDUINO IDE
Code: [Select]

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

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

PaulS

Code: [Select]
  myPort = new Serial(this, Serial.list()[1], 9600);
You can speed things up by a factor of 12, by using a faster baud rate.

Unlike the Arduino's loop() function, the draw() function in Processing is NOT called over and over, as fast as possible.

There is a frame rate setting that defines how often draw() gets called. If you send new data only when there was a change, you would be sending, and Processing would be reading and discarding, far less data.

You should NOT do serial I/O in draw(). That is what serialEvent() is for.

You may have problems redrawing the image on every pass through draw().
The art of getting good answers lies in asking good questions.

giulio_

Thank you, are you referring to the frameRate() function? Should I increase it? I tried but without results.

How can I send new data only when there was a change?

PaulS

Quote
How can I send new data only when there was a change?
Code: [Select]
if(thisValue !=thatValue)
{
   send(thisValue);
   thatValue = thisValue;
}


Did you read what I said about not doing serial I/O in draw()? Did you change your program?
The art of getting good answers lies in asking good questions.

giulio_

Honestly, I don't know how to do that. I still cannot manage Processing functions very well.
Should I use the serialEvent() function? Or write something else in draw()?

At this moment I followed the manual's guide.

Did you read what I said about not doing serial I/O in draw()? Did you change your program?

Whandall

This reads the new color index not as a byte, but as a single line containing the number printed.

Code: [Select]

import processing.serial.*;
Serial myPort;

PImage logo;

int bgcolor = 0;

void setup() {
  colorMode(HSB, 255);

  logo = loadImage("http://arduino.cc/logo.png");
  size(250, 200);

  println("Available serial ports: ");
  println(Serial.list());

  myPort = new Serial(this, Serial.list()[1], 9600);
  myPort.bufferUntil('\n');
}

void draw() {
  background(bgcolor, 255, 255);
  imageMode(CENTER);
  image(logo, width/2, height/2);
}

void serialEvent(Serial myP) {
  String inLine = myP.readString();
  bgcolor = Integer.parseInt(inLine.substring(0, inLine.length()-2));
}
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

PaulS

Quote
Should I use the serialEvent() function?
I've said that twice, so, yeah, it seems like a good idea.
The art of getting good answers lies in asking good questions.

giulio_

This reads the new color index not as a byte, but as a single line containing the number printed.

Code: [Select]

import processing.serial.*;
Serial myPort;

PImage logo;

int bgcolor = 0;

void setup() {
  colorMode(HSB, 255);

  logo = loadImage("http://arduino.cc/logo.png");
  size(250, 200);

  println("Available serial ports: ");
  println(Serial.list());

  myPort = new Serial(this, Serial.list()[1], 9600);
  myPort.bufferUntil('\n');
}

void draw() {
  background(bgcolor, 255, 255);
  imageMode(CENTER);
  image(logo, width/2, height/2);
}

void serialEvent(Serial myP) {
  String inLine = myP.readString();
  bgcolor = Integer.parseInt(inLine.substring(0, inLine.length()-2));
}

Thank you, this is pretty clear.

Go Up