GPIO interrupt for 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

GPIO interrupt for nrf52832

Post by Pratyusha_duvvi »

Hi, here i am using nrf52832 controller. I am putting controller in sleep mode for 10 seconds using __wfi(); during that sleep if i press button it should perform interrupt and it will continue the loop. But here after sleep (10 sec) only interrupt is performing. Sample code is below

Code: Select all

#include <Arduino.h>
#define led 17
#define led1 19
#define button 14
unsigned long previousMillis = 0;
unsigned long prevMillis = 0;
const int interval = 500;
const int interval1 = 1000;
int ledState = LOW;
int ledState1 = LOW;
 volatile bool buttonpresed = false;
void ledblink();
void ISR();
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(led ,OUTPUT);
  pinMode(button,INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(button),ISR,FALLING);
}
void loop()
{
  // put your main code here, to run repeatedly:
  int state = 0;
  int sleeptime = 0;
  while (1)
  {
    if (buttonpresed)
    {
      Serial.println("Button pressed");
      int i = 0;
      while (i < 10)
      {
        unsigned long currMillis = millis();
        if (currMillis - prevMillis >= interval1)
        {
          i++;
          prevMillis = currMillis;
          // if the LED is off turn it on and vice-versa:
          if (ledState1 == LOW)
          {
            ledState1 = HIGH;
          }
          else
          {
            ledState1 = LOW;
          }
          digitalWrite(led1, ledState1);
        }
      }
      buttonpresed = false;
    }
    switch (state)
    {
    case 0:
      for(int x =0;x<5;x++)
      {
      ledblink();
      }
      state = 1;
      break;
    case 1:
    sleeptime = 0;
    Serial.println("Button pressed1");
      while (sleeptime < 10000)
      {
        __WFI();
        sleeptime++;
      }
      Serial.println("Button pressed2");
      state = 0;
      break;
    default:
      break;
    }
  }
}
  void ledblink()
  {
      digitalWrite(led, HIGH);
      delay(500);
      digitalWrite(led,LOW);
      delay(500);
    }
void ISR()
{
  buttonpresed = true;
}

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: GPIO interrupt for nrf52832

Post by adafruit_support_mike »

There are a lot of nested loops in your code, which could be causing control issues.

Try dropping back to a simple

Code: Select all

void loop () {
    if ( buttonpressed ) {
        buttonpressed = false;
        Serial.print( "Caught an interrupt" );
    }
}
main loop so you can just test the interrupt.

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

Return to “General Project help”