Advertisement
myarkqub

Sketch_01

Sep 27th, 2018
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Viral Science
  2. //RFID
  3. #include <SPI.h>
  4. #include <MFRC522.h>
  5.  
  6.  
  7. #define SS_PIN 10
  8. #define RST_PIN 9
  9. #define LED_G 4 //define green LED pin
  10. #define LED_R 5 //define red LED
  11. #define LED_detect 3; //led when car detected
  12. #define BUZZER 2 //buzzer pin
  13. const int trigPin = 7;
  14. const int echoPin = 6;
  15. long duration;
  16. int distance;
  17. int detectCard = 0;
  18. MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
  19.  
  20. void setup()
  21. {
  22.   Serial.begin(9600);   // Initiate a serial communication
  23.   SPI.begin();      // Initiate  SPI bus
  24.   mfrc522.PCD_Init();   // Initiate MFRC522
  25.   pinMode(LED_G, OUTPUT);
  26.   pinMode(LED_R, OUTPUT);
  27.   pinMode(LED_detect, OUTPUT);
  28.   pinMode(BUZZER, OUTPUT);
  29.   pinMode(trigPin, OUTPUT);
  30.   pinMode(echoPin, INPUT);
  31.   noTone(BUZZER);
  32.   Serial.println("Put your card to the reader...");
  33.   Serial.println();
  34.  
  35. }
  36. void loop()
  37. {
  38.   digitalWrite(trigPin, LOW);
  39.   delayMicroseconds(2);
  40.   digitalWrite(trigPin, HIGH);
  41.   delayMicroseconds(10);
  42.   digitalWrite(trigPin, LOW);
  43.   duration = pulseIn(echoPin, HIGH);
  44.   distance = duration*0.034/2;
  45.  
  46.  
  47.  
  48.   if(distance >= 5 || distance <= 10){
  49.  
  50.     //detect car
  51.     digitalWrite(LED_detect, HIGH);
  52.     delay(3000);
  53.     digitalWrite(LED_detect, LOW);
  54.    
  55.  
  56.   // Look for new cards
  57.   if ( ! mfrc522.PICC_IsNewCardPresent())
  58.   {
  59.     return;
  60.   }
  61.   // Select one of the cards
  62.   if ( ! mfrc522.PICC_ReadCardSerial())
  63.   {
  64.     return;
  65.   }
  66.   //Show UID on serial monitor
  67.   Serial.print("UID tag :");
  68.   String content= "";
  69.   byte letter;
  70.   for (byte i = 0; i < mfrc522.uid.size; i++)
  71.   {
  72.      Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
  73.      Serial.print(mfrc522.uid.uidByte[i], HEX);
  74.      content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
  75.      content.concat(String(mfrc522.uid.uidByte[i], HEX));
  76.   }
  77.   Serial.println();
  78.   Serial.print("Message : ");
  79.   content.toUpperCase();
  80.   if (content.substring(1) == "D4 00 A8 1E") //change here the UID of the card/cards that you want to give access
  81.   {
  82.     detectCard = 1;
  83.     noTone(BUZZER);
  84.     digitalWrite(LED_R, LOW);
  85.     digitalWrite(LED_G, LOW);
  86.   }
  87.   if(detectCard == 0){
  88.     digitalWrite(LED_R, HIGH);
  89.     digitalWrite(LED_G, HIGH);
  90.     tone(BUZZER, 300);
  91.     delay(1000);
  92.     }
  93.   }
  94.   else{
  95.     noTone(BUZZER);
  96.     detectCard = 0;
  97.     digitalWrite(LED_R, LOW);
  98.     digitalWrite(LED_G, LOW);
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement