Pages: [1]   Go Down
Author Topic: Hardware interrupt problem  (Read 303 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am having trouble with a hardware interrupt.
It is set to pin2 and does activate when pressed. I want it to run a little subroutine and it does that too.
It then return to the main loop where it left off and that's waht I don't want to happen.
I don't want it to excecute the main loop after the interrupt and just sit idle.

* Timer_w_int.pde (2.96 KB - downloaded 5 times.)
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do you need to declare runstate as volatile?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 642
Posts: 50423
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
It then return to the main loop where it left off and that's waht I don't want to happen.
That's the nature of an interrupt. Stop what you are doing, complete this higher priority task, then resume what you were doing.

Quote
I don't want it to excecute the main loop after the interrupt and just sit idle.
You don't have any choice about where to resume. You do have a choice whether loop() recognizes that the interrupt occurred, and modifies its behavior based on whether or not the interrupt occurred.
Logged

South Texas
Offline Offline
Edison Member
*
Karma: 8
Posts: 1025
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So - you just want this code to execute when the interrupt occurs?

You have 2 choices -
either put the code in the interrupt routine and it will execute whenever the interrupt is received

or

In the interrupt routine set a flag that you reset inside your routine in loop.

Kind of like this -

void Loop(){

    if (Flag) {
         your code here...
         Flag = 0;
      }

void Interrupt (){
   Flag = -1;
}

Working from my old and tired memory - you might want to swap the Flag = 0 and Flag = -1 to have it work properly.

Warning - will the code you want to run every interrupt execute in less time than the time between interrupts?
Logged

Pages: [1]   Go Up
Jump to: