proccesing disconnect from arduino

hello! My problem seems to be that i use processing with the arduino and after fiddling with my project on processing the serial connection stinks out and my processing sketch freezes! a quick press of the "stop" button in the processing program stops the program but upon trying to restart the program i get a bunch of red errors and some kind of error initiating serial connection. the only way to get the whole thing working again is by unplugging the usb cable and re plugging it back in, then everything starts to work again. I'm running with an arduino Diecimila ( yes the older one ) with the most recent processing and most recent firmita AND most recent arduino ide. ( i believe but ill re-check ).

I'm also wondering if perhaps this is a known bug or not, and if there's a way to offset it. I was thinking perhaps of injecting a line of code into firmita that restarts the arduino automatically when this problem occurs?

Thank you for any help! I'm working on a very big project and getting the seamless connectivity between arduino and processing is imperative, so i really appreciate any bit of help!

Can you share your Processing and Arduino code?

There is nothing inherent about the Arduino that would cause it to drop the serial connection, so there must be something you are doing (in your code) that is the source of the problem.

Absolutely!

well my code for processing is a work in progress, so it’s going to continually change, but nothing with the serial connection stuff is going to change, only things in the void draw are will, purely to control what i already set up.

processing:

import processing.serial.*;

import cc.arduino.*;

Arduino arduino;
int col1;
int col2;
int val1;
int val2;
int val3;


void setup() {
  size(screen.width,screen.height);
 
  arduino = new Arduino(this, Arduino.list()[1], 57600);
  
 
arduino.pinMode(3, Arduino.OUTPUT);
arduino.pinMode(4, Arduino.OUTPUT);
arduino.pinMode(5, Arduino.OUTPUT);
arduino.pinMode(6, Arduino.OUTPUT);
arduino.pinMode(7, Arduino.OUTPUT);






}

void draw() {
 background(0,0,0);

 noStroke();
 fill(255);
 quad(20, 50, 1250, 50, 1250, 400, 20, 400);
 quad(600, 350, 1000, 350, 1000, 800, 600, 800);
 
 //----------------------------------------single spot
 if(mouseX > 200 & mouseY > 100 & mouseX < 250 & mouseY < 130 & mousePressed == true)
{
val1 = 1;
}
else
{
 val1 = 0;
}
if(mouseX > 200 & mouseY > 130 & mouseX < 250 & mouseY < 150 & mousePressed == true)
{
  val2 ++;
  delay(200);
}
if(val2 >= 2)
{
  val2 = 0;
}
val3 = val1 + val2;

if(val3 == 1)
{
  col1 = 255;
  col2 = 0;
  arduino.digitalWrite(5, Arduino.HIGH);
}
else
{
    col1 = 0;
  arduino.digitalWrite(5, Arduino.LOW);
}
  
 
stroke(100);
fill(col1,col1,col2);
rect(200, 100, 50, 50);
rect(200, 130, 50, 20);


//-----------------------------------------triple spot
if(mouseX > 775 & mouseY > 550 & mouseX < 825 & mouseY < 600 & mousePressed == true)
{
  col1 = 255;
  col2 = 0;
  arduino.digitalWrite(2, Arduino.HIGH);
}
else
{
  col1 = 0;
  arduino.digitalWrite(2, Arduino.LOW);
}
  
stroke(100);
fill(col1,col1,col2);
rect(775, 550, 50, 50);




}

please please don’t make fun of my code :slight_smile:

Arduino:

the code is just standard firmita, i couldn’t fit it in the post sorry… i’m running arduino 0018 so that should give you the code…

The & is a bit operator. The && is the logical AND operator. You are using & where you should be using &&. Both compile, but the effect is not the same.

The compiler ignores most white space, so the long if tests can be split and put on two (or more) lines. That helps the gray-matter powered interpreters:

if(mouseX > 200 && mouseY > 130 &&
    mouseX < 250 && mouseY < 150 &&
    mousePressed == true)

As soon as a false condition occurs, evaluation of the conditions stops. The least likely condition in that list of conditions is the mousePressed condition, so, it should come first. No reason to determine where the mouse is, if no button is pressed.

On the Arduino, the loop function is called in an endless loop. Things that need to happen only once should be done in setup, instead.

In Processing, draw is called in an endless loop. You’re redrawing stuff over pass through the draw function, even though nothing has changed.

While it may not make a difference in this application, it’s better to start with good habits than to try to change them later, when it does matter.

Since draw is called in an infinite loop, and under the right conditions, you are streaming a lot of data at the Arduino, I’m wondering if the serial buffer is filling up, and that causes your program to go haywire.

You could put a short delay after each Arduino operation, too see if that helps.

Is there some particular “fiddling” that you do that causes the serial connection to “stink out”?

The compiler ignores most white space, so the long if tests can be split and put on two (or more) lines.

That’s an excellent practice that too few people follow. Personally, I prefer to split the tests by putting the ‘&&’ or ‘||’ at the beginning of the continuation line, like this:

