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 & HelpSyntax Questions › Error when using delay()
Page Index Toggle Pages: 1
Error when using delay() (Read 493 times)
Error when using delay()
Jul 13th, 2007, 7:47am
 
Hello.
I am trying to write a processing app to send a text file line by line to an Arduino. It works, but it's sending the file so fast that it doesn't have time to do what it needs to do which is to move stepper motors. I tried to use a delay() but it is giving me this error:

"java.lang.IllegalMonitorStateException: current thread not owner

at java.lang.Object.wait(Native Method)

at processing.core.PApplet.delay(PApplet.java:2121)

at Temporary_1338_948$1.drop(Temporary_1338_948.java:56)"

Code:

import processing.serial.*;

import java.awt.dnd.*;
import java.awt.datatransfer.*;

int senddelay = 250;
PFont font;
String textDisplayed = "";

DropTarget dt = new DropTarget(this, new DropTargetListener() {
public void dragEnter(DropTargetDragEvent event) {
// debug messages for diagnostics
//System.out.println("dragEnter " + event);
event.acceptDrag(DnDConstants.ACTION_COPY);
}

public void dragExit(DropTargetEvent event) {
//System.out.println("dragExit " + event);
}

public void dragOver(DropTargetDragEvent event) {
//System.out.println("dragOver " + event);
event.acceptDrag(DnDConstants.ACTION_COPY);
}

public void dropActionChanged(DropTargetDragEvent event) {
//System.out.println("dropActionChanged " + event);
}

public void drop(DropTargetDropEvent event) {
//System.out.println("drop " + event);
event.acceptDrop(DnDConstants.ACTION_COPY);

Transferable transferable = event.getTransferable();
DataFlavor flavors[] = transferable.getTransferDataFlavors();
int successful = 0;

for (int i = 0; i < flavors.length; i++) {
try {
Object stuff = transferable.getTransferData(flavors[i]);
if (!(stuff instanceof java.util.List)) continue;
java.util.List list = (java.util.List) stuff;

for (int j = 0; j < list.size(); j++) {
Object item = list.get(j);
if (item instanceof File) {
File file = (File) item;


String filename = file.getPath();
if(filename.lastIndexOf(".txt") != -1){
String lines[] = loadStrings(filename);
println("there are " + lines.length + " lines");

for (int p=0; p < lines.length; p++) {
delay(senddelay);
String currentline = lines[p];
println(currentline);
send(currentline);
}
}
else{
println("wrong filetype! txt supported");
}
}
}

}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
);

// The serial port
Serial port;

// Serial variables
String serialString = "";

void setup() {
size(200, 200);



// SERIAL SETUP
// Print a list of the serial ports, for debugging purposes:
println("Serial ports");
println(Serial.list());

// On my computer, the Arduino is the first port
print("Selected port: ");
println(Serial.list()[1]);
port = new Serial(this, Serial.list()[1], 57600);

font = loadFont("Arial-BoldMT-48.vlw");
textFont(font, 22);
}

void draw() {
background(176);
text(textDisplayed, 15, 60);
}

void send(String currline) {
textDisplayed = currline;
port.write(currline);
port.write(13);
}
Re: Error when using delay()
Reply #1 - Jul 13th, 2007, 1:42pm
 
drop events happen on another thread, so you can use delay there. you might try using Thread.sleep(senddelay) instead, but you should probably queue the delay events and handle them in draw().
Page Index Toggle Pages: 1