Feasibility of Serial I/O in a Timer Interrupt

I am currently having issues trying to read a serial input signal from an IR receiver while in a timer interrupt loop. From browsing other forum posts, I have concluded that one can’t utilize serial I/O while in a timer interrupt ISR because the ISR works using serial I/O. Is there a solution to be able to look for an IR receiver signal while in an ISR?

My project involves controlling a stepper motor that goes through three different “routines” of rotating CW or CCW for a set number of pulses to actuate a lead screw. The stepper motor routine is selected by the user by using an IR remote to choose which mode to run. When the user presses play on the IR remote, the stepper motor routine starts. I am trying to allow the user to pause/resume the stepper motor routine by using a timer interrupt to look for a push of the pause button while still in the middle of the stepper routine.

I currently have the timer interrupt working to the point where a press of the pause button causes the stepper motor routine to halt, but the Arduino does not look for another press of the play button as it is programmed. Printing to the serial monitor shows me that the program enters my while() loop in the ISR, but skips over the code that looks for the next IR signal.

Attached is my code. Any help with this or suggestions with a better way to accomplish this task is greatly appreciated. This program is being used with a Uno.

Stepper Motor IR Control.doc (34 KB)

If you are using an IR receiver, you check every loop iteration if it has received something. If you have received something from the IR sensor, set flags to indicate what the command was (stop, pause, resume etc.) which you check later in the loop. If the commands from the IR device involve chains of several button presses, collect these in a buffer until you have a complete command, then set the flag.
Don’t block the processing of your sketch by waiting in an interrupt service routine until you have got and processed commands from the IR device.

When you are posting a sketch which is too big to fit between code tags, attach an ino text file (your sketch file), don’t put it in a Word document.

Both the Stepper and IRremote (and other) libraries use interrupts for their own purposes. If you try to use them in your own ISR, these libraries will not work any more.

Put all your logic glue into loop(), not into an ISR. The loop() function is the boss in your project, which asks the other workers for input and tells them what to do, and eventually sends reports to its own boss in front of the Serial Monitor :wink:

Please post your code as a .ino file.


Thank you all for the help as well as the tip on uploading long code in future forum posts. I'm new to the whole Arduino thing, as I'm a mechanical engineer instead of a ones and zeros wrangler.

I was able to get the play and pause figured out using for loops in my main loop instead of using the ISR which interfered with the my stepper motor and IR functions, as was pointed out.