RFID + PIR SENSOR

Hello ,

Quick question. I have an Arduino uno with ethernet shield 2 + I have a PIR SENSOR attached on pin 2 ( wich is an interrupt pin).

I found a bug in the program but i'm not sure how to solve this. When the program first sees motion detection when I open the serial monitor it doesn't listen anymore to any RFID cards anymore.

But when I wait and first put an RFID tag to the reader and then trigger my motion sensor only then it can combine both signals together.

/*

   All the resources for this project: https://www.hackster.io/Aritro
   Modified by Aritro Mukherjee


*/
#include <Ethernet2.h> //Load Ethernet Library
#include <EthernetUdp2.h> //Load UDP Library
#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 4
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEE};
IPAddress ip(192, 168, 0, 220); //Assign my IP adress , not necessary
unsigned int localPort = 5006; //Assign a Port to talk over
EthernetUDP Udp; //Define UDP Object

int inputPin = 2;
int pirState = LOW;
int val = 0;

void setup()
{
  Ethernet.begin(mac, ip); // Begins Ethernet
  Udp.begin(localPort); // Makes UDP connection possible on the assigned port
  Serial.begin(9600);   // Initiate a serial communication
  pinMode(inputPin, INPUT); // Tell the arduino to make pin 2 an INPUT
  attachInterrupt(digitalPinToInterrupt(inputPin), detection, CHANGE); // When there is a change on pin 2 the detection code
  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
  Serial.println("Approximate your card to the reader...");
  Serial.println();
}
void loop()
{
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent())
  {
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial())
  {
    return;
  }
  //Show UID on serial monitor
  Serial.print("UID tag :");
  String content = "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++)
  {
    Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
    Serial.print(mfrc522.uid.uidByte[i], HEX);
    content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : ""));
    content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  Serial.println();
  Serial.print("Message : ");
  content.toUpperCase();

  Serial.println("Sending to Python");
  Serial.println();
  Udp.beginPacket("192.168.0.219", 5005); // Begin packet with the client
  Udp.print(content); // Send the string
  Udp.endPacket(); // Terminate packet
  delay(3000);
}

void detection() { // the function "detection"
  val = digitalRead(inputPin); // reads the digital state on pin 2 ( HIGH OR LOW )
  if (val == HIGH) { // if this value is HIGH
    if (pirState == LOW) { // And the variable is LOW
      Serial.println("Motion detected!"); // Then there is motion , write this to the serial monitor
      Udp.beginPacket("192.168.0.219", 5005); // Begin packet with the client
      Udp.write("trigger"); // Send the string
      Udp.endPacket(); // Terminate packet
      pirState = HIGH; // Make variable HIGH
    }
  }
  else {
    if (pirState == HIGH) { // if the variable is HIGH
      Serial.println("Motion ended!"); // Then there is no detection
      pirState = LOW; // Make variable LOW
    }
  }
}
void detection() { // the function "detection"
  val = digitalRead(inputPin); // reads the digital state on pin 2 ( HIGH OR LOW )
  if (val == HIGH) { // if this value is HIGH
    if (pirState == LOW) { // And the variable is LOW
      Serial.println("Motion detected!"); // Then there is motion , write this to the serial monitor
      Udp.beginPacket("192.168.0.219", 5005); // Begin packet with the client
      Udp.write("trigger"); // Send the string
      Udp.endPacket(); // Terminate packet
      pirState = HIGH; // Make variable HIGH
    }
  }
  else {
    if (pirState == HIGH) { // if the variable is HIGH
      Serial.println("Motion ended!"); // Then there is no detection
      pirState = LOW; // Make variable LOW
    }
  }
}

You should NOT use Serial or Udp inside an Interrupt Service Routine. Your ISR should set a flag and then you can check the flag in loop().

boolean DetectionChanged = false;

void detection() {DetectionChanged = true;}


void loop() {
  if (DetectionChanged) {
    DetectionChanged = false;
  val = digitalRead(inputPin); // reads the digital state on pin 2 ( HIGH OR LOW )
  if (val == HIGH) { // if this value is HIGH
    if (pirState == LOW) { // And the variable is LOW
      Serial.println("Motion detected!"); // Then there is motion , write this to the serial monitor
      Udp.beginPacket("192.168.0.219", 5005); // Begin packet with the client
      Udp.write("trigger"); // Send the string
      Udp.endPacket(); // Terminate packet
      pirState = HIGH; // Make variable HIGH
    }
  }
  else {
    if (pirState == HIGH) { // if the variable is HIGH
      Serial.println("Motion ended!"); // Then there is no detection
      pirState = LOW; // Make variable LOW
    }
  }
 //  Put the rest of loop() here...
}

Thank you so much johnwasser !