Hi everybody, i tried to a solution on web especially at forum.arduino.cc but i couldn't find so i decide to start a new topic.
My project is that :
I make a model house, little one. Out of door, i have a rfid card reader and a id card. I set up all functions of RFID library, i can read id card, open door with servo motor,close after 5 seconds. I try to make a burglar system, i can detect abnormal access which is not done use door(burglar).
But problem is that, when i do a normal access to home with my id card, i have to push a button in 10 seconds to close alarm system (pır motion sensor and buzzer). If i don't push the button in 10 seconds alarm will work and buzzer will sound.
My code is here :
//Kart oku_LCD_SERVO
#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
#include <LiquidCrystal.h>
const int rs = 47, en = 46, d4 = 45, d5 = 44, d6 = 43, d7 = 42;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
#define SS_PIN 53
#define RST_PIN 5
#define PIR 10
#define Buzzer 9
#define buton 2
int PIR_deger;
int buton_deger;
unsigned long ilkZaman;
unsigned long sonZaman;
unsigned long zaman;
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
Servo myServo; //define servo name
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
myServo.attach(8); //servo pin!!!!
myServo.write(0); //servo start position
///led buz
lcd.setCursor(0, 0);
lcd.println("Karti Okutun : "); // Read card
pinMode(Buzzer, OUTPUT);
pinMode(PIR,INPUT);
pinMode(buton, INPUT);
digitalWrite(Buzzer,LOW);
}
void loop()
{
PIR_deger=digitalRead(PIR);
Serial.println(PIR_deger);
delay(1000);
if( ( ! mfrc522.PICC_ReadCardSerial() ) && PIR_deger ==HIGH)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.println("HIRSIZZZZ!!! "); // There are unwanted access.
lcd.setCursor(0, 1);
lcd.println("VARRR!!!!!!!! ");
digitalWrite(Buzzer,HIGH);
delay(3000);
digitalWrite(Buzzer,LOW);
}
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return ;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return ;
}
//Show UID on lcd
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("UID tag :");
delay(1500);
String content= "";
byte letter;
lcd.setCursor(0, 1);
for (byte i = 0; i < mfrc522.uid.size; i++)
{
lcd.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
lcd.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
delay(1500);
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.println("Sonuc : ");
content.toUpperCase();
PIR_deger=digitalRead(PIR); // PIR_deger = value of pır
Serial.println(PIR_deger);
delay(2000);
if (content.substring(1) == "AE 4C 54 20") // Is it aviable idendity card?
lcd.setCursor(8, 0);
lcd.println("Giriniz ");
delay(500);
myServo.write(180); // Door open
delay(5000);
myServo.write(0); // Door close
if(buton_deger == LOW) // buton deger= value of button, high or low
{
ilkZaman=millis(); // Measure (ilkZaman = firsttime) time when buton is low
Serial.print("ilk zaman : "); // Write on the Serial Port to see whether is it true.
Serial.println(ilkZaman);
}
/* PIR_deger=digitalRead(PIR);
Serial.print("PIRRR : ");
Serial.println(PIR_deger);
delay(500);*/
buton_deger = digitalRead(buton); // Is button high? (buton_deger = situation of button?
// delay(30);
if( buton_deger == HIGH ) // It is not wait to read button is high, it is keep going to down, i measure all time 0 ms.
{
sonZaman=millis();
Serial.print("Son zaman : ");
Serial.println(sonZaman);
zaman= (sonZaman-ilkZaman); // zaman = time -- time= LastTime-FirstTime i mean it is elapsed time between high and low
}
Serial.println(zaman);
if(buton_deger == HIGH && zaman<=10000) // button is high && time<10000 ms = 10 seconcds
{
PIR_deger = LOW; // turn off pır motion sensor
Serial.println(PIR_deger);
delay(1000);
}
if(buton_deger == HIGH && zaman>10000) button is high but i didn't push within 10 second
{
PIR_deger = HIGH; // if i push after 10 seconds, pır will high and buzzer will sound.
Serial.println(PIR_deger);
delay(1000);
digitalWrite(Buzzer,HIGH);
delay(3000);
digitalWrite(Buzzer, LOW);
}
}
// Down is not important, i have second card and it will work to show wrong id card.
if(content.substring(1) != "AE 4C 54 20")
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.println("Sonuc : ");
lcd.setCursor(8,0);
lcd.print("Gecersiz");
digitalWrite(Buzzer,HIGH);
delay(100);
digitalWrite(Buzzer,LOW);
delay(100);
digitalWrite(Buzzer,HIGH);
delay(100);
digitalWrite(Buzzer,LOW);
delay(1500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.println("Karti Okutun : ");
}
}
Main problem is that, if(button ==HIGH) loop is not waiting, and i miss it. So i measure elapsed time between high and low time is zero.
Thanks for all your helps. I read how to use this forum and start a new topic, if i made a mistake please tell me, i will fix immediately.