External Interrpt in nRF52832

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Pratyusha_duvvi
 
Posts: 10
Joined: Tue Aug 02, 2022 6:56 am

External Interrpt in nRF52832

Post by Pratyusha_duvvi »

Hi,
[
#include <Arduino.h>

#define int_pin1 13
#define led 17

void handle_irq3(void);

void handle_irq3(void) {
Serial.println("interrupt started");
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
}

void setup() {
Serial.begin(9600);
Serial.println("External Interrupter ");
pinMode(led, OUTPUT);
pinMode(int_pin1, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(int_pin1), handle_irq3, FALLING);
}

void loop() {
Serial.println("interrupt ");
delay(500);
}
]

Hi, here I am using nrf52832 controller, above is the example code for external interrupt but unfortunately it is not working properly. When button is pressed the code stops inside the handle_irq3 function, led blink is not happening. Is there anything wrong inside the ISR function?? Refer the below attached output image

Thanks
Attachments
Screenshot from 2022-08-02 17-48-57.png
Screenshot from 2022-08-02 17-48-57.png (133.51 KiB) Viewed 148 times
Last edited by Pratyusha_duvvi on Tue Aug 02, 2022 8:33 am, edited 2 times in total.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: External Interrpt in nRF52832

Post by adafruit_support_bill »

Serial I/O and delays are both problematic in an ISR. ISR code should be quick and do minimal work. Interrupts are typically disabled on entry, so you should avoid anything that requires the use of interrupts (such as serial I/O and delay).

One strategy that works for many cases is to simply set a flag and record whatever state is necessary, then process that on your next pass through the main loop.

User avatar
Pratyusha_duvvi
 
Posts: 10
Joined: Tue Aug 02, 2022 6:56 am

Re: External Interrpt in nRF52832

Post by Pratyusha_duvvi »

Thanks for your response. I also tried with flag but the problem is the interrupt process will only execute after the current process. what i need is when button pressed it should terminate the current process and execute the interrupt process.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: External Interrpt in nRF52832

Post by adafruit_support_bill »

An interrupt does not terminate anything. It just interrupts. The main loop will continue once you return from the interrupt.

Please post the code you are using,.

User avatar
Pratyusha_duvvi
 
Posts: 10
Joined: Tue Aug 02, 2022 6:56 am

Re: External Interrpt in nRF52832

Post by Pratyusha_duvvi »

[

Code: Select all

#include <Arduino.h>

#define int_pin1 13
#define led 17
#define led1 19
#define led2 18

void interrupt();
void blink();
void ledBlinking();

bool pressButton = false;

void setup() {
  Serial.begin(9600);
  Serial.println("External Interrupter  ");
  pinMode(led, OUTPUT);
  pinMode(int_pin1, INPUT_PULLUP);
}

void loop() {
  ledBlinking();
  interrupt();
  if(pressButton){
    Serial.println("button pressed");
    delay(500);
    Serial.println("button Released");
    delay(500);
    pressButton = false;
  }
}

void interrupt()
{
  attachInterrupt(digitalPinToInterrupt(int_pin1), blink, FALLING);
}

void blink(void) {
  if(digitalRead(int_pin1) == LOW)
  {
    pressButton = true;
  }
}

void ledBlinking(){
  for(int i=1; i<= 10; i++){
  digitalWrite(led1, HIGH);
  delay(500);
  digitalWrite(led1, LOW);
  delay(500);
  }
}
]

Actually when i press button it should stop ledBlinking() and execute interrupt, after that the controller continue the current process but as per the above code when i press the button the interrupt will execute only after completing ledBlinking().
Last edited by adafruit_support_bill on Wed Aug 03, 2022 5:29 am, edited 1 time in total.
Reason: Please use [code] tags when posting code to the forums.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: External Interrpt in nRF52832

Post by adafruit_support_bill »

the problem is that you are using delay() for timing. The delay() function prevents your code from being responsive to external events and is incompatible with any kind of real-time programming. You should use millis() instead for timing.

https://learn.adafruit.com/multi-taskin ... ino-part-1
https://learn.adafruit.com/multi-taskin ... ino-part-2
https://learn.adafruit.com/multi-taskin ... ino-part-3

Also, you only need to attach the interrupt once. You should move that code to your setup() function.

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”