Having problem using with inturrupt

hi.

I have a problem with my project.

I want to put inturrupt in my code but it always ended with fleezing.

and still I couldnt figured out what is wrong with the code.

Can someone help me?

My code is below :

// signal the nRF24 if the PIR senses nearby person
// LEDs and Serial lines are for the test

#include  <util/atomic.h>
#include  <SPI.h>                                                 //   Include SPI Code Library which can be downloaded below
#include  <nRF24L01.h>                                            //   Include NRF24L01 Code Library which can be downloaded below
#include  <RF24.h>                                                //   Inlcude NRF24 Code Library which can be downloaded below

volatile byte changed = 0;
 
int msg[1];
const int PIR    = 2;                                             //   Declare PIR pin
const int red    = 6;                                             //   Declare red LED pin
const int green  = 5;                                             //   Declare green LED pin
const int blue   = 4;                                             //   Declare blue LED pin

const int value1 = 0;                                             //   

RF24 radio(7,8);                                                  //   The NRF24L01 Pin CE and Pin CSN
const uint64_t pipe = 0xA8E8F0F0E1LL;                             //   Communication Pip Address
                    
volatile int PIRstate = 0;


void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
  pinMode(PIR, INPUT);
  pinMode(green, OUTPUT);
  pinMode(red, OUTPUT);
  pinMode(blue, OUTPUT);                                          //  PIR
  attachInterrupt(digitalPinToInterrupt(2), toggle, RISING);
}

void loop() {
    Serial.println("Waiting..");
    digitalWrite(blue, HIGH);
    delay(200);
    digitalWrite(blue, LOW);
  if (changed == 1) {
    // toggle() has been called from interrupts!

    // Reset changed to 0
    changed = 0;

    Serial.println("Waiting..");
    digitalWrite(blue, HIGH);
    delay(200);
    digitalWrite(blue, LOW);
  }
}

void toggle() {
 
  msg[0] = 111;                                                  //  Send the 111 to the reciever 
  radio.write(msg, 1);
  digitalWrite(blue, LOW); 
  digitalWrite(red, LOW); 
  digitalWrite(green, HIGH); 
  changed = 1;
}

 //reference
 
 //nRF24L01
 //https://m.blog.naver.com/roboholic84/221139363425
 //https://www.14core.com/wiring-the-nrf24l01-2-4ghz-radio-as-remote-switching/
 //https://blog.naver.com/twophase/221122054270
 //https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=twophase&logNo=221122351663
 //https://m.blog.naver.com/twophase/221132324612
 
 //PIR
 //https://fishpoint.tistory.com/6399
 
 //Ultra sonic
 //https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=boilmint7&logNo=220926404472

 //Interrupt function
 //https://mechanic-workshop.tistory.com/m/16
 //https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=roboholic84&logNo=220421863761
 //https://juahnpop.tistory.com/84
 //https://engineerparkbro.tistory.com/30
 //https://makersportal.com/blog/2019/5/27/arduino-interrupts-with-pir-motion-detector
 //https://www.allaboutcircuits.com/technical-articles/using-interrupts-on-arduino/
 //https://forum.arduino.cc/t/problem-using-sleep-mode/695188/3
 //https://www.arduino.cc/reference/en/language/variables/variable-scope-qualifiers/volatile/

Never do serial I/O in an interrupt routine, as that depends on interrupts, which are turned off.

Instead, set a flag declared "volatile", exit the interrupt and do the I/O in the main program.

1 Like

As a general rule it's not a good idea to use libraries within an interrupt routine.. Set a flag and then do your work outside of the interrupt routine.

You could try just moving all the code, except the 'changed = 1;' out of the interrupt...

:smiley_cat:

1 Like

Thanks for your advice! :heart: I really appreciated. :smiley:

The same holds true for the SPI accesses used by the above routine.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.