/* //MIRRU IN MIRRU OUT //CODE TO CONTROL OPENING SYSTEM RFID+SERVO MOTOR+BLUETOOTH //latest version //17 June 2019 Pamela Martello */ //For Bluetooth communication// #include SoftwareSerial BT(6,7); //TXD,RXD pin for Bluetooth module #include //peripheral communication #include ///RFID library #include // Servo library #define SS_PIN 10 // RFID PINS #define RST_PIN 9 //RFID PINS #define ACCESS_DELAY 10000 //delay open the lock 10,000millis=10secs #define DENIED_DELAY 1000 //For Bluetooth communication// int state; //the current state int i; int flag=0; //makes sure that the serial only prints once the state MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance Servo myservo; // create servo object to control a servo void setup() { Serial.begin(9600); // Initiate a serial communication BT.begin(115200); //enable bluetooth communication delay(3000); SPI.begin(); // Initiate SPI bus mfrc522.PCD_Init(); // Initiate MFRC522 Serial.println("Put your card to the reader..."); Serial.println(); myservo.attach(3); // attaches the servo on pin 3 to the servo object myservo.write(100); mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max); //If you set Antenna Gain to Max it will increase reading distance it increases about 1-2cm } void loop() { if (BT.available())//condition to find and read BT device //if the bluetooth is available functions { state= BT.read(); //initialize reading flag=0; } if(state=='O'){ //this is the message that will be sent to the bluetooth that will enable the opening myservo.write(0); } else if(state=='C'){ //this is the message that will be sent to the bluetooth that will enable the closing myservo.write(90); } delay(200);//delay between pressing open/close button // Look for new cards-this is the loop for the RFID tag reader 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(); if (content.substring(1) == "09 5E BB 59") // MIRRU NECKLACE 09 5E BB 59 change here the UID of the card/cards that you want to give access (to know the code of your tag use MFRC522 examples inside MFRC522>DumpInfo) { Serial.println("Authorize mirru access"); Serial.println(); delay(20); myservo.write(0);// lock position delay(ACCESS_DELAY); //delay to keep the lock open and let the cat go in myservo.write(90); // unlock position delay(ACCESS_DELAY); } else { Serial.println(" CATccess denied"); // delay(DENIED_DELAY); } }