Back to normal loop after Interrupt

Hi gang,

I am having some trouble with writing some code. The example I have gotten out of an educational book for beginners and it introduces interrupts. The example itself only included code that would detect an interruption and nothing else. I wanted to test out some of the knowledge I have learnt and to write something that would allow me to return to the main loop after interruption had occurred.

I could only think of a situation where there would be 3 states:

  1. Normal before any interruption
  2. Interrupted state & the use of the serial monitor to communicate said state
  3. Revert back to normal state

I have used some wire between pin 2 and GND to simulate the interruption when a state change has occurred i.e. when the arduino picks up a signal. I have used the Serial monitor and a Serial.println function to communicate an interrupted state and have used the frequency of this to determine how long the arduino shall remain in an interrupted state before going back to the main loop.

I think my code can only get back to a state which resembles (3. Revert back to normal state) as I cannot trigger another interrupt after the initial.

I would appreciate any help I can get with this one.

My hardware setup:

  • Duinotech Arduino UNO
int interruptPin = 2;
int ledPin = 13; 
int period = 500;
int count = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); // write the LED output pin
  pinMode(interruptPin, INPUT); // write pin 2 as digital input
  digitalWrite(interruptPin, HIGH); // pull - up to set the state 
  attachInterrupt(0, goFast, FALLING); // interrupt function which recognizes a falling interrupt signal 
}

void loop()
{ // Section A: Uninterrupted state prior to any interruption occurring
  digitalWrite(ledPin, HIGH); 
  delay(period);
  digitalWrite(ledPin, LOW);
  delay(period);
  while (period < 500) // Section B: To communicate an interrupted state 
  {
    digitalWrite(ledPin, HIGH);
    delay(period);
    digitalWrite(ledPin, LOW);
    delay(period);
    Serial.println("INTERRUPTED"); 
    count ++;
    if (count == 10) // To revert back to an uninterrupted state after interruption has occurred  
    {
      period = 500;
      digitalWrite(ledPin, HIGH);
      delay(period);
      digitalWrite(ledPin, LOW);
      delay(period);
    }
  }
   
}

void goFast()
{
  period = 100;
}

Best,
bedroomdeejay

bedroomdeejay:
2. Interrupted state & the use of the serial monitor to communicate said state

Don't do serial print inside the Interrupt Service Routine. In the ISR set a (volatile) boolean flag and monitor that flag somewhere in the main code. Once the interrupt has been acted upon clear the flag.

write something that would allow me to return to the main loop after interruption had occurred.

Sure, it's

}

bedroomdeejay:
to determine how long the arduino shall remain in an interrupted state before going back to the main loop.

The code in an ISR should execute so quickly (100 microseconds would be a very long time) so that human-scale measurement is impractical.

An ISR always returns control the the statement immediately after the statement at which the interrupt occurred. You cannot make it return to a different place. (Unless you have a master's degree in computer science).

...R

Hello,
I think the attachInterrupt call is wrong, must be as following

attachInterrupt(digitalPinToInterrupt(interruptPin), goFast,FALLING)

Thanks

abdelhmimas:
Hello,
I think the attachInterrupt call is wrong, must be as following

attachInterrupt(digitalPinToInterrupt(interruptPin), goFast,FALLING)

Thanks

It's not wrong, interrupt 0 is the correct number for pin 2 on the UNO. However, that is not the preferred way to do it. The omission is in the old book the OP is using. It's always best to consult current documentation.

That’s not what this says

The serial stops working because pin 0 called in the interrupt routine is connected to the uno serial port.
Regards

abdelhmimas:
attachInterrupt() - Arduino Reference

That’s not what this says

Actually, it does say that:
"
Board int.0 int.1
Uno, Ethernet
2 3
"

You can, and should use digitalPinToInterrupt(interruptPin), but it is not mandatory as you said, "must". The code as written will run correctly (at least, that part will).

I always used pin2 and 3 on uno, nano and it worked.
Please see the example code they gave on this link

abdelhmimas:
I always used pin2 and 3 on uno, nano and it worked.
Please see the example code they gave on this link

What worked? Perhaps I should clarify, when using pin 2 on an Uno

attachInterrupt(digitalPinToInterrupt(2), goFast,FALLING)

and

attachInterrupt(0, goFast,FALLING)

are exactly equivalent, and will both work

I never tested this way, but if you said it is equivalent that’s OK.
Cheers

Robin2:
An ISR always returns control the the statement immediately after the statement at which the interrupt occurred. You cannot make it return to a different place. (Unless you have a master's degree in computer science).

...R

Hello,
Thanks for your response. Just to clarify - what you are describing would be most ideal as the ISR would control back to the main loop? But how do you get the ISR to return control? can I do some more with the ISR function itself?

Oh and the local uni is only offering - "learn to code in 6 month" courses. Afraid ill have to compensate for lack of expertise :sunglasses:

Best

But how do you get the ISR to return control?

See reply #3 :wink: The closing } of the ISR is the end and returns the control. This is the same as the last } of functions.

For an ISR, it translates to a RETI (return from interrupt) instruction in assembly, for a function it translates to a RET (return) instruction in assembly.

I don't understand what you mean by

what you are describing would be most ideal as the ISR would control back to the main loop? But how do you get the ISR to return control?

how does "control back" work?
do you mean the code jumps back from the ISR to the main code?

how does "return control" work
do you mean
how does the ISR gets executed again?

attach-interrupt to an IO-pin means:
whenever the chosen event of "RISING" or "FALLING" or "CHANGE" happens the ISR gets executed
if you code

attachInterrupt(digitalPinToInterrupt(2), goFast,FALLING)

whenever the voltage goes from 5V to 0V = HIGH-LOW = FALLING the ISR gets executed.
Completely regardless of what your main-code is doing in this moment.
and as soon as the ISR has finished your main-code goes on with the next command

only exception is of you code noInterrupts(). This disables all interrupts
best regards Stefan

bedroomdeejay:
But how do you get the ISR to return control?

You don't have to do anything - it happens automatically when the ISR code finishes.

can I do some more with the ISR function itself?

What do you mean by "more"?

An ISR function is just a regular function. However you should ensure that the code within it completes very quickly. 100 microsecs would be a long time.

...R

you should ensure that the code within it completes very quickly

and that it does not contain any code that relies on interrupts for correct operation (such as Serial.print()) as they are automatically disabled on entry to the ISR and automatically enabled again on exit from it

bedroomdeejay:
Hello,
Thanks for your response. Just to clarify - what you are describing would be most ideal as the ISR would control back to the main loop? But how do you get the ISR to return control? can I do some more with the ISR function itself?

Oh and the local uni is only offering - "learn to code in 6 month" courses. Afraid ill have to compensate for lack of expertise :sunglasses:

Best

Actually, in the design of a CPU, there is an address counter that stores the address of all the instructions while executing.
When there is an ISR, the address where the program was is saved, the CPU will execute the ISR and return back to the next instruction address.

Hi all,

Thank you for all your responses, I am now thoroughly confused. I will need to do some work on it to better describe the problem and if I'm lucky the solution.

Thanks again for the information have a safe and happy new year!

Bedroomdeejay