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 › Processing to arduino freeze my sketch
Page Index Toggle Pages: 1
Processing to arduino freeze my sketch (Read 1313 times)
Processing to arduino freeze my sketch
Feb 26th, 2009, 3:50am
 
Hi all, I'm trying to send simple 'int' over serial communication to my arduino. When I test the communication with simple Arduino and Processing sketch everythings working. The problem occur when I integrate serial communication code to an already working Processing sketch. A gray window appear but the sketch doesn't start. As soon as I remove all the 'serial communication code' my original sketch work like it should. I can't find what's the problem. I hope someone can help me! :)

Note: My arduino sketch is fine and perfectly working with his IDE console and a PureData patch.

What the sketch is suppose to do:
    - simplify a webcam image to a 6pixels X 6pixels image
    - simplify the colors to stay in a range of 8 basics colors
    - send to an arduino board throught serial 3 simple informations:
    position of the pixel( X and Y) and the colors of this pixel
    - those three int should be send separatly

What the arduino board do:
    - control a simple 6x6 matrix of red, bleu and green LEDs(108 LEDs)

Here is the code:
Quote:


/**
* Pixelate  
* by Hernando Barragan.
*
* Load a QuickTime file and display the video signal
* using rectangles as pixels by reading the values stored
* in the current video frame pixels array.
*/

import processing.serial.*;
import processing.video.*;

Serial port;

int pixelColor = 0;
int r = 0;
int g = 0;
int b = 0;
int ledVal = 0;
int tolerance = 100;
color screenCol = 0;
int grayVal = 0;
int testVal = 0;
int y = 0;

int numPixels;
int blockSize = 80;
             // Movie myMovie;
             
Capture cam;
color myMovieColors[];


void setup() {
 size(480, 480, P3D);
 port = new Serial(this, Serial.list()[0], 9600);
 //colorMode(RGB, 8);
 //size(640, 480, P3D);
 noStroke();
 background(0);

 cam = new Capture(this, 6, 6, 15);  

 numPixels = width / blockSize;
 myMovieColors = new color[numPixels * numPixels];
}


// Read new values from movie
void captureEvent(Capture m) {
 m.read();
 m.loadPixels();
 
 for (int j = 0; j < numPixels; j++) {
   for (int i = 0; i < numPixels; i++) {
     myMovieColors[j*numPixels + i] = m.get(i, j);
   }
 }
}


// Display values from movie
void draw()  {
 for (int j = 0; j < numPixels; j++) {
   for (int i = 0; i < numPixels; i++) {
     ledVal = 0;
     pixelColor = myMovieColors[j*numPixels + i];
     r = (pixelColor >> 16) & 0xff;
     g = (pixelColor >> 8) & 0xff;
     b = pixelColor & 0xff;
     
     grayVal = (r + g + b)/3;
     
     if(grayVal > 5){
       if(grayVal >= 22){
         if(grayVal >= 38){
           if(grayVal >= 88){
             if(grayVal >= 140){
               if(grayVal >= 176){
                 if(grayVal >= 229){
                   ledVal = 0;
                   testVal = 1;
                   }
                 if(testVal != 1){
                 ledVal = 1;
                 testVal = 1;
                 }
                 }
               if(testVal != 1){
               ledVal = 4;
               testVal = 1;
               }
               }
             if(testVal != 1){
             ledVal = 5;
             testVal = 1;
             }
             }
           if(testVal != 1){
           ledVal = 2;
           testVal = 1;
           }
           }
         if(testVal != 1){
         ledVal = 3;
         testVal = 1;
         }
         }
       if(testVal != 1){
       ledVal = 6;
       }
       }
     else{
       ledVal = 7;
       }
     testVal = 0;

Re: Processing to arduino freeze my sketch
Reply #1 - Feb 26th, 2009, 3:52am
 
Here's the second part of the program:
Quote:
       
//===========      
/*
     if(r >= tolerance){
       ledVal = ledVal + 4;
       }
     if(b >= tolerance){
       ledVal = ledVal + 1;
       }
     if(g >= tolerance){
       ledVal = ledVal + 2;
       }
       */
//==========
     
     switch(ledVal){
       case 0:
         screenCol = color(0, 0, 0);
         break;
       case 1:
         screenCol = color(0, 0, 255);
         break;
       case 2:
         screenCol = color(0, 255, 0);
         break;
       case 3:
         screenCol = color(0, 255, 255);
         break;
       case 4:
         screenCol = color(255, 0, 0);
         break;
       case 5:
         screenCol = color(255, 0, 255);
         break;
       case 6:
         screenCol = color(255, 255, 0);
         break;
       case 7:
         screenCol = color(255, 255, 255);
         break;
       }
     switch(i){
       case 0:
         y = 7;
         break;
       case 1:
         y = 6;
         break;
       case 2:
         y = 5;
         break;
       case 3:
         y = 4;
         break;
       case 4:
         y = 3;
         break;
       case 5:
         y = 2;
         break;
       }
     port.write(j);
     //print("ledVal="); println(ledVal);
     port.write(y);
     port.write(ledVal);
     fill(screenCol);
     rect(i*blockSize, j*blockSize, blockSize-1, blockSize-1);
   }
 }
}




thanks to all help I can get! :)
Re: Processing to arduino freeze my sketch
Reply #2 - Feb 27th, 2009, 2:46am
 
Well, I've answer my own question after hours and hours of search and experimentations. Here is the answer:
   1-It appear that the 'serial initialization code' don't like to be load after the 'Capture initialization code'. So I moved all 'serial code' to just a line before the corresponding 'capture code'. EX:
       Before:
Quote:


import processing.video.*;
import processing.serial.*;

Serial port;
Capture cam;

void setup(){
 size(480,480, P3D);
 
 port = new Serial(this, Serial.list()[0], 9600);  
 cam = new Capture(this, 6, 6, 15);
 
 }

void draw(){
 //code...
 }



      After:
Quote:


import processing.video.*;
import processing.serial.*;

Capture cam;
Serial port;

void setup(){
 size(480,480, P3D);
 
 cam = new Capture(this, 6, 6, 15);
 port = new Serial(this, Serial.list()[0], 9600);  

 
 }

void draw(){
 //code...
 }



It's a strange solution but it's working!

Note that the line 'port = new Serial(this, Serial.list()[0], 9600);' is AFTER the 'size(480,480, P3D);' line. If you want to solve the

Quote:
gnu.io.PortInUseException: Unknown Application
              at
 gnu.io.CommPortIdentifier.open(CommPortIdentifier.java:354)

error the serial code should always be after the 'size' line. Why? well... I don't know I found this solution at

http://todbot.com/blog/2008/01/14/in-processing-size-matters-for-serial-ports/

I hope it will help someone.
Page Index Toggle Pages: 1