I would like to code my ISR to be time intensive or interrupt the main program for atleast 1 or more seconds. My goal is to verify that if I am reading a sensor in the main loop, would it stop reading the sensor when the code is interrupted and for how long. I have trying to call various functions from the ISR like to turn on the motor but everything is instantaneous.

  • 2
    Please explain what it is you want to achieve. It sound like you are trying to use ISR for something, while there might be a much better solution. – Gerben Dec 1 '16 at 16:11
  • Putting it simply, I am trying to design a position controller. I have an input which drives a motor. Hall sensors read the revolutions. Trying to determine which of the above, input or hall sensor output should be used as an interrupt – Vignesh Dec 1 '16 at 16:14
  • 2
    Could you please post your code? – sa_leinad Dec 1 '16 at 16:49
  • Use a pin interrupt for the hall-effect sensor. That way, whenever the hall-effect sensors triggers, you get an interrupt. Then you can calculate the speed using the difference in millis between this interrupt and the previous one. The main loop will act based on the current speed you've written to some variable. None of these things should take a lot of processor-time. – Gerben Dec 1 '16 at 18:43

How long can an interrupt be?

If you don't want anything else to happen, then as long as the time between successive interrupts.

would it stop reading the sensor when the code is interrupted and for how long.

While the interrupt is running nothing else can happen in software. Absolutely nothing. Only one thing ever happens at once - either your main loop or the interrupt. For the entire duration of your interrupt everything else is put on hold.

That includes things that rely on other interrupts to function, such as serial communications, millis() and delay(), etc. That means using such functions from within your interrupt handler is a big no-go.

It is generally very very bad practice to have an interrupt blocking for more time than absolutely necessary (a few milliseconds or even less). If you need to have things taking a long time then you should consider breaking them down into smaller operations and doing different sections at different times through different means.

  • Thank you. So between a rotary encoder reader and an input signal, I would assume based on the fact that the main loop is suspended, the encoder reader is better suited as an interrupt. Is that right? – Vignesh Dec 1 '16 at 16:12
  • Rotary encoders work well with interrupts, yes. React to a change in one of the signals and work out which way it has been turned. You need very rapid responses to a rotary encoder since the signals can change quite fast. – Majenko Dec 1 '16 at 16:14
  • Great, I guess that simplifies things. Thanks for your help – Vignesh Dec 1 '16 at 16:15
  • @Vignesh I'm not sure why you are treating this as an either/or situation. They can both be interrupts. All the interrupt has to do is make a record of the new input or increase a count of the number of pulses and set a flag indicating that the value has changed. The main loop then looks for those flags and does all the slow work in the background. Just make sure you code the main loop so that it can cope with the possibility that the data may get changed half way through. e.g. make a copy of it and work from the copy. – Andrew Dec 1 '16 at 17:05

Does the delay() function not work for you? It should halt all other processing, whether in ISR or not; so if you call it inside your ISR, your ISR would remain active for as long as the delay.

Of course, ISR should not be long like this. Perhaps there is a better way to code your test. Perhaps a variable that the sensor-reading code checks; if it's set to 1, don't read the sensor, and if it's 0, it's okay to read the sensor, etc.

  • Thank you. I think the delay() function does not work as it depends on the timer which is suspended when an ISR is called. Will check it either way. – Vignesh Dec 1 '16 at 16:10
  • The main goal is to see which aspect of the program should be an interrupt. The rotary encoder reader or an input signal – Vignesh Dec 1 '16 at 16:11
  • use delayMicroseconds(), then. – jose can u c Dec 1 '16 at 16:13
  • 1) delay() does not work if called from within an ISR. 2) When called from main code, delay() does not halt ISR processing. – Edgar Bonet Dec 2 '16 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.