interrupts revisited

I've got a simple program that calls a function from the main loop when a momentary switch is pressed. When the function is finished, it calls another function, and so on until the process is complete. I'm looking for a way to to change the value of a variable when a second momentary switch is pressed for a short period of time, the second switch can be pressed at anytime during the process. I figured using an "attachInterrupt(interrupt, function, mode)" is the way to go. I've looked at a small bit of sample code at:

http://www.arduino.cc/en/Reference/AttachInterrupt

The "function" would be where I change the value of a variable, the "mode" would be "HIGH".
Unless there is a better way to go, can someone point me in the direction of more sample/example code related to this subject?

Thank you,
Cris

What was wrong with the sample code in that link? You just set a variable in the interrupt function and test that in your other functions. Make sure you make the variable "volatile" (as in the example) so the compiler knows it might change at any time.

The "function" would be where I change the value of a variable, the "mode" would be "HIGH".

HIGH isn't an option in the attachinterrupt function. And even if it was (along with LOW) is rarerly a correct method to utilize interrupts, as when the the ISR function completes and reenables interrupts one is bound to get caught up is a interrupt loop where one is not wanted if the HIGH input state is still active. Unless the active ISR can remove the HIGH state on the interrupt pin a circuil jerk is sure to result.

Using RAISING, FALLING, or CHANGE are the more normal and usually more valid methods to trigger interrupts events.

Nick,
There is nothing wrong with the code as it makes the led blink, but in the description it states "Specifies a function to call when an external interrupt occurs"... the code sample does not employ an external interrupt.
This is what I'm trying to accomplish:

  1. Press a start button that calls a function that causes a stepper motor to step "x" number of steps.
  2. Call a function that gives an output to cycle a solenoid.
  3. Call a function that increments a counter.

Then items 1 through 3 repeat in that order "y" number of times.

Here is my problem: there is a stop button that can be pressed momentarily anytime during this cycle of events...this momentary action needs to stop the process after item #3.
For now, the only way I can stop the process is to have a 4th function that reads the state of the stop button and either stops the cycle or doesn't. Problem is the stop button has be held in at the time the 4th function is called and that is unacceptable. I could do this with a latching relay (mechanically), but I'd rather find a software solution.

Thanks,
Cris

redtank:
There is nothing wrong with the code as it makes the led blink, but in the description it states "Specifies a function to call when an external interrupt occurs"... the code sample does not employ an external interrupt.

Yes it does:

  attachInterrupt(0, blink, CHANGE);

And earlier up that page:

Most Arduino boards have two external interrupts: numbers 0 (on digital pin 2) ...

So if you connect your switch to pin 2, this particular interrupt would detect a change on the pin (eg. you pressed it).

A more practical example would probably employ a pull-up resistor, eg.

pinMode (2, INPUT);  // (the default)
digitalWrite (2, HIGH);  // enable pull-up
attachInterrupt (0, switch_pressed, LOW);  // called when switch pressed

Then connect your switch in such a way that it grounds pin 2 when pressed. The pull-up keeps pin 2 high normally. Then the interrupt fires when pressing it brings it low, your routine is called, and you do whatever you need to do to stop the motor.

Nick,
Thank you for the explanation and sample code. With that, I was able to get the interrupt to work inside my program :smiley:

Cris