To Interrupt or not to Interrupt

Hi everyone, I am working on a project on motion capturing an guidance. The goal is to let an Arduino board blink lights in a certain pattern to indicate the start and direction of a movement, measure this movement (dynamic acceleration) with accelerometers and detect whether it's done in the right way and finally determine the tilt at the final position and if necessary correct this position by giving tactile feedback. I now have a setup with an Arduino Uno that can make LEDs blink in a certain pattern (like Knight Rider etc.), turn vibrating elements on or off and read data from an accelerometer. This system should be controlled with a computer, for which a team member has defined a communication protocol. The Arduino should be able to accept commands in the form of "start accelerometer measurement", "read accelerometer data", "start LED pattern 1", "turn vibration sensor 2",... As I see it, I have a few options:

  1. Process all of this in the Arduino loop(). Check for commands, read and store data, send data, activate LEDs, activate vibrating elements and start over. I guess this would be okay since the LEDs and vibrating elements need not be active the entire time.
  2. Process the feedback (LEDs and vibrating elements) in the loop() function and write an ISR to check for commands and receive/send/store data.
  3. Process the feedback (LEDs and vibrating elements) and receive/send/store data in the loop() function and write an ISR to check for commands.
  4. Check for commands and receive/send/store data in the loop() function and process the feedback in the ISR.

I am inclined to use option 1 in the first phases of the project and use option 3 for the final system. The only reason I would avoid using timed interrupts is that as far as I know this disables the PWM on some pins and I will probably need to change the intensity of the light and vibration in later phases of the project. However, it's been a long time since I have done any kind of programming and I have no idea what the best practice would be for this problem. Any advice?

When evaluating your options, you need to answer this question:

  1. Process the feedback (LEDs and vibrating elements) in the loop() function and write an ISR to check for commands and receive/send/store data.
  2. Process the feedback (LEDs and vibrating elements) and receive/send/store data in the loop() function and write an ISR to check for commands.
  3. Check for commands and receive/send/store data in the loop() function and process the feedback in the ISR.

What, exactly, is going to generate the interrupt that causes your ISR to be called?

I don't see anything in what you have described that will generate the interrupt. Did I miss something?

A lot depends on the speed of your patters and your sampling frequency for the sensors. If the time between your actions is on the order milliseconds or more, I wouldn't worry too much about interrupts, make just a loop. If some timings have to be more precise, having the critical part of the code done in an interrupt might be advantageous.

The serial receiver writes into a buffer anyway in an interrupt, so you don't have to worry about it anyway, just process it before the 128 byte buffer is full.

And finally, sometimes a well designed main loop can be more precise than interrupts, so for really short and precise timings, interrupts may give worse results than a busy loop.

Korman

What, exactly, is going to generate the interrupt that causes your ISR to be called?

I don't see anything in what you have described that will generate the interrupt. Did I miss something?

I was thinking of using timed interrupts, like on this website: Arduino Interrupts – uCHobby

A lot depends on the speed of your patters and your sampling frequency for the sensors. If the time between your actions is on the order milliseconds or more, I wouldn't worry too much about interrupts, make just a loop. If some timings have to be more precise, having the critical part of the code done in an interrupt might be advantageous.

The time between the actions is in the order of seconds, so that will not be a problem. I guess it's best to keep everything in the main loop.

I was thinking of using timed interrupts

A timer based interrupt will fire at some random point in your code, and cause your ISR to do some stuff. I fail to see how that can possibly be better than checking for serial input in loop(), on each pass.

ISR routines need to be kept very short. The things you propose to do in the ISR don't belong there. The ISR should simply set a flag indicating that loop() needs to do stuff, the next time it checks the flag.

Again, I fail to see how that would be more effective than simply checking for serial data in loop().

I don't think that you have a case for using interrupts. So, I'd go with the not.

ISR routines need to be kept very short. The things you propose to do in the ISR don't belong there. The ISR should simply set a flag indicating that loop() needs to do stuff, the next time it checks the flag.

This was exactly the thing I wasn't sure off, what does and doesn't go into ISR. Most of the programming I've done before was in high level languages and I've never needed to manually create an ISR before. But it seems I'm not going to need it now either. Thanks for the advice!