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