我的软件无法检测到任何运动[RFID +运动传感器]

时间:2019-08-20 04:53:07

标签: arduino rfid

您好,我目前正在做一个项目,并且花了大约一个月的时间来弄清楚我关于运动传感器无法检测到任何运动的软件问题。
当我组合RC522 RFID阅读器,HC-SR501运动传感器和DHT22温度传感器时,会出现此问题。我的微控制器是Arduino Mega。下面是我的源代码,请务必看一下,我们可以讨论如何一起解决此问题。

#include <SPI.h> //library for the SPI communication
#include <MFRC522.h> //library for the RFID reader
#include <LiquidCrystal.h> //library for the LCD 
#include <dht.h> // library for the temp sensor
dht DHT; // Creates a DHT object
#define SS_PIN 53 // SS Pin of RFID reader to the microcontroller
#define RST_PIN 8 // RST Pin of RFID reader to the microcontroller 
#define dataPin 13 // Defines pin number to which the sensor is connected
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
int inputPin = 9;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; // LCD connection to the microcontroller
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() 
{ lcd.begin(20, 4); // Initiate the LCD
  delay(1500);
  SPI.begin();                  // Initiate  SPI bus
  mfrc522.PCD_Init();           // Initiate MFRC522
  pinMode(inputPin, INPUT);     // declare sensor as input
}
void loop() 
{ val = digitalRead(inputPin);            // read input value
  int readData = DHT.read22(dataPin);     // DHT22/AM2302
  float t = DHT.temperature;             // Gets the values of the temperature
  float h = DHT.humidity;               // Gets the values of the humidity
  lcd.setCursor(0,0);                  // set the cursor to the first row and column
  lcd.print("Temp = ");              // Printing to the LCD
  lcd.print(t);                     // Printing the measurement result
  lcd.print((char)223);            // Printing the degree symbol
  lcd.print(" C ");               // Printing to the LCD
  lcd.setCursor(0, 2);           // set the cursor to the third row and column
  lcd.print("Humid = ");
  lcd.print(h);
  lcd.print(" % ");
  delay(300);
  String content= "";
  byte letter;
   // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) 
     {return;
     }
  // Select one of the cards
  else if ( ! mfrc522.PICC_ReadCardSerial()) 
      {    return;
      }
  for (byte i = 0; i < mfrc522.uid.size; i++) 
      {  content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
         content.concat(String(mfrc522.uid.uidByte[i], HEX));
      }
  content.toUpperCase();


  if (content.substring(1) == "47 B5 89 2C")    //change here the UID of the card/cards that you want to give access
  { lcd.setCursor(0, 3);
    lcd.print("Authorized access");
    delay(1200);
    }

 else if(content.substring(1) != "47 B5 89 2C")
   {lcd.setCursor(0, 3);
    lcd.print("                    ");
    lcd.setCursor(0, 3);
    lcd.print("Access denied");
    delay(1500);
     }

 //val = digitalRead(inputPin);            // read input value
 if (val == HIGH)  // check if the input is HIGH
          { if (pirState == LOW) // No motion 
               {
                lcd.setCursor(0,1);
                lcd.print("Motion detected!"); // print on output change
                pirState = HIGH;
               }
           } 
   else 
           {
            if (pirState == HIGH) // after it have a motion
                { 
                  lcd.setCursor(0,1);
                  lcd.print(" test                "); 
                  lcd.setCursor(0,1);
                  lcd.print("Motion ended!");  // print on output change
                  pirState = LOW;
                }
            }
} 

我真的需要有关如何解决此问题的指导,因为这是我的最后一年的项目。

0 个答案:

没有答案