Feedback loop with an Arduino via Serial

My current application is a motion picture film scanner. I’m using RealStudio 2011 R1.1 for the desktop application, and an Arduino Mega for control of the hardware. I’m sending messages to the arduino via Serial.write(). These messages are typically in the form of: “|” – so for example, if I want to advance the film by 2 perforations I can call “advanceFilmByPerf|2” – this is working really well.

In the other direction, I’m listening for messages from the Arduino. There’s a lot of data that might sent back to me: error messages, position updates, sensor state information. Similar to what the application sends the board, the board sends the messages in the form of: “|” – again, this works well.

I want the application to be able to tell the Arduino to do a series of things from inside a loop, but those tasks are sometimes conditional, based on feedback from the scanner. What I want is a feedback loop with the Arduino. For example, if I want to scan a sequence of frames:

[code]Start a loop that iterates for the number of frames we’re scanning

  1. Tell Arduino to turn on the red light
    1a) wait for response from Arduino, then proceed to step 1b
    1b) tell frame grabber (via declare calls to its DLL) to snap the red frame
    1c) Do stuff with the snapped frame
  2. Tell Arduino to turn off the red light, turn on the green light
    2a) wait for response from Arduino, then proceed to step 2b
    2b) tell frame grabber to snap the green frame
    2c) Do stuff with the snapped frame
  3. Tell Arduino to turn off the green light, turn on the blue light
    3a) wait for response from Arduino, then proceed to step 3b
    3b) tell frame grabber to snap the blue frame
    3c) Do stuff with the snapped frame
  4. Tell the arduino to advance the film one frame

End the loop if we’re done.
[/code]

The problem I’m having is that while I’m in the loop, which is blocking, I don’t seem to be getting the incoming serial data, some of which I need inside the loop. One example is a frame counter: the Arduino sends the current frame number back to the application at the beginning of the sequence outlined above. But the frame counter in realstudio isn’t getting updated. It’s as if the function I wrote to do that never gets called - and in fact, I added a break into that function to debug, and I never reach it.

However, if I remove the While/Wend loop from around that block of code, and only run the sequence on the current frame, I’m getting the serial messages loud and clear.

So how do I best set up a system like this? I can’t poll the arduino at each step in the loop where I need information, because the loop prevents the serial messages from getting through. Besides, there’s other information that’s not related to what I’m doing in the loop, that I need to catch and process asynchronously (such as writing stuff to a log file, or catching Panic messages from the arduino in the event a sensor determines something bad has happened).

Thanks!

-perry

Some suggestions that may help.

Use a Timer to extract data from the Serial object, the DataAvailable() event under the Serial object, or a combination of both. See the following thread for examples:
https://forum.xojo.com/12492-need-a-simple-serial-terminal/13
https://forum.xojo.com/12492-need-a-simple-serial-terminal/32

Encapsulate your loop in a thread so it yields CPU to the other tasks (e.g. consume Serial events).

Thanks. Since I posted that, I’ve gone back through my code with a fine toothed comb, and I think I’ve got it figured out – at least, I’m now getting data from the Arduino, while I’m in my loop. I’m calling serial.poll() more frequently within the loop now, to check and see if in fact I’ve received anything from the board, and that seems to be working. The previous test I did with serial.poll had it in a bad spot in the code (it wouldn’t have received anything new from the board at that point), and I mistakenly thought that meant I couldn’t access the incoming serial data.

I’ve added in a call to it after every outgoing serial command to the board where I require a response before proceeding, and that seems to be working, at least in my preliminary testing.

Thanks!