#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
#define trigPin 4
#define echoPin 2
#define distanceThreshold 30
int RST_PIN = 9;
int SS_PIN = 10;
int servoPin = 8;
long duration, distance;
bool barrierUp = false;
Servo motor;
MFRC522 rfid(SS_PIN, RST_PIN);
byte ID[4] = {155, 58, 148, 34};
void setup() {
motor.attach(servoPin);
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
SPI.begin();
rfid.PCD_Init();
}
void loop() {
if (!rfid.PICC_IsNewCardPresent()) {
return;
}
if (!rfid.PICC_ReadCardSerial()) {
return;
}
if (rfid.uid.uidByte[0] == ID[0] &&
rfid.uid.uidByte[1] == ID[1] &&
rfid.uid.uidByte[2] == ID[2] &&
rfid.uid.uidByte[3] == ID[3]) {
Serial.println("Kapi acildi");
ekranaYazdir();
if (!barrierUp) {
motor.write(180);
barrierUp = true;
} else {
motor.write(50);
barrierUp = false;
}
delay(3000);
} else {
Serial.println("Yetkisiz Kart");
ekranaYazdir();
}
rfid.PICC_HaltA();
}
void ekranaYazdir() {
Serial.print("ID Numarasi: ");
for (int sayac = 0; sayac < 4; sayac++) {
Serial.print(rfid.uid.uidByte[sayac]);
Serial.print(" ");
}
Serial.println("");
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.0343 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
if (distance < distanceThreshold) {
motor.write(50);
barrierUp = false;
}
}