Hi,
I'm building a project using Processing and Arduino using a relay board for turning a 220V light bulb on and off, and running into a strange problem. The application works fine for a while (between 30 seconds and 3 minutes) and after some time the Processing app crashes (the JVM stops working). The serial port is then unusable until I disconnect the USB cable and restart the Arduino board.
I suspected that the Arduino is using too much power so that the OS shuts down the USB port, but the same issue happens when I connect the board with it's own power supply.
I tried this with Processing 1.5 and 2.0, the issue is similar but the errors I get are slightly different - in all cases it is referring to an error writing to the serial port but I guess this is because it is closed by the OS. I'm pretty new to Arduino and electronics so I may be making no sense here, but this was my guess.
I'm attaching a trimmed down version of my code which recreates the problem. Any help will be greatly appreciated.
Processing:
import processing.serial.*;
final int UNIT = 250;
int wait;
int last;
boolean on = false;
Serial port;
void setup() {
size(400,400);
println(Serial.list());
port = new Serial(this, Serial.list()[0], 9600);
port.clear();
}
void draw() {
if (last + wait - millis() > 0)
return;
if (on) {
port.write('0');
on = false;
wait = 3 * UNIT;
}
else {
port.write('1');
on = true;
wait = 5 * UNIT;
}
last = millis();
}
Arduino:
const int LIGHT_PIN = 4;
void setup() {
pinMode(LIGHT_PIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
// read the incoming byte:
int incomingByte = Serial.read();
// say what you got:
//Serial.print("Received: ");
//Serial.println(incomingByte, DEC);
if (incomingByte == 48) {
//Serial.println("turning off");
digitalWrite(LIGHT_PIN, HIGH);
}
if (incomingByte == 49) {
//Serial.println("turning on");
digitalWrite(LIGHT_PIN, LOW);
}
}
}