Arduino Raspberry Pi Serial Interrupts

Greetings all

I am currently interfacing an Arduino and Raspberry Pi using serial communication. This is working, but occasionally things "get stuck".

On both the Arduino and the Raspberry Pi I am running code which at times requires one of the devices to spend a lot of time doing its own thing. And by a lot of time I mean up to 20 seconds -maybe longer!- in some cases as it waits for a user input. During this time, the other device is waiting for Serial.available() to be > 0, i.e. no serial communication. This also gives some time for noise to creep in from the very noisy environment.

My most pertinent problem is in fact that each device ends up waiting for the other one to make the first move. They are both waiting to receive serial data. Check mate. This is when it "gets stuck" I do believe.

I would like to implement an interrupt which, after an appropriate amount of time, tells the Raspberry Pi through serial communication that the Arduino is busy. According to Nick Gammon's notes serial printing is not advised to be used in an interrupt:

  • Don't attempt to delay, eg: delay (100);
  • You can get the time from a call to millis, however it won't increment, so don't attempt to delay by waiting for it to increase.
  • Don't do serial prints (eg. Serial.println ("ISR entered"); )
  • Don't try to do serial reading.

Hopefully including this "busy" feature will keep the order of operations correct on either device's end and alleviate the getting stuck problem. I would prefer to use interrupts, as there are many heavily nested loops which would be a pain to manually insert millis() timeouts in. Code is going on for 2000 lines now.

Additionally, is there any nifty way that I could restart the main for loop, without resetting the board, by means of an interrupt?

Thanks in advance, I hope there is indeed a solution for this that doesn't involve rewriting loads of code!

About to head out for work but a quick word...
It would involve a little more code...
Before the "busy stuff" on each controller, set a pin HIGH/LOW then make communication dependent on state of pin.

Can put into code later today if needed.