Can I choose where to restart after external interrupt?

Hi everyone,

I’m new to programming in C and to Arduino, I have an Uno board and am just playing around at the moment.

My problem is with an external reset button. I wrote a program to use in/outputs but everything must be reset once the interrupt button goes from LOW to HIGH. It all works as expected except that the interrupt runs from the place it finished in loop(). I want it to restart at the beginning of loop instead. Had a look at goto and setjmp/longjmp but the former is only local and the latter didn’t seem to be what I was after.

Does anyone have any suggestions/criticisms/code/thoughts that might help?

My prog:

/*************************************************************************
Program Name:   Test
Version:        2
Date:           10/09/2011
Author:         Heather Hedger
Changes:        change the sensor button to trigger opposite            
Inputs:         - push to break button
                - Push-to-make external interrupt sleeper button
Outputs:        - Visual alert (yellow LED)
                - Relay controlling red LED
                - Relay controlling yellow LED
Description:    Program starts on sensor high and starts 
                alert system, waits (user defined time) for user to manually 
                interrupt with sleeper. If not interrupted: switches on red  
                relay, waits (user defined time) and then triggers yellow relay. 
                The interrupt button resets the system - all alerts and 
                relays are switched off, except the yellow relay which is not 
                interruptable or resettable.
**************************************************************************/

/*-----------------------------global variables---------------------------*/
int sleeper = 1, sensor = 2, ledalert = 4, rrelay = 5, yrelay = 6;
int on = 0;


/*-----------------------------user defined functions---------------------*/

/*************************************************************************
Function Name:  sleep (an interrupt function)
Inputs:         none
Return:         none
Operation:      reset system -> all outputs (except yellow relay) = off, 
                and delay before finishing                
**************************************************************************/
void sleep()
  {
  digitalWrite(ledalert, LOW);
  digitalWrite(rrelay, LOW);
  delay(60000);
  }

/*************************************************************************
Function Name:  alert
Inputs:         none
Return:         none
Operation:      indicate power is turned on to the alert circuit.
**************************************************************************/
void alert()
  {
  digitalWrite(ledalert, HIGH);
  } 

/*************************************************************************
Function Name:  setup
Inputs:         none
Return:         none
Operation:      intialise the I/Os and external interrupts 
**************************************************************************/  
void setup()
  {
  pinMode(sensor, INPUT);
  pinMode(ledalert, OUTPUT);
  pinMode(rrelay, OUTPUT);
  pinMode(yrelay, OUTPUT);
  attachInterrupt(sleeper, sleep, FALLING); 
  }

/*************************************************************************
Function Name:  loop
Inputs:         none
Return:         none
Operation:      run main program 
**************************************************************************/
void loop()
  {
  on = digitalRead(sensor);
  if (on==LOW)
    {
    alert(); 
    delay(2000);
    digitalWrite(rrelay, HIGH);
    delay(100000);
    digitalWrite(yrelay, HIGH);
    }
  }
  • I am aware that delay() doesn’t work in interrupt functions but it does the job, if anyone has suggestions for inserting a timed delay there, I would appreciate :slight_smile:
    Thank you!

It all works as expected except that the interrupt runs from the place it finished in loop().

Yes, that's what it's supposed to do. That's the definition of an interrupt.

I want it to restart at the beginning of loop instead.

Then, you need to make sure that nothing else executes after the interrupt has triggered.

After looking at your code, I don't think you need interrupts at all. You need to learn how to do things without using delay(). Look at the blink without delay example.

Imagine that you were doing what you what you want the Arduino to do. You can have either a timer or a watch and piece of paper.

Your current approach uses the timer. Think about how you would do it with the watch and paper.