if (mouseX > 200 && mouseY > 130
    && mouseX < 250 && mouseY < 150 
    && mousePressed == true)

I think that helps make it more clear that it’s a continuation of the “if”, rather than code that’s conditional on it. But that’s at least partly a matter of taste.

The compiler also is unbothered by “extra” parentheses: when conditions get complex, explicitly identifying “sub-conditions” by enclosing them in parentheses can make it more clear to some other person reading your code what’s being tested.

And “some other person” includes you, if you haven’t worked on the code for a few months.

Ran

hahaha i knew you guys would make fun of my code, i'm a very big noob ( i also haven't used processing in months so i forgot what little i even know! ) but thank you for the advice and i will fix all the &'s in my sketch! is for your fiddling question, there is no fiddling that i do. prior to posting here i kept testing my sketch over and over and i realized that YES: if i mess with the turning on and off of the pin very fast ( click the mouse to make the sketch turn the digital pin on and off very fast ) it eventually craps out on me in random amounts of time, but also when i'm seriously testing it and i happen to press it once or twice, not fast at all, or just press it once, the whole freak out thing happens, with the disconnecting and whatnot. As for the delays, i don't mind adding delays if you think it will help i just don't want to compromise the speed and interactive refresh rate i have going, if you can understand what i mean... so when do you think i should add delays? after every section for every new light? and how big of a delay? 1 ms, 10 ms?

oh and if you meant i should move everything to setup, i can't because i need to have everything in draw continue to operate over and over, it is int a one time thing.

Nobody was making fun of your code. Everybody starts out knowing nothing about programming. Some muddle along on their own until they get it. Some ask for help.

You made a couple of newbie mistakes. Those were pointed out as gently as possible.

There were some suggestions about formatting. You can accept the comments and make changes, or not. Totally your choice.

In your draw function, you have these statements:

 background(0,0,0);

 noStroke();
 fill(255);
 quad(20, 50, 1250, 50, 1250, 400, 20, 400);
 quad(600, 350, 1000, 350, 1000, 800, 600, 800);

stroke(100);
fill(col1,col1,col2);
rect(200, 100, 50, 50);
rect(200, 130, 50, 20);

stroke(100);
fill(col1,col1,col2);
rect(775, 550, 50, 50);

None of them are based on any variables. None of them are conditional. So, exactly the same code will be executed EVERY time though draw. That could be thousands of times per second.

Moving some, or all of this stuff to setup is possible. If you elect not to, that is your choice. It's better to be an informed choice, though.

From your original post:

i use processing with the arduino and after fiddling with my project on processing the serial connection stinks out

So, I was wondering what fiddling you were doing, and what exactly the serial connection is doing. My book of technical jargon is missing that prase, I'm afraid.

ha don’t worry i was joking about my code, i know it’s very nooby, but as far as i know it’s serving my purposes, im not sure if changing it around will change how it works, so i’d rather leave it. i will however fix that things that only don’t make sense! ill also change it if it is the reason for making the connection not work… so i think i defined my fiddeling in my previous post… is there something i need to update?

rethinking it, i don't mind fixing my code up... but im wondering what you think i can put in the setup part, i thought that everything i put in the draw needed to stay there because it needed to be constantly changed and checked...

The quad statements at the beginning of the draw method are always drawn the same.

The rect’s drawn later are always in the same place, but are different colors, depending on the mouse position. I missed that when I was suggesting that all the drawing was always the same.

You should be able to move the code up to the mouse location tests into setup, since that drawing code never changes.

In this case, though, it’s probably not worth the effort.

If you were constructing a real-time graphing function, or an interactive display, moving as much of the non-changing drawing functions out of draw would be a good thing.

But i still need to find out what making the serial connection crap out on me :(

But i still need to find out what making the serial connection crap out on me

You need to explain this statement better. Does Processing close the serial connection? Does the Arduino quit receiving data? Does Processing quit sending? What evidence do you have that the serial connection has "crapped" out? I checked by book of technical jargon, and I didn't see that term in there, so I need more details.

oh darn man i'm really sorry! i should have been more specific!!!

when i say "crap out" i mean the processing sketch i'm running during that time FREEZES!, the things i made "buttons" in my sketch ( the black squares ) are no longer click able, and the window becomes unclosable if i try to click the "x" button at the top right. I have to click my windows key, switch into the processing ide and click the "stop" ( small square button on top left ) Then if i try to simply click the "play" ( or the "run ) button again, the sketch does NOT work. I have to physically yank the usb cable from the computer, and plug it back into the computer for the sketch to work again, which leads me to believe the problem is arduino based.

Also: the ide prints out this message out at the bottem in red:

Error 0x5 at /home/bob/foo/rxtx-devel/build/../src/termios.c(482): Access is denied.

Error 0x5 at /home/bob/foo/rxtx-devel/build/../src/termios.c(2714): Access is denied.

Error 0x5 at /home/bob/foo/rxtx-devel/build/../src/termios.c(482): Access is denied.

Error 0x5 at /home/bob/foo/rxtx-devel/build/../src/termios.c(2714): Access is denied.

Error 0x5 at /home/bob/foo/rxtx-devel/build/../src/termios.c(482): Access is denied.

Error 0x5 at /home/bob/foo/rxtx-devel/buijava.io.IOException: No error in nativeavailable at gnu.io.RXTXPort.nativeavailable(Native Method) at gnu.io.RXTXPort$SerialInputStream.available(RXTXPort.java:1532) at processing.serial.Serial.serialEvent(Serial.java:221) at gnu.io.RXTXPort.sendEvent(RXTXPort.java:732) at gnu.io.RXTXPort.eventLoop(Native Method) at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575) ld/../src/termios.c(2714): Access is denied.

Error 0x5 at /home/bob/foo/rxtx-devel/build/../src/termios.c(482): Access is denied.

Error 0x5 at /home/bob/foo/rxtx-devel/build/../src/termios.c(2714): Access is denied.

Error 0x5 at /home/bob/foo/rxtx-devel/build/../src/termios.c(482): Access is denied.

Error 0x5 at /home/bob/foo/rxtx-devel/build/../src/termios.c(2714): Access is denied.

Error 0x5 at /home/bob/foo/rxtx-devel/build/../src/termios.c(482): Access is denied.

Error 0x5 at /home/bob/foo/rjava.io.IOException: No error in nativeavailable at gnu.io.RXTXPort.nativeavailable(Native Method) at gnu.io.RXTXPort$SerialInputStream.available(RXTXPort.java:1532) at processing.serial.Serial.serialEvent(Serial.java:221) at gnu.io.RXTXPort.sendEvent(RXTXPort.java:732) at gnu.io.RXTXPort.eventLoop(Native Method) at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575) xtx-devel/build/../src/termios.c(2714): Access is denied.

Error 0x5 at /home/bob/foo/rxtx-devel/build/../src/termios.c(482): Access is denied.

Error 0x5 at /home/bob/foo/rxtx-devel/build/../src/termios.c(2714): Access is denied.

Error 0x5 at /home/bob/foo/rxtx-devel/build/../src/termios.c(482): Access is denied.

Error 0x5 at /home/bob/foo/rxtx-devel/build/../src/termios.c(2714): Access is denied.

Error 0x5 at /home/bob/foo/rxtx-devel/build/../src/termios.c(482): Access is denied.

Error 0x5 at /hjava.io.IOException: No error in nativeavailable at gnu.io.RXTXPort.nativeavailable(Native Method) at gnu.io.RXTXPort$SerialInputStream.available(RXTXPort.java:1532) at processing.serial.Serial.serialEvent(Serial.java:221) at gnu.io.RXTXPort.sendEvent(RXTXPort.java:732) at gnu.io.RXTXPort.eventLoop(Native Method) at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575) java.io.IOException: No error in nativeavailable at gnu.io.RXTXPort.nativeavailable(Native Method) at gnu.io.RXTXPort$SerialInputStream.available(RXTXPort.java:1532) at processing.serial.Serial.serialEvent(Serial.java:221) at gnu.io.RXTXPort.sendEvent(RXTXPort.java:732) at gnu.io.RXTXPort.eventLoop(Native Method) at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575) ome/bob/foo/rxtx-devel/build/../src/termios.c(2714): Access is denied.

Error 0x5 at /home/bob/foo/rxtx-devel/build/../src/termios.c(482): Access is denied.

Error 0x5 at /home/bob/foo/rxtx-devel/build/../src/termios.c(2714): Access is denied.

Error 0x5 at /home/bob/foo/rxtx-devel/build/../src/termios.c(482): Access is denied.

Error 0x5 at /home/bob/foo/rxtx-devel/build/../src/termios.c(2714): Access is denied.

Error 0x5 at /home/bob/foo/rxtx-devel/build/../src/termios.c(482): Access is denied.

Erjava.io.IOException: No error in nativeavailable at gnu.io.RXTXPort.nativeavailable(Native Method) at gnu.io.RXTXPort$SerialInputStream.available(RXTXPort.java:1532) at processing.serial.Serial.serialEvent(Serial.java:221) at gnu.io.RXTXPort.sendEvent(RXTXPort.java:732) at gnu.io.RXTXPort.eventLoop(Native Method) at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575)

And in the red box it sais:

Error inside serial.write()

and thats what i mean when i say "crap out, sorry about that!!!

sorry to bump, but anybody know what the problem is? is it possible my arduino is "outdated" and i just need to get the newer one?

I get this as well when I intentionally disconnect the arduino from my computer. Any way to detect this on the processing side and reconnect to the arduino in question?

I'm trying to build fault tolerant data acquisition board with arduinos, and this processing script is going to be collecting data on 10 different units. If one gets disconnected and the processing script hangs, we'd potentially lose a couple months worth of data...

To fix it... I ended up needing to get the latest version of the rxtx driver (alpha or beta version) and making my own serial driver off of that...

lot of work for something that should have been handled in the first place