Pages: [1]   Go Down
Author Topic: How to: Timeout routine inside an interrupt function  (Read 516 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
    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?
Logged

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 22
Posts: 1675
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: November 26, 2012, 08:59:23 am by DuaneB » Logged


Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 143
Posts: 5318
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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().
Logged

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 22
Posts: 1675
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: November 26, 2012, 11:06:16 am by DuaneB » Logged


Pages: [1]   Go Up
Jump to: