Arduino not swithing off if usb communication is lost

I am running a mega2560 board from Processing via Firmata. The problem is that if I loose communication between processing and the arduino (say the computer hangs or the usb cable pull out) then the arduino keep the last instructions, so for instance motors keep on running. I would like the arduino to reset to the defautls as it were before the sketch started running. All help would be appreciated.

I would like the arduino to reset to the defautls as it were before the sketch started running.

How are you powering the Arduino? Power it from the USB cable, and unplugging it will stop the sketch from running.

The problem is that if I loose communication between processing and the arduino (say the computer hangs

You really can't expect the computer to tell the Arduino that it is going to hang. If it knew that, it wouldn't do it.

then the arduino keep the last instructions

As it is supposed to.

Please explain how you are powering the Arduino and the motors.

Thanks for the reply.
Feeling quite stupid this side because of pulling the cable. I understand that the computer can't give notice that it is going to crash and I was not thinking is that line. I was wondering if it was possible to work code into the firmata sketch running on the arduino that would check if there are still communication going on and if communication was lost then it can reset to the state it was in before the sketch started running. As for pulling the cable I was not thinking straight.
Thanks

I was wondering if it was possible to work code into the firmata sketch running on the arduino that would check if there are still communication going on and if communication was lost then it can reset to the state it was in before the sketch started running.

No, yes, and maybe.

Yes, it is possible to write code to implement handshaking. That is, periodically (on whatever schedule is appropriate), the Arduino asks the other end to confirm that it is still there. If it gets the expected response, the sketch can continue what it is doing. If not, it can do something different.

No, it is not possible to do that with firmata running. Firmata is not well implemented, and is not supported at all, on the Arduino. There is no flexibility to extend the protocol that I can see. Ditch it, and develop your own. It is not hard.

The maybe part comes from you not being clear what you mean by "it can reset to the state it was in before the sketch started running." The "state it was in before the sketch started running" was powered off. The Arduino has no way to turn itself off. You could add a servo, rigged to pull the plug, when commanded, but I doubt that this is what you really want to do.

I appreciate the time you spend on this topic.
What I mean is that if I swith my laptop on and connect the arduino via usb and open up my sketch in Processing nothing is still happening on the arduino side. Its only once I run the sketch that the arduino comes to life. Now if for any reason the sketch stops running the arduino remains on in the last state that it was. I am running motors through relays connected to the arduino. It is part of a crushing plant and I have conditions like temperarture and pressures that I am monitoring that I use to switch of the motor if it go out of bounds.
The sketch don't run on the Arduino itself so that should unsafe conditions arise it would switch off what needs to be switched off. If I loose communication the motor will run even if it overheats or if the pumps run out of oil.

What I mean is that if I swith my laptop on and connect the arduino via usb and open up my sketch in Processing nothing is still happening on the arduino side. Its only once I run the sketch that the arduino comes to life.

This is not true. As soon as the Arduino is powered, it starts doing something. Whether that something is obvious or not is impossible to tell, since I have no idea what code is running on the Arduino or what hardware is attached to it.

I think that it is time that you get specific. Post your Processing sketch and your Arduino sketch, and tell what is attached to the Arduino.

Now if for any reason the sketch stops running the arduino remains on in the last state that it was.

Processing runs sketches. So does the Arduino. So, when you say the sketch stops running, I don't know whether you are referring to the Arduino sketch or the Processing sketch.

I am running motors through relays connected to the arduino. It is part of a crushing plant and I have conditions like temperarture and pressures that I am monitoring that I use to switch of the motor if it go out of bounds.

OK, so far.

The sketch don't run on the Arduino itself

Some sketch does.

so that should unsafe conditions arise it would switch off what needs to be switched off. If I loose communication the motor will run even if it overheats or if the pumps run out of oil.

If you are using Firmata for any kind of mission-critical situation, you really, and I mean REALLY, need to rethink this.

Firmata is fine for blinking LEDs. Beyond that, it is not.

I understand your last comment about not having used firmata and I understand. When I started this project which is now about a jear ago I did not know anything about programming (like in nothing) and I started looking on the internet for possibilities to build a PLC. I came across Arduino and in reading up it seemed to me like a good choice to go for firmata because I would only have to learn the programming of the processing sketch. Because of my inexperience with programming it took me a year to research examples and and learning some programming to get to where I am now. I was hoping for a solution like I asked for because I dont know how long I am going to struggle to learn to program the Arduino directly and to communicate all the data to processing and then control the Arduino from processing as well.
I have 1528 lines of code which can definitely be reduced by a lot through propper programming.
What I am doing with the Arduino:
I am measuring eight rtd temperature probes,
I am measuring four liquid pressure sensors,
I am using four digital inputs,
I have fourteen digital outputs that control relays through two transistor arrays,
All this I control using my laptop as a user interface module. I use my mouse to control the relays through radio buttons. All my machinery is then protected by the rest of the code and evrything is being displayed on the laptop screen. I would like to send you a screen shot of how the laptop screen looks like but do not know how to insert it.

All this I control using my laptop as a user interface module.

None of what you described precludes you from developing your protocol for exchanging data. Something as simple as (R=read, T=type - analog or digital, NN-pin number), (M=mode, T=type - input or output, NN=pin number), or <SNN,VVV> (S=set, NN=pin number, VVV=value) is easy to implement.

The Arduino could then send data to the serial port, and wait some period of time for a response. If it gets one, great. If not, it would turn off all output pins and go into an infinite loop doing nothing.

I must confess I have no idea what you are talking about but I will go and read up and research some more on it.
Is what you are suggesting (RTNN, MTNN) to run together with firmata or is this for when I dump firmata and write seperate code for Processing and Arduino?

Is what you are suggesting (RTNN, MTNN) to run together with firmata or is this for when I dump firmata and write seperate code for Processing and Arduino?

It's for when you dump Firmata.

Thanks

This can be done fairly easily. I used the watchdog timer with a slightly modified firmata set at 8 seconds. I then put the reset watchdog timer command into the on serial receive while loop.

This seams to be working for me although Its not solved.
I programmed some code into processing that switches a ledpin on and off on the arduino every 5 seconds.
I then told arduino to check if the led stays on or off for 8 seconds and if that happens to turn whichever pins low that I want to turn low.
This makes the operation somewhat safer for me but for some reason the communication between the arduino and processing keeps on dropping and then I have to restart the computer or alr-control-delete and then end the sketch. The sketch don't close any other way. When the sketch is running I just have to press esc and it closes. As soon as communication is lost it just freezes.

I tested the whole thing on the table and everything was running smooth. I have made a pcb with relays that are being switched from the arduino via transistor arrays. All the relays work and everything seems fine. As soon as I started the installation and connected the 220v through the ralays me problems started. As soon as I energized any of the 220v relays (even not with the arduino) the sketch just freezes . I then installed another set of 24v relays inbetween the relays on the pcb and the 220v relays. I also moved the arduino totally away from the 220v environment. I then installed the arduno in a steel sealed box with only the usb cable coming out. I then installed the pcb in its own steel box and fastened the arduino (in steel box) onto the box with the pcb.Only the ribbon cable comes out of the pcb case directly into the arduino. The rest of the cables from the pcb box goes to analog sensors and switches that run with 24v dc. The relays now switches 24v dc instead of 22v ac. I noticed that if my usb cable even touches my laptop's 19v charge cable it freezes the running sketch. Now I am making sure to run my usb cable as far away as possible from any other cables. After all these precautions the sketch still freezes maybe 4 times in as many hours. It is a problem because I am running machinery and every time this happens the whole plant must stop to restart the computer. Any suggestions would be welcome.

All these steel cases don't appear to be grounded are they?

What is the mains switching? It is that that is generating interference that is resting the arduino so some suppression would not go amiss.

Also extra decoupling on the arduino supply would help.

Sounds like a ground loop.

Some cheap USB cables have their "shields" incorrectly connected.

  1. What sort of load are you switching?
  2. Do you have X2 capacitor+ resistor ACROSS the relay contacts and a protection diode and 0.1uf ceramic parallel across the COIL?

Because it could be you have a massive spark gap generator, forcing EMF into the relay coils and back down to your arduino controller, via a common GND rail.