Components and supplies
5 mm LED: Green
RGB Backlight LCD - 16x2
Breadboard (generic)
Arduino UNO
RFID reader (generic)
Rotary potentiometer (generic)
Apps and platforms
Arduino IDE
Project description
Code
FVTJX1LIWWFMMK9.zip
c_cpp
1 2#include <EEPROM.h> //Library To read and write PICC's UIDs from/to EEPROM 3#include <SPI.h> //Library RC522 Module uses SPI protocol 4#include <MFRC522.h> //Library RC522 Module 5#include <LiquidCrystal.h> //Library for LCD Display 6 7boolean match = false; // initialize card match to false 8boolean programMode = false; // initialize programming mode to false 9int successRead; // Variable integer to keep if we have Successful Read from Reader 10byte storedCard[4]; // Stores an ID read from EEPROM 11byte readCard[4]; // Stores scanned ID read from RFID Module 12byte masterCard[4]; // Stores master card's ID read from EEPROM 13#define SS_PIN 10 14#define RST_PIN 9 15MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance. 16LiquidCrystal lcd(7, 6, 5, 4, 3, 2); //Initializing LCD PINS as (RS,EN,D4,D5,D6,D7) 17void setup() { 18 // put your setup code here, to run once: 19 Serial.begin(9600); // Initialize serial communications with PC 20 lcd.begin(16, 2); //Initializing LCD 16x2 21 pinMode(8, OUTPUT); //LED and Buzzer PIN OUT 22 SPI.begin(); // MFRC522 Hardware uses SPI protocol 23 mfrc522.PCD_Init(); // Initialize MFRC522 Hardware 24 mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max); 25 if (EEPROM.read(1) != 1) { // Look EEPROM if Master Card defined, EEPROM address 1 holds if defined 26 Serial.println("No Master Card Defined"); //When no Master Card in Your EEROM (Serial Display) 27 Serial.println("Scan A PICC to Define as Master Card"); 28 lcd.clear(); 29 lcd.setCursor(0, 0); 30 lcd.println("SET MASTERCARD "); //When no Master Card in Your EEROM (LCD Display) 31 lcd.setCursor(0, 1); 32 lcd.println("SCAN A PICC....."); //Scan any RFID CARD to set Your Master Card in Your EEROM (LCD Display) 33 delay(1500); 34 do { 35 successRead = getID(); // sets successRead to 1 when we get read from reader otherwise 0 36 } 37 while (!successRead); //the program will not go further while you not get a successful read 38 for ( int j = 0; j < 4; j++ ) { // Loop 4 times 39 EEPROM.write( 2 + j, readCard[j] ); // Write scanned PICC's UID to EEPROM, start from address 3 40 } 41 EEPROM.write(1, 1); //Write to EEPROM we defined Master Card. 42 Serial.println("Master Card Defined"); 43 44 } 45 Serial.println("Master Card's UID"); 46 for ( int i = 0; i < 4; i++ ) { // Read Master Card's UID from EEPROM 47 masterCard[i] = EEPROM.read(2 + i); // Write it to masterCard 48 Serial.print(masterCard[i], HEX); //Master Card only view in serial 49 Serial.println("Waiting PICCs to bo scanned :)"); 50 } 51 //WAITING TO SCAN THE RFID CARDS: 52 Serial.println(""); 53 Serial.println("Waiting PICCs to bo scanned :)"); 54 lcd.clear(); 55 lcd.setCursor(0, 0); 56 lcd.println("WAITING "); 57 lcd.setCursor(0, 1); 58 lcd.println("FOR PICC.... "); 59 delay(1500); 60} 61void loop() { 62 lcd.clear(); 63 lcd.setCursor(0, 0); 64 lcd.print(" SWIPE"); 65 lcd.setCursor(0, 1); 66 lcd.print(" YOUR CARD"); 67 68 /* 69 if (digitalRead(BUTTON) == HIGH); //To Delete the EEROM USE the below command just run it 70 { 71 // for (int i = 0 ; i < EEPROM.length() ; i++) { 72 // EEPROM.write(i, 0); 73 // } 74 // } */ 75 do { 76 successRead = getID(); // sets successRead to 1 when we get read from reader otherwise 0 77 if (programMode) { 78 // Program Mode cycles through RGB waiting to read a new card 79 } 80 else { 81 }} 82 while (!successRead); //the program will not go further while you not get a successful read 83 if (programMode) { 84 if ( isMaster(readCard) ) { //If master card scanned again exit program mode 85 Serial.println("This is Master Card"); 86 Serial.println("Exiting Program Mode"); 87 lcd.clear(); 88 lcd.setCursor(0, 0); 89 lcd.print("EXITING FROM"); 90 lcd.setCursor(0, 1); 91 lcd.print("MASTERCARD MODE"); 92 delay(2000); 93 programMode = false; 94 return; 95 } 96 else { 97 if ( findID(readCard) ) { //If scanned card is known delete it 98 Serial.println("I know this PICC, so removing"); 99 lcd.clear(); 100 lcd.setCursor(0, 0); 101 lcd.print("AVAILABLE!"); 102 lcd.setCursor(0, 1); 103 lcd.print("SO DELETING....."); 104 delay(5000); 105 deleteID(readCard); 106 Serial.println("-----------------------------"); 107 } 108 else { // If scanned card is not known add it 109 Serial.println("I do not know this PICC, adding..."); 110 lcd.clear(); 111 lcd.setCursor(0, 0); 112 lcd.print("Card no:"); 113 lcd.setCursor(0, 1); 114 lcd.print(readCard[0], HEX); 115 lcd.print(readCard[1], HEX); 116 lcd.print(readCard[2], HEX); 117 lcd.print(readCard[3], HEX); 118 lcd.print(readCard[4], HEX); 119 delay(4000); 120 lcd.clear(); 121 lcd.setCursor(0, 0); 122 lcd.print("NOT AVAILABLE"); 123 lcd.setCursor(0, 1); 124 lcd.print("SO ADDING......."); 125 delay(5000); 126 writeID(readCard); 127 Serial.println("-----------------------------"); 128 }} } 129 else { 130 if ( isMaster(readCard) ) { // If scanned card's ID matches Master Card's ID enter program mode 131 programMode = true; 132 Serial.println("Welcome to Mastercard Mode"); 133 lcd.clear(); 134 lcd.setCursor(0, 0); 135 lcd.print("WELCOME TO"); 136 lcd.setCursor(0, 1); 137 lcd.print("MASTERCARD MODE"); 138 delay(3000); 139 int count = EEPROM.read(0); // Read the first Byte of EEPROM that 140 Serial.print("I have "); // stores the number of ID's in EEPROM 141 Serial.print(count); 142 Serial.print(" record(s) on EEPROM"); 143 Serial.println(""); 144 Serial.println("Scan a PICC to ADD or REMOVE"); 145 Serial.println("-----------------------------"); 146 lcd.clear(); 147 lcd.setCursor(0, 0); 148 lcd.print("SCAN PICC TO"); 149 lcd.setCursor(0, 1); 150 lcd.print("ADD OR REMOVE..."); 151 delay(2500); 152 } 153 else { 154 if ( findID(readCard) ) { // If not, see if the card is in the EEPROM 155 Serial.println("Acces Granted"); 156 lcd.clear(); 157 lcd.setCursor(0, 0); 158 lcd.print(" CONGRATULATION"); 159 lcd.setCursor(0, 1); 160 lcd.print(" ACCESS GRANTED"); 161 digitalWrite(8, HIGH); 162 delay(1500); 163 digitalWrite(8, LOW); 164 lcd.clear(); 165 } 166 else { // If not, show that the ID was not valid 167 Serial.println("Access Denied"); 168 for (int abcd = 0; abcd < 6; abcd++) 169 { 170 lcd.clear(); 171 lcd.setCursor(0, 0); 172 lcd.print(" SORRY"); 173 lcd.setCursor(0, 1); 174 lcd.print(" ACCESS DENIED"); 175 digitalWrite(8, HIGH); 176 delay(700); 177 digitalWrite(8, LOW); 178 lcd.clear(); 179 lcd.print(" YOU'RE NOT "); 180 lcd.setCursor(0, 1); 181 lcd.print(" AUTHORIZED "); 182 delay(700); 183 } 184 lcd.clear(); 185 }}}} 186int getID() { 187 // Getting ready for Reading PICCs 188 if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue 189 return 0; 190 } 191 if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get Serial and continue 192 return 0; 193 } 194 // There are Mifare PICCs which have 4 byte or 7 byte UID care if you use 7 byte PICC 195 // I think we should assume every PICC as they have 4 byte UID 196 // Until we support 7 byte PICCs 197 198 Serial.println("Scanning PICC's UID........."); 199 lcd.clear(); 200 lcd.setCursor(0, 0); 201 lcd.print("SCANNING"); 202 lcd.setCursor(0, 1); 203 lcd.print("PICC's UID....."); 204 delay(2000); 205 for (int i = 0; i < 4; i++) { // 206 readCard[i] = mfrc522.uid.uidByte[i]; 207 Serial.print(readCard[i], HEX); 208 } 209 Serial.println(""); 210 mfrc522.PICC_HaltA(); // Stop reading 211 return 1; 212} 213boolean isMaster( byte test[] ) { 214 if ( checkTwo( test, masterCard ) ) 215 return true; 216 else 217 return false; 218} 219 220boolean checkTwo ( byte a[], byte b[] ) { 221 if ( a[0] != NULL ) // Make sure there is something in the array first 222 match = true; // Assume they match at first 223 for ( int k = 0; k < 4; k++ ) { // Loop 4 times 224 if ( a[k] != b[k] ) // IF a != b then set match = false, one fails, all fail 225 match = false; 226 } 227 if ( match ) { // Check to see if if match is still true 228 return true; // Return true 229 } 230 else { 231 return false; // Return false 232 }} 233boolean findID( byte find[] ) { 234 int count = EEPROM.read(0); // Read the first Byte of EEPROM that 235 for ( int i = 1; i <= count; i++ ) { // Loop once for each EEPROM entry 236 readID(i); // Read an ID from EEPROM, it is stored in storedCard[4] 237 if ( checkTwo( find, storedCard ) ) { // Check to see if the storedCard read from EEPROM 238 return true; 239 break; // Stop looking we found it 240 } 241 else { // If not, return false 242 }} 243 return false; 244} 245void readID( int number ) { 246 int start = (number * 4 ) + 2; // Figure out starting position 247 for ( int i = 0; i < 4; i++ ) { // Loop 4 times to get the 4 Bytes 248 storedCard[i] = EEPROM.read(start + i); // Assign values read from EEPROM to array 249 } 250} 251void deleteID( byte a[] ) { 252 if ( !findID( a ) ) { // Before we delete from the EEPROM, check to see if we have this card! 253 failedWrite(); // If not 254 } 255 else { 256 int num = EEPROM.read(0); // Get the numer of used spaces, position 0 stores the number of ID cards 257 int slot; // Figure out the slot number of the card 258 int start;// = ( num * 4 ) + 6; // Figure out where the next slot starts 259 int looping; // The number of times the loop repeats 260 int j; 261 int count = EEPROM.read(0); // Read the first Byte of EEPROM that stores number of cards 262 slot = findIDSLOT( a ); //Figure out the slot number of the card to delete 263 start = (slot * 4) + 2; 264 looping = ((num - slot) * 4); 265 num--; // Decrement the counter by one 266 EEPROM.write( 0, num ); // Write the new count to the counter 267 for ( j = 0; j < looping; j++ ) { // Loop the card shift times 268 EEPROM.write( start + j, EEPROM.read(start + 4 + j)); // Shift the array values to 4 places earlier in the EEPROM 269 } 270 for ( int k = 0; k < 4; k++ ) { //Shifting loop 271 EEPROM.write( start + j + k, 0); 272 } 273 successDelete(); 274 }} 275 //For Failed to add the card: 276void failedWrite() { 277 278 Serial.println("something wrong with Card"); 279 lcd.clear(); 280 lcd.setCursor(0, 0); 281 lcd.print("SOMETHING WRONG"); 282 lcd.setCursor(0, 1); 283 lcd.print("WITH CARD"); 284 delay(2000); 285} 286//For Sucessfully Deleted: 287void successDelete() { 288 Serial.println("Succesfully removed"); 289 lcd.clear(); 290 lcd.setCursor(0, 0); 291 lcd.print("SUCCESFULLY"); 292 lcd.setCursor(0, 1); 293 lcd.print("REMOVED"); 294 delay(2000); 295} 296int findIDSLOT( byte find[] ) { 297 int count = EEPROM.read(0); // Read the first Byte of EEPROM that 298 for ( int i = 1; i <= count; i++ ) { // Loop once for each EEPROM entry 299 readID(i); // Read an ID from EEPROM, it is stored in storedCard[4] 300 if ( checkTwo( find, storedCard ) ) { // Check to see if the storedCard read from EEPROM 301 // is the same as the find[] ID card passed 302 return i; // The slot number of the card 303 break; // Stop looking we found it 304 } 305 } 306} 307//For Sucessfully Added: 308void successWrite() { 309 310 Serial.println("Succesfully added"); 311 lcd.clear(); 312 lcd.setCursor(0, 0); 313 lcd.print("SUCCESFULLY"); 314 lcd.setCursor(0, 1); 315 lcd.print("ADDED"); 316 delay(2000); 317} 318//For Adding card to EEROM: 319void writeID( byte a[] ) { 320 if ( !findID( a ) ) { // Before we write to the EEPROM, check to see if we have seen this card before! 321 int num = EEPROM.read(0); // Get the numer of used spaces, position 0 stores the number of ID cards 322 int start = ( num * 4 ) + 6; // Figure out where the next slot starts 323 num++; // Increment the counter by one 324 EEPROM.write( 0, num ); // Write the new count to the counter 325 for ( int j = 0; j < 4; j++ ) { // Loop 4 times 326 EEPROM.write( start + j, a[j] ); // Write the array values to EEPROM in the right position 327 } 328 successWrite(); 329 } 330 else { 331 failedWrite(); 332 } 333} 334 335 336
FVTJX1LIWWFMMK9.zip
c_cpp
1 2#include <EEPROM.h> //Library To read and write PICC's UIDs from/to 3 EEPROM 4#include <SPI.h> //Library RC522 Module uses SPI protocol 5#include 6 <MFRC522.h> //Library RC522 Module 7#include <LiquidCrystal.h> //Library for 8 LCD Display 9 10boolean match = false; // initialize card match to false 11boolean 12 programMode = false; // initialize programming mode to false 13int successRead; 14 // Variable integer to keep if we have Successful Read from Reader 15byte storedCard[4]; 16 // Stores an ID read from EEPROM 17byte readCard[4]; // Stores scanned 18 ID read from RFID Module 19byte masterCard[4]; // Stores master card's ID read 20 from EEPROM 21#define SS_PIN 10 22#define RST_PIN 9 23MFRC522 mfrc522(SS_PIN, 24 RST_PIN); // Create MFRC522 instance. 25LiquidCrystal lcd(7, 6, 5, 4, 3, 2); //Initializing 26 LCD PINS as (RS,EN,D4,D5,D6,D7) 27void setup() { 28 // put your setup code here, 29 to run once: 30 Serial.begin(9600); // Initialize serial communications with 31 PC 32 lcd.begin(16, 2); //Initializing LCD 16x2 33 pinMode(8, OUTPUT); //LED 34 and Buzzer PIN OUT 35 SPI.begin(); // MFRC522 Hardware uses SPI protocol 36 37 mfrc522.PCD_Init(); // Initialize MFRC522 Hardware 38 mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max); 39 40 if (EEPROM.read(1) != 1) { // Look EEPROM if Master Card defined, EEPROM address 41 1 holds if defined 42 Serial.println("No Master Card Defined"); //When no 43 Master Card in Your EEROM (Serial Display) 44 Serial.println("Scan A PICC to 45 Define as Master Card"); 46 lcd.clear(); 47 lcd.setCursor(0, 0); 48 lcd.println("SET 49 MASTERCARD "); //When no Master Card in Your EEROM (LCD Display) 50 lcd.setCursor(0, 51 1); 52 lcd.println("SCAN A PICC....."); //Scan any RFID CARD to set Your Master 53 Card in Your EEROM (LCD Display) 54 delay(1500); 55 do { 56 successRead 57 = getID(); // sets successRead to 1 when we get read from reader otherwise 0 58 59 } 60 while (!successRead); //the program will not go further while you not 61 get a successful read 62 for ( int j = 0; j < 4; j++ ) { // Loop 4 times 63 64 EEPROM.write( 2 + j, readCard[j] ); // Write scanned PICC's UID to EEPROM, 65 start from address 3 66 } 67 EEPROM.write(1, 1); //Write to EEPROM we defined 68 Master Card. 69 Serial.println("Master Card Defined"); 70 71 } 72 73 Serial.println("Master Card's UID"); 74 for ( int i = 0; i < 4; i++ ) { // 75 Read Master Card's UID from EEPROM 76 masterCard[i] = EEPROM.read(2 + i); // 77 Write it to masterCard 78 Serial.print(masterCard[i], HEX); //Master Card only 79 view in serial 80 Serial.println("Waiting PICCs to bo scanned :)"); 81 82 } 83 //WAITING TO SCAN THE RFID CARDS: 84 Serial.println(""); 85 Serial.println("Waiting 86 PICCs to bo scanned :)"); 87 lcd.clear(); 88 lcd.setCursor(0, 0); 89 lcd.println("WAITING 90 "); 91 lcd.setCursor(0, 1); 92 lcd.println("FOR PICC.... "); 93 94 delay(1500); 95} 96void loop() { 97 lcd.clear(); 98 lcd.setCursor(0, 0); 99 100 lcd.print(" SWIPE"); 101 lcd.setCursor(0, 1); 102 lcd.print(" YOUR 103 CARD"); 104 105 /* 106 if (digitalRead(BUTTON) == HIGH); //To 107 Delete the EEROM USE the below command just run it 108 { 109 // for (int i = 0 110 ; i < EEPROM.length() ; i++) { 111 // EEPROM.write(i, 0); 112 // } 113 // } */ 114 115 do { 116 successRead = getID(); // sets successRead to 1 when we get read from 117 reader otherwise 0 118 if (programMode) { 119 // Program Mode cycles through 120 RGB waiting to read a new card 121 } 122 else { 123 }} 124 while (!successRead); 125 //the program will not go further while you not get a successful read 126 if (programMode) 127 { 128 if ( isMaster(readCard) ) { //If master card scanned again exit program 129 mode 130 Serial.println("This is Master Card"); 131 Serial.println("Exiting 132 Program Mode"); 133 lcd.clear(); 134 lcd.setCursor(0, 0); 135 lcd.print("EXITING 136 FROM"); 137 lcd.setCursor(0, 1); 138 lcd.print("MASTERCARD MODE"); 139 140 delay(2000); 141 programMode = false; 142 return; 143 } 144 else 145 { 146 if ( findID(readCard) ) { //If scanned card is known delete it 147 Serial.println("I 148 know this PICC, so removing"); 149 lcd.clear(); 150 lcd.setCursor(0, 151 0); 152 lcd.print("AVAILABLE!"); 153 lcd.setCursor(0, 1); 154 lcd.print("SO 155 DELETING....."); 156 delay(5000); 157 deleteID(readCard); 158 Serial.println("-----------------------------"); 159 160 } 161 else { // If scanned card is not known add it 162 163 Serial.println("I do not know this PICC, adding..."); 164 lcd.clear(); 165 166 lcd.setCursor(0, 0); 167 lcd.print("Card no:"); 168 lcd.setCursor(0, 169 1); 170 lcd.print(readCard[0], HEX); 171 lcd.print(readCard[1], HEX); 172 173 lcd.print(readCard[2], HEX); 174 lcd.print(readCard[3], HEX); 175 176 lcd.print(readCard[4], HEX); 177 delay(4000); 178 lcd.clear(); 179 180 lcd.setCursor(0, 0); 181 lcd.print("NOT AVAILABLE"); 182 lcd.setCursor(0, 183 1); 184 lcd.print("SO ADDING......."); 185 delay(5000); 186 writeID(readCard); 187 188 Serial.println("-----------------------------"); 189 }} } 190 else 191 { 192 if ( isMaster(readCard) ) { // If scanned card's ID matches Master Card's 193 ID enter program mode 194 programMode = true; 195 Serial.println("Welcome 196 to Mastercard Mode"); 197 lcd.clear(); 198 lcd.setCursor(0, 0); 199 lcd.print("WELCOME 200 TO"); 201 lcd.setCursor(0, 1); 202 lcd.print("MASTERCARD MODE"); 203 204 delay(3000); 205 int count = EEPROM.read(0); // Read the first Byte of 206 EEPROM that 207 Serial.print("I have "); // stores the number of ID's 208 in EEPROM 209 Serial.print(count); 210 Serial.print(" record(s) on EEPROM"); 211 212 Serial.println(""); 213 Serial.println("Scan a PICC to ADD or REMOVE"); 214 215 Serial.println("-----------------------------"); 216 lcd.clear(); 217 218 lcd.setCursor(0, 0); 219 lcd.print("SCAN PICC TO"); 220 lcd.setCursor(0, 221 1); 222 lcd.print("ADD OR REMOVE..."); 223 delay(2500); 224 } 225 226 else { 227 if ( findID(readCard) ) { // If not, see if the card 228 is in the EEPROM 229 Serial.println("Acces Granted"); 230 lcd.clear(); 231 232 lcd.setCursor(0, 0); 233 lcd.print(" CONGRATULATION"); 234 lcd.setCursor(0, 235 1); 236 lcd.print(" ACCESS GRANTED"); 237 digitalWrite(8, HIGH); 238 239 delay(1500); 240 digitalWrite(8, LOW); 241 lcd.clear(); 242 243 } 244 else { // If not, show that the ID was not valid 245 Serial.println("Access 246 Denied"); 247 for (int abcd = 0; abcd < 6; abcd++) 248 { 249 lcd.clear(); 250 251 lcd.setCursor(0, 0); 252 lcd.print(" SORRY"); 253 lcd.setCursor(0, 254 1); 255 lcd.print(" ACCESS DENIED"); 256 digitalWrite(8, HIGH); 257 258 delay(700); 259 digitalWrite(8, LOW); 260 lcd.clear(); 261 262 lcd.print(" YOU'RE NOT "); 263 lcd.setCursor(0, 1); 264 265 lcd.print(" AUTHORIZED "); 266 delay(700); 267 } 268 269 lcd.clear(); 270 }}}} 271int getID() { 272 // Getting ready for Reading 273 PICCs 274 if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID 275 reader continue 276 return 0; 277 } 278 if ( ! mfrc522.PICC_ReadCardSerial()) 279 { //Since a PICC placed get Serial and continue 280 return 0; 281 } 282 // 283 There are Mifare PICCs which have 4 byte or 7 byte UID care if you use 7 byte PICC 284 285 // I think we should assume every PICC as they have 4 byte UID 286 // Until we 287 support 7 byte PICCs 288 289 Serial.println("Scanning PICC's UID........."); 290 291 lcd.clear(); 292 lcd.setCursor(0, 0); 293 lcd.print("SCANNING"); 294 lcd.setCursor(0, 295 1); 296 lcd.print("PICC's UID....."); 297 delay(2000); 298 for (int i = 0; 299 i < 4; i++) { // 300 readCard[i] = mfrc522.uid.uidByte[i]; 301 Serial.print(readCard[i], 302 HEX); 303 } 304 Serial.println(""); 305 mfrc522.PICC_HaltA(); // Stop reading 306 307 return 1; 308} 309boolean isMaster( byte test[] ) { 310 if ( checkTwo( test, 311 masterCard ) ) 312 return true; 313 else 314 return false; 315} 316 317boolean 318 checkTwo ( byte a[], byte b[] ) { 319 if ( a[0] != NULL ) // Make sure there is 320 something in the array first 321 match = true; // Assume they match at first 322 323 for ( int k = 0; k < 4; k++ ) { // Loop 4 times 324 if ( a[k] != b[k] ) // 325 IF a != b then set match = false, one fails, all fail 326 match = false; 327 328 } 329 if ( match ) { // Check to see if if match is still true 330 return 331 true; // Return true 332 } 333 else { 334 return false; // Return false 335 336 }} 337boolean findID( byte find[] ) { 338 int count = EEPROM.read(0); // Read 339 the first Byte of EEPROM that 340 for ( int i = 1; i <= count; i++ ) { // Loop 341 once for each EEPROM entry 342 readID(i); // Read an ID from EEPROM, it is stored 343 in storedCard[4] 344 if ( checkTwo( find, storedCard ) ) { // Check to see if 345 the storedCard read from EEPROM 346 return true; 347 break; // Stop looking 348 we found it 349 } 350 else { // If not, return false 351 }} 352 return 353 false; 354} 355void readID( int number ) { 356 int start = (number * 4 ) + 2; // 357 Figure out starting position 358 for ( int i = 0; i < 4; i++ ) { // Loop 4 times 359 to get the 4 Bytes 360 storedCard[i] = EEPROM.read(start + i); // Assign values 361 read from EEPROM to array 362 } 363} 364void deleteID( byte a[] ) { 365 if ( !findID( 366 a ) ) { // Before we delete from the EEPROM, check to see if we have this card! 367 368 failedWrite(); // If not 369 } 370 else { 371 int num = EEPROM.read(0); 372 // Get the numer of used spaces, position 0 stores the number of ID cards 373 int 374 slot; // Figure out the slot number of the card 375 int start;// = ( num * 4 376 ) + 6; // Figure out where the next slot starts 377 int looping; // The number 378 of times the loop repeats 379 int j; 380 int count = EEPROM.read(0); // Read 381 the first Byte of EEPROM that stores number of cards 382 slot = findIDSLOT( a 383 ); //Figure out the slot number of the card to delete 384 start = (slot * 4) 385 + 2; 386 looping = ((num - slot) * 4); 387 num--; // Decrement the counter 388 by one 389 EEPROM.write( 0, num ); // Write the new count to the counter 390 391 for ( j = 0; j < looping; j++ ) { // Loop the card shift times 392 EEPROM.write( 393 start + j, EEPROM.read(start + 4 + j)); // Shift the array values to 4 places earlier 394 in the EEPROM 395 } 396 for ( int k = 0; k < 4; k++ ) { //Shifting loop 397 398 EEPROM.write( start + j + k, 0); 399 } 400 successDelete(); 401 }} 402 403 //For Failed to add the card: 404void failedWrite() { 405 406 Serial.println("something 407 wrong with Card"); 408 lcd.clear(); 409 lcd.setCursor(0, 0); 410 lcd.print("SOMETHING 411 WRONG"); 412 lcd.setCursor(0, 1); 413 lcd.print("WITH CARD"); 414 delay(2000); 415} 416//For 417 Sucessfully Deleted: 418void successDelete() { 419 Serial.println("Succesfully 420 removed"); 421 lcd.clear(); 422 lcd.setCursor(0, 0); 423 lcd.print("SUCCESFULLY"); 424 425 lcd.setCursor(0, 1); 426 lcd.print("REMOVED"); 427 delay(2000); 428} 429int 430 findIDSLOT( byte find[] ) { 431 int count = EEPROM.read(0); // Read the first Byte 432 of EEPROM that 433 for ( int i = 1; i <= count; i++ ) { // Loop once for each EEPROM 434 entry 435 readID(i); // Read an ID from EEPROM, it is stored in storedCard[4] 436 437 if ( checkTwo( find, storedCard ) ) { // Check to see if the storedCard read 438 from EEPROM 439 // is the same as the find[] ID card passed 440 return 441 i; // The slot number of the card 442 break; // Stop looking we found it 443 444 } 445 } 446} 447//For Sucessfully Added: 448void successWrite() { 449 450 Serial.println("Succesfully 451 added"); 452 lcd.clear(); 453 lcd.setCursor(0, 0); 454 lcd.print("SUCCESFULLY"); 455 456 lcd.setCursor(0, 1); 457 lcd.print("ADDED"); 458 delay(2000); 459} 460//For 461 Adding card to EEROM: 462void writeID( byte a[] ) { 463 if ( !findID( a ) ) { // 464 Before we write to the EEPROM, check to see if we have seen this card before! 465 466 int num = EEPROM.read(0); // Get the numer of used spaces, position 0 stores 467 the number of ID cards 468 int start = ( num * 4 ) + 6; // Figure out where the 469 next slot starts 470 num++; // Increment the counter by one 471 EEPROM.write( 472 0, num ); // Write the new count to the counter 473 for ( int j = 0; j < 4; j++ 474 ) { // Loop 4 times 475 EEPROM.write( start + j, a[j] ); // Write the array 476 values to EEPROM in the right position 477 } 478 successWrite(); 479 } 480 481 else { 482 failedWrite(); 483 } 484} 485 486 487
Downloadable files
CIRCUIT
CIRCUIT
Comments
Only logged in users can leave comments