Pages: [1]   Go Down
Author Topic: Error inside Serial serialEvent  (Read 805 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
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:

Code:
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);
    }
  }
}

 
Logged

Miami/Florida
Offline Offline
Sr. Member
****
Karma: 14
Posts: 342
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi:

The communication link between Arduino and computer is being lost or corrupted somehow. These are possible reasons:

-Check and/or replace the USB cable, could be deffective.
-Check for strong sources of interference close to your system, such as big AC motors, compressors, etc.
-Plug you system into a different outlet.
-Move the realy you are actuating a few feet away from Arduino and USB cable. The sparking in the contacts  could be interfering. You are switching 220 VAC with a light bulb which is a heavy load. This creates heavy sparking in the realy contacts. When the contacts close and open they spark and radiate Electromagnetic Interference (EMI) maybe strong enough to interfere with your system communication link. There are methods to reduce EMI. 
-Check the USB connector is not introducing faulty contacts when you move the cable.

Adrian
Logged

Perseverance is 90% of the solution. The remaining 10% is more perseverance.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the quick response - moving the relay farther away from the Arduino (now about 70cm) does seem to do the trick!
I also put a layer of tin foil between the arduino and relay.
I never would have guessed this.

Ideally I'd like the relay and board to be inside the same box - is there any way to reduce the interference at close distance?

Thanks again.
Logged

Miami/Florida
Offline Offline
Sr. Member
****
Karma: 14
Posts: 342
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes there are; but as this is a very complex subject, I would pass on the specific advises here. Although there are general meassures that can be taken to reduce EMI, there are also other methods highly dependent on the specifics of your circuits. Try to study the subject so you will understand the principles.

Just a few general subjects. There are many more...

-EMI filters.
-Relay contacts spark reduction methods.
-Relay coil damper diode.
-EMI shielding.
-Shieldered cables.
-Damper capacitors.
-etc.
Logged

Perseverance is 90% of the solution. The remaining 10% is more perseverance.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok, thanks. I'll go study a bit now...

you've been a tremendous help. I was breaking my head for hours around this problem.
Logged

Pages: [1]   Go Up
Jump to: