|
| 1 | +/* |
| 2 | +##################################################################################### |
| 3 | +# File: RFID_Simples_V3.ino |
| 4 | +# micro controller: Arduino UNO ou Teensy 2.0++ |
| 5 | +# Language: Wiring / C /Processing /Fritzing / Arduino IDE |
| 6 | +# Version: 1.0 |
| 7 | +# |
| 8 | +# Objectives: Arduino RFID - Security System and Access Control |
| 9 | +# |
| 10 | +# Operation: Using the RFID MFCR-522AN reader, we can do the personal access control |
| 11 | +# to an specific environment. |
| 12 | +# To do this we need to check the card or tag ID |
| 13 | +# If a valid ID is found it is possible to lit a LED, play a soundand drive a servo-motor |
| 14 | +# to open the door or gate - if an invalid ID is found, it is possible to do the access rejection |
| 15 | +# flashing a red LED and playing an alert sound and locking the door. |
| 16 | +# A LCD (20 columm x 4 rows shos us the system messages |
| 17 | +# |
| 18 | +# |
| 19 | +# Author: Marcelo Moraes |
| 20 | +# Date: 06/16/15 |
| 21 | +# Place: Sorocaba - SP, Brazil |
| 22 | +# |
| 23 | +##################################################################################### |
| 24 | + |
| 25 | +This project contains public domain code. |
| 26 | +The modification is permitted without notice. |
| 27 | +
|
| 28 | +
|
| 29 | + ******PINS FOR ARDUINO********* |
| 30 | + *** Reset = Pin 5 *** |
| 31 | + *** SDA = Pin 10 *** |
| 32 | + *** MOSI = Pin 11 *** |
| 33 | + *** MISO = Pin 12 *** |
| 34 | + *** SCK = Pin 13 *** |
| 35 | + *** GND = GROUND *** |
| 36 | + *** 3.3 = 3.3v *** |
| 37 | + |
| 38 | + ****************************************** |
| 39 | + |
| 40 | +*/ |
| 41 | + |
| 42 | + |
| 43 | +// libraries |
| 44 | +#include <SPI.h> |
| 45 | +#include <RFID.h> |
| 46 | +#include <Servo.h> |
| 47 | +#include "pitches.h" |
| 48 | +#include <LiquidCrystal_I2C.h> |
| 49 | +#include <Wire.h> |
| 50 | + |
| 51 | +// RFID definition |
| 52 | +RFID rfid(10,5); |
| 53 | + |
| 54 | +// 5 data bytes from cards |
| 55 | +// you can read these bytes on the Serial console |
| 56 | +byte emaomos[5] = {0xDE,0x1F,0x47,0xC9,0x4F}; // Marcelo Moraes (e+mao+mos) |
| 57 | +byte newuser1[5] = {0xB5,0x3B,0x14,0xB7,0x2D}; //new User 1 Flora |
| 58 | +byte newuser2[5] = {0x01,0x9A,0x92,0x2b,0x23}; //new User 2 |
| 59 | +// Add allowed card IDs here |
| 60 | + |
| 61 | +// // LCD address and type declaration |
| 62 | +LiquidCrystal_I2C lcd(0x27,20,4); |
| 63 | + |
| 64 | +byte serNum[5]; |
| 65 | +byte data[5]; |
| 66 | +int cardRead; // card read 1 = good 0 = bad for playTune function |
| 67 | +String Name; // used for user name displayMsg function |
| 68 | + |
| 69 | +// Melodies definition: access, welcome and rejection |
| 70 | +int access_melody[] = {NOTE_G4,0, NOTE_A4,0, NOTE_B4,0, NOTE_A4,0, NOTE_B4,0, NOTE_C5,0}; |
| 71 | +int access_noteDurations[] = {8,8,8,8,8,4,8,8,8,8,8,4}; |
| 72 | +int fail_melody[] = {NOTE_G2,0, NOTE_F2,0, NOTE_D2,0}; |
| 73 | +int fail_noteDurations[] = {8,8,8,8,8,4}; |
| 74 | + |
| 75 | +// pins definition - LED, Buzzer and Servo-motor |
| 76 | +int LED_access = 3; |
| 77 | +int LED_intruder = 4; |
| 78 | +int speaker_pin = 8; |
| 79 | +int servoPin = 6; |
| 80 | + |
| 81 | +// servo motor definition |
| 82 | +Servo doorLock; |
| 83 | + |
| 84 | + |
| 85 | +void setup(){ |
| 86 | + doorLock.attach(servoPin); // servo motor attaching |
| 87 | + Serial.begin(9600); // Serial communication initialization |
| 88 | + lcd.init(); // LCD initialization |
| 89 | + lcd.backlight(); |
| 90 | + lcd.clear(); // Clears the LCD display |
| 91 | + SPI.begin(); // SPI communication initialization |
| 92 | + rfid.init(); // RFID module initialization |
| 93 | + Serial.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); |
| 94 | + Serial.println("+ http://arduinobymyself.blogspot.com.br +"); |
| 95 | + Serial.println("+ Arduino RFID Security System using MFRC522-AN +"); |
| 96 | + Serial.println("+ The RFID module is initiate in the automatic read mode, waiting for a valid card... +"); |
| 97 | + Serial.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); |
| 98 | + displayMsgInitial(); |
| 99 | + delay(3000); |
| 100 | + pinMode(LED_access,OUTPUT); |
| 101 | + pinMode(LED_intruder,OUTPUT); |
| 102 | + pinMode(speaker_pin,OUTPUT); |
| 103 | + pinMode(servoPin,OUTPUT); |
| 104 | +} |
| 105 | + |
| 106 | +void loop(){ |
| 107 | + lcd.clear(); |
| 108 | + lcd.noBacklight(); |
| 109 | + // Here you can create a variable for each user |
| 110 | + // NAME_card ou KEY_card |
| 111 | + boolean emaomos_card = true; // that is my card (e + mao + mos)...e + marcelo = mao, moraes = mos |
| 112 | + boolean newuser1_card = true; |
| 113 | + boolean newuser2_card = true; |
| 114 | + //put another users here |
| 115 | + |
| 116 | + if (rfid.isCard()){ // valid card found |
| 117 | + if (rfid.readCardSerial()){ // reads the card |
| 118 | + delay(1000); |
| 119 | + data[0] = rfid.serNum[0]; // stores the serial number |
| 120 | + data[1] = rfid.serNum[1]; |
| 121 | + data[2] = rfid.serNum[2]; |
| 122 | + data[3] = rfid.serNum[3]; |
| 123 | + data[4] = rfid.serNum[4]; |
| 124 | + } |
| 125 | + rfid.halt(); // RFID module to standby |
| 126 | + cardRead = 0; |
| 127 | + displayID_LCD(); |
| 128 | + displayID_Console(); // used for testing purposes only. Prints the Card ID at the serial console |
| 129 | + for(int i=0; i<5; i++){ |
| 130 | + if(data[i] != emaomos[i]) emaomos_card = false; // if it is not a valid card, put false to this user |
| 131 | + if (data[i] != newuser1[i]) newuser1_card = false; |
| 132 | + if (data[i] != newuser2[i]) newuser2_card = false; |
| 133 | + // Here you can check the another allowed users, just put lines like above with the user name |
| 134 | + } |
| 135 | + |
| 136 | + Serial.println(); // used for testing purposes only |
| 137 | + if (emaomos_card){ // if a valid card was found |
| 138 | + cardRead = 1; |
| 139 | + displayMsgOK("Marcelo"); |
| 140 | + playTune(cardRead); |
| 141 | + } |
| 142 | + else if(newuser1_card){ |
| 143 | + cardRead = 1; |
| 144 | + displayMsgOK("Sara"); |
| 145 | + playTune(cardRead); |
| 146 | + } |
| 147 | + else if(newuser2_card){ |
| 148 | + cardRead = 1; |
| 149 | + displayMsgOK("newuser2"); |
| 150 | + playTune(cardRead); |
| 151 | + } |
| 152 | + // another cards analysis, put many blocks like this |
| 153 | + // as many users you have |
| 154 | + |
| 155 | + else{ // if a card is not recognized |
| 156 | + displayMsgNOK(); |
| 157 | + Serial.println("Card not recognized! contact administration!"); // used for testing purposes only |
| 158 | + digitalWrite(LED_intruder, HIGH); // turn on the red LED |
| 159 | + playTune(cardRead); |
| 160 | + delay(1000); |
| 161 | + digitalWrite(LED_intruder, LOW); // turn off the red LED |
| 162 | + } |
| 163 | + if (emaomos_card || newuser1_card || newuser2_card){// add another user using an logical or condition || |
| 164 | + Serial.println("Access Granted!... Welcome to ArduinoByMySelf!"); // used for testing purposes only |
| 165 | + digitalWrite(LED_access,HIGH); // turn on the green LED |
| 166 | + doorLock.write(0); // releases the door, you need to adjust this to positioning the servo according your door locker |
| 167 | + delay(3000); // wait 5 senconds |
| 168 | + doorLock.write(180); // Locks the door, brings the serve to the original position should be adjusted too.... |
| 169 | + digitalWrite(LED_access,LOW); // turn off the green LED |
| 170 | + } |
| 171 | + Serial.println(); // used for testing purposes only |
| 172 | + delay(1000); |
| 173 | + rfid.halt(); // wait till sense a card over the reader |
| 174 | + lcd.noBacklight(); // turn the LCD backlight off |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | + |
| 179 | + |
| 180 | + |
| 181 | + |
| 182 | +//========== Function to play the access granted or denied tunes ========== |
| 183 | +void playTune(int Scan) { |
| 184 | + if (Scan == 1) // A Good card Read |
| 185 | + { |
| 186 | + for (int i = 0; i < 12; i++) //loop through the notes |
| 187 | + { // Good card read |
| 188 | + int access_noteDuration = 1000 / access_noteDurations[i]; |
| 189 | + tone(speaker_pin, access_melody[i], access_noteDuration); |
| 190 | + int access_pauseBetweenNotes = access_noteDuration * 1.30; |
| 191 | + delay(access_pauseBetweenNotes); |
| 192 | + noTone(speaker_pin); |
| 193 | + } |
| 194 | + } |
| 195 | + else // A Bad card read |
| 196 | + for (int i = 0; i < 6; i++) //loop through the notes |
| 197 | + { |
| 198 | + int fail_noteDuration = 1000 / fail_noteDurations[i]; |
| 199 | + tone(speaker_pin, fail_melody[i], fail_noteDuration); |
| 200 | + int fail_pauseBetweenNotes = fail_noteDuration * 1.30; |
| 201 | + delay(fail_pauseBetweenNotes); |
| 202 | + noTone(speaker_pin); |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + |
| 207 | +//========== Display LCD messages for the users ========== |
| 208 | +void displayMsgOK(String user) |
| 209 | +{ |
| 210 | + lcd.backlight(); |
| 211 | + lcd.setCursor(0,0); |
| 212 | + lcd.print(" Welcome "); |
| 213 | + lcd.print(user); |
| 214 | + lcd.setCursor(0,3); |
| 215 | + lcd.print("--------------------"); |
| 216 | + lcd.setCursor(0,2); |
| 217 | + lcd.print(" Access Granted "); |
| 218 | + lcd.setCursor(0,3); |
| 219 | + lcd.print(" Welcome! "); |
| 220 | + |
| 221 | +} |
| 222 | + |
| 223 | +void displayMsgNOK() |
| 224 | +{ |
| 225 | + lcd.backlight(); |
| 226 | + lcd.setCursor(0,0); |
| 227 | + lcd.print("********************"); |
| 228 | + lcd.setCursor(0,1); |
| 229 | + lcd.print("* Contact Admin! *"); |
| 230 | + lcd.setCursor(0,2); |
| 231 | + lcd.print("* Access denied! *"); |
| 232 | + lcd.setCursor(0,3); |
| 233 | + lcd.print("********************"); |
| 234 | +} |
| 235 | + |
| 236 | +void displayMsgInitial() |
| 237 | +{ |
| 238 | + lcd.backlight(); |
| 239 | + lcd.setCursor(0,0); |
| 240 | + lcd.print("********************"); |
| 241 | + lcd.setCursor(0,1); |
| 242 | + lcd.print("* ArduinoByMyself *"); |
| 243 | + lcd.setCursor(0,2); |
| 244 | + lcd.print("* Security System *"); |
| 245 | + lcd.setCursor(0,3); |
| 246 | + lcd.print("********************"); |
| 247 | +} |
| 248 | + |
| 249 | +void displayID_LCD(){ |
| 250 | + lcd.backlight(); |
| 251 | + lcd.setCursor(0,0); |
| 252 | + lcd.print("********************"); |
| 253 | + lcd.setCursor(0,1); |
| 254 | + lcd.print("* ID Found *"); |
| 255 | + //Serial.print("Card found - code:"); |
| 256 | + lcd.setCursor(0,2); |
| 257 | + lcd.print("* "); |
| 258 | + if(rfid.serNum[0] < 16){ |
| 259 | + lcd.print("0"); |
| 260 | + } |
| 261 | + lcd.print(rfid.serNum[0],HEX); |
| 262 | + |
| 263 | + if(rfid.serNum[1] < 16){ |
| 264 | + lcd.print("0"); |
| 265 | + } |
| 266 | + lcd.print(rfid.serNum[1],HEX); |
| 267 | + |
| 268 | + if(rfid.serNum[2] < 16){ |
| 269 | + lcd.print("0"); |
| 270 | + } |
| 271 | + lcd.print(rfid.serNum[2],HEX); |
| 272 | + |
| 273 | + if(rfid.serNum[3] < 16){ |
| 274 | + lcd.print("0"); |
| 275 | + } |
| 276 | + lcd.print(rfid.serNum[3],HEX); |
| 277 | + |
| 278 | + if(rfid.serNum[4] < 16){ |
| 279 | + lcd.print("0"); |
| 280 | + } |
| 281 | + lcd.print(rfid.serNum[4],HEX); |
| 282 | + |
| 283 | + lcd.print(" *"); |
| 284 | + |
| 285 | + lcd.setCursor(0,3); |
| 286 | + lcd.print("********************"); |
| 287 | + delay(1000); // shows the ID for about 1000ms |
| 288 | + lcd.clear(); |
| 289 | + lcd.noBacklight(); |
| 290 | +} |
| 291 | + |
| 292 | + |
| 293 | + |
| 294 | +void displayID_Console(){ |
| 295 | + Serial.print("Card found - code:"); |
| 296 | + if(rfid.serNum[0] < 16){ |
| 297 | + Serial.print("0"); |
| 298 | + } |
| 299 | + Serial.print(rfid.serNum[0],HEX); |
| 300 | + |
| 301 | + if(rfid.serNum[1] < 16){ |
| 302 | + Serial.print("0"); |
| 303 | + } |
| 304 | + Serial.print(rfid.serNum[1],HEX); |
| 305 | + |
| 306 | + if(rfid.serNum[2] < 16){ |
| 307 | + Serial.print("0"); |
| 308 | + } |
| 309 | + Serial.print(rfid.serNum[2],HEX); |
| 310 | + |
| 311 | + if(rfid.serNum[3] < 16){ |
| 312 | + Serial.print("0"); |
| 313 | + } |
| 314 | + Serial.print(rfid.serNum[3],HEX); |
| 315 | + |
| 316 | + if(rfid.serNum[4] < 16){ |
| 317 | + Serial.print("0"); |
| 318 | + } |
| 319 | + Serial.print(rfid.serNum[4],HEX); |
| 320 | +} |
| 321 | + |
0 commit comments