Hey everyone,
I've been fooling around with a sketch that controls an arduino (via the serial library) and also takes images via the video library. However, I can only do one at a time. If I try to take images AND control the arduino in the same sketch, it hangs. I've traced this as far as the hang being in arduino.available() (Serial.available()).
So my question: is this a bug, or am I doing something very wrong?
Sketch code (ignore any commented code lines, they're mostly there for debugging):
Quote:
import processing.video.*;
import processing.serial.*;
Serial arduino;
PImage nolaser;
PImage laser;
Capture video;
void setup() {
println(Serial.list());
println(Capture.list());
println("started");
size(640, 240); //So we can display two side-by-side images
video = new Capture(this, 320, 240, 12); //About the size my camera is, and on a slow framerate.
arduino = new Serial(this, "/dev/cu.usbserial-A4001tLy", 9600); // Start the arduino
arduino.write('A'); //Initialize communication
println("setup done");
}
void draw() {
//println("begin draw loop");
while(!video.available()) {
//idle
}
println("nolaser frame available...");
video.read();
//nolaser = video;
//println("read.");
image(video,0,0);
//arduino.write('G');
laserOn();
println("laser is now on!");
println("starting laser frame. Idling.");
while(!video.available()) {
//idle
}
//println("laser frame available...");
video.read();
//laser = video;
//println("read.");
//arduino.write('F');
laserOff();
println("laser is now off");
image(video,320,0);
}
boolean laserOn() {
println("attempting to turn on the laser...");
arduino.write('G');
//println("wrote!");
while(arduino.available()<1) {
//We wait!
println("waiting");
}
//Ok, the next piece is somewhat superflous now.
//But this makes sure the arduino is able to delay properly.
println("reading");
if(arduino.read()=='K') {
println("laser turned on");
return(true);
}
else {
return(false);
}
}
boolean laserOff() {
//println("attempting to turn of the laser...");
arduino.write('F');
//println("wrote!");
if (arduino.available()>1) {
//We wait!
//println("waiting");
}
println("reading");
if(arduino.read()=='K') {
//println("laser turned off");
return(true);
}
else {
return(false);
}
}
The specific point of failure is in laserOn() and laserOff() - the two lines which read
Quote:if (arduino.available()>1) {
. The sketch runs properly without the calls to laserOn() and laserOff(), but hangs as soon as it hits that specific line.
Finally, some machine specific info: Mac OS X 10.4.11, G4. The camera is a lego-branded Logitech quickcam web (as reported by Macam). Processing shows it as
[2] "QuickCam Web"
[3] "USB Video Class Video"
Comments, help, suggestions? Perhaps I should try putting the camera stuff and the laser stuff into a class so theres absolutely no way they can interfere with eachother?