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.
Page Index Toggle Pages: 1
Arduino + Processing + Webcam (Read 3744 times)
Arduino + Processing + Webcam
Jan 10th, 2010, 6:08pm
 
I've been working on a telecine project using Arduino, Processing and a webcam. As a summary, a sensor attached to the Arduino will send a "ok" message through the serial port. Processing will then take a snapshot using the webcam. I have a couple snipets of code that I've managed to dig up but getting them to work together is seeming to get the best of me.

My Arduino code doesn't seem to be causing the problem. So I'm doing my testing with this very simplified code on the Arduino end just so I can get a message sent across the serial port.

Code:
int buttonPin = 9;

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

void loop() {
 if(digitalRead(buttonPin) == HIGH) {
   Serial.print("!");
 } else {
   Serial.print("?");
 }
 delay(200);
}


This is the first part of my code that I'm using to read in from the serial port, courtesy of the libraries. I can get this code working independently while using the above Arduino code.

Code:
import processing.serial.*;

Serial arduinoPort;

void setup() {
 arduinoPort = new Serial(this, Serial.list()[0], 9600);
}

void draw() {
 byte[] inBuffer = new byte[7];
 while (arduinoPort.available() > 0) {
   inBuffer = arduinoPort.readBytes();
   arduinoPort.readBytes(inBuffer);
   if (inBuffer != null) {
     String myString = new String(inBuffer);
     println(myString);
   }
 }
}


And this is the code I'm using to take snapshots with the webcam. Also from the libraries and I can get it going independently.

Code:
import processing.video.*;
Capture webcam;

void setup() {
 size(640,480);
 webcam = new Capture(this,640,480);  
 String[] devices = Capture.list();
 println(devices);
 webcam.settings();
}

void draw() {
 if (webcam.available() == true) {
   webcam.read();
   image(webcam ,0,0);
 }
 saveFrame("line-####.bmp");
}


I think I'm having problems in the void draw(), but this is what my combined code looks like. I am also using the above Arduino code with this processing sketch.

Code:
import processing.serial.*;
import processing.video.*;

Serial arduinoPort;
Capture webcam;
boolean check;

void setup() {
 size(640,480);
 webcam = new Capture(this,640,480);  
 String[] devices = Capture.list();
 println(devices);
 webcam.settings();
 arduinoPort = new Serial(this, Serial.list()[0], 9600);

void draw() {
 byte[] inBuffer = new byte[7];
 while (arduinoPort.available() > 0) {
   inBuffer = arduinoPort.readBytes();
   arduinoPort.readBytes(inBuffer);
   if (inBuffer != null) {
      String myString = new String(inBuffer);

     // I think this is my problem line, tried different
     // things. Everything I try changing it to, ends
     // up causing an error of some sort
     
if (myString == "!") {

          check = true;          // if correct message check
      } else {                // true or check
          check = false;          // false
      }
      if (check != true) {               // if false println
          println("Not Aligned! \n");   // "Not Aligned!"
      }  else {
          if (webcam.available() == true) // cont.
                          && (check != false)) {
             webcam.read();
             image(webcam ,0,0);
          }
          saveFrame("line-####.bmp");
      }
    }
  }
}


I think my problem is the "if (myString == "!");" line. During testing (omitted to save room) I've added in a handful of println/delay() so I can visually follow the code and it all seems to be originating from that particular line. The boolean check if/else always goes to false. I can't seem to interpret the signal from the serial port into "ok take the picture." So I think I'm missing something or trying to compare two unalike variables.

Sorry for the lengthy post and the slight sloppiness of my code. I've been stuck on this for a while and wanted to be as thorough as possible. Any suggestions or comments would be great!

Thanks!  :)
Re: Arduino + Processing + Webcam
Reply #1 - Jan 11th, 2010, 11:40pm
 
I believe you are right about that line.
Try using "if (myString.equals("!")) {..."

http://processing.org/reference/String_equals_.html

Because a String is an object, using "==" compares the reference to that object, not the contents. So myString is not the same object as the constant "!".
Re: Arduino + Processing + Webcam
Reply #2 - Jan 16th, 2010, 5:52pm
 
Thanks NoahBuddy, that definitely was my problem!
Page Index Toggle Pages: 1