Go Down

Topic: How to: Timeout routine inside an interrupt function (Read 651 times) previous topic - next topic

domingosl

I need to check for timeout after 200ms, normaly in the main loop I use:

Code: [Select]
    unsigned long started_waiting_at = millis();
    bool timeout = false;
    while ( ! radio.available() && ! timeout )
      if (millis() - started_waiting_at > 200 )
        timeout = true;


But now I need to use this inside an interrupt function, and as you know millis dont update when its outside the main loop, how can I solve this?

DuaneB

#1
Nov 26, 2012, 02:56 pm Last Edit: Nov 26, 2012, 02:59 pm by DuaneB Reason: 1
I am guessing from the timeout value that your trying to read an RC pulse or frame. You should not be inside an interrupt for more than a few microseconds, its really bad design to be in there for longer. Keep your ISR for fast signal measurement, and do the higher level functions like checking for timeouts, missing pulses, valid and invalid pulses in loop.

This might help - there is quite a lot of explanation in this series of posts
http://rcarduino.blogspot.com/2012/01/how-to-read-rc-receiver-with.html
http://rcarduino.blogspot.com/2012/04/how-to-read-multiple-rc-channels-draft.html
http://rcarduino.blogspot.com/2012/11/how-to-read-rc-channels-rcarduinofastlib.html

Duane B

rcarduino.blogspot.com
Read this
http://rcarduino.blogspot.com/2012/04/servo-problems-with-arduino-part-1.html
then watch this
http://rcarduino.blogspot.com/2012/04/servo-problems-part-2-demonstration.html

Rcarduino.blogspot.com

fungus


But now I need to use this inside an interrupt function, and as you know millis dont update when its outside the main loop, how can I solve this?


You should never stay in an interrupt for200ms...

Maybe you can set a flag in the interrupt and deal with it in loop().
No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

domingosl



But now I need to use this inside an interrupt function, and as you know millis dont update when its outside the main loop, how can I solve this?


You should never stay in an interrupt for200ms...

Maybe you can set a flag in the interrupt and deal with it in loop().



Using a flag is a nice idea, but why do you say never stay in an interrupt for200ms ? whats wrong with that?

DuaneB

#4
Nov 26, 2012, 05:02 pm Last Edit: Nov 26, 2012, 05:06 pm by DuaneB Reason: 1
Hi,

The Arduino can only process one interrupt at a time, so everything else that uses interrupts will not work, some realworld examples -

millis and micros functions use interrupts, so will become unusable

The servo library also uses interrupts and will become unusable

Interrupts are a generally assumed to be time sensitive and everyone who writes code for others to use has this generally understanding that they will be in and out of interrupts as quickly as possible and so will the users of thier code.

If anyone starts to 'hog' interrupts, everything starts to fall apart - apart from that its easy to avoid doing so there really is no advantage or justification for long slow blocking interrupts.

Bit more information here -
http://rcarduino.blogspot.com/2012/11/how-to-read-rc-channels-rcarduinofastlib.html

Duane B

rcarduino.blogspot.com
Read this
http://rcarduino.blogspot.com/2012/04/servo-problems-with-arduino-part-1.html
then watch this
http://rcarduino.blogspot.com/2012/04/servo-problems-part-2-demonstration.html

Rcarduino.blogspot.com

Go Up