We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpElectronics,  Serial Library › Arduino / Processing Lag Issues...
Page Index Toggle Pages: 1
Arduino / Processing Lag Issues... (Read 1025 times)
Arduino / Processing Lag Issues...
Dec 17th, 2006, 5:56pm
 
Hey,

I'm working with arduino, I have a grid of 64 photordiodes that I'm reading via the serial usb.

The grid of sensors is 8 x 8 and orginally translated via Processing to 64 circles on screen, that changed in size / color depending on light hitting the surface. Here's a look: http://www.traviskirton.com/lss/index.html

What I'm trying to do in the code that follows is increase the resolution from 64 to 225 by calculating the averages between points.

Take a 4-point resolution, where each point on screen represents a photodiode, like this:

.     .

.     .

I am trying to interpret the average values for the 5 points lying inbetween the photodiodes so that I end up with a 'seemingly' higher resolution:

.  .  .
.  .  .
.  .  .

The issue I'm running into is a severe amount of lag whenever I run the sketch.

I know I'm making a bunch of calculations, but to be honest I'm surprised that calculating the extra 161 points is so draining on the performance of the sketch (even when I use OPENGL).

Can anyone suggest a better way to approach this that what's I've come up with below?

Thanks!

Code:

import processing.opengl.*;
import processing.serial.*;

Serial port;
String portname = "/dev/tty.usbserial-A1000ecK";  
int[][] high, low, vals;
double[][] mult;
double x, y;
Point zero;
PFont font;
int fontSize, baudrate, reading, value, cols, rows;
boolean c;

void setup(){
 size(640,640,OPENGL);
 frameRate(60);
 baudrate = 115200;
 cols = 15;
 rows = 15;
 port = new Serial(this, portname, baudrate);
 high = new int[cols][rows];
 low = new int[cols][rows];
 vals = new int[cols][rows];
 mult = new double[cols][rows];
 x = width/(cols+1);
 y = height/(rows+1);
 c = false;
 noStroke();
 smooth();
}

void draw(){
 background(255);
 reading = port.read();
 if(reading >=0 && reading < 8){
   for(int i = 0; i < 8; i++){
     value = port.read();
     if(value >= 0)
       if(c)
         vals[reading*2][i*2] = (int)((value-low[reading*2][i*2])*mult[reading*2][i*2]);
       else
         vals[reading*2][i*2] = value;
   }
 }
 vals[6][10] = (vals[5][10]+vals[6][9]+vals[6][11]+vals[7][10])/4;
 updateVals();
 if(c)
   circles();
}

void updateVals(){
 updateLR();
 updateUD();
 updateLRUD();
}

void updateLR(){
 for(int col = 1; col < cols; col+=2){
   for(int row = 0; row < rows; row+=2){
     vals[col][row] = (vals[col-1][row]+vals[col+1][row])/2;
   }
 }
}

void updateUD(){
 for(int col = 0; col < cols; col+=2){
   for(int row = 1; row < rows; row+=2){
     vals[col][row] = (vals[col][row-1]+vals[col][row+1])/2;
   }
 }
}

void updateLRUD(){
 for(int col = 1; col < cols; col+=2){
   for(int row = 1; row < rows; row+=2){
     vals[col][row] = (vals[col-1][row]+vals[col][row-1]+vals[col][row+1]+vals[col+1][row])/4;
   }
 }
}

void circles(){
 for(int col = 0; col < cols; col++){
   for(int row = 0; row < rows; row++){
     fill(0);
     ellipse((float)(x*(col+1)), (float)(y*(row+1)), (float)(vals[col][row]/5), (float)(vals[col][row]/5));
   }
 }
}

void keyPressed(){
 if(key == 'i'){
   println();
   println("----------");
   for(int col = 0; col < cols; col++){
     for(int row = 0; row < rows; row++){
       if(vals[col][row] > 140)
         println("vals["+col+"]["+row+"] = "+vals[col][row]);
     }
   }
 }
 if(key == 'h' && !c){
   for(int col = 0; col < cols; col++){
     for(int row = 0; row < rows; row++){
       high[col][row] = vals[col][row];
     }
   }
   println("high set");
 }
 if(key == 'l' && !c){
   for(int col = 0; col < cols; col++){
     for(int row = 0; row < rows; row++){
       low[col][row] = vals[col][row];
     }
   }
   println("low set");
 }
 if(key == 'c'){
   c = true;
   for(int col = 0; col < cols; col++){
     for(int row = 0; row < rows; row++){
       mult[col][row] = 255.0/(high[col][row]-low[col][row]);
     }
   }
   println("calibrated");
 }
}
Page Index Toggle Pages: 1