#include <EEPROM.h>
#include <SPI.h>
#include <MFRC522.h>
#define BUZZER 8
#define botao 6 // botao
#define wipeB 5
#define relay_2 4 // rele
#define relay_1 3 // rele
boolean match = false;
boolean programMode = false;
boolean replaceMaster = false;
int successRead;
byte storedCard[4];
byte readCard[4];
byte masterCard[4];
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
// Setup //
void setup() {
pinMode(wipeB, INPUT_PULLUP);
pinMode(botao, INPUT_PULLUP);
pinMode(relay_1, OUTPUT);
pinMode(relay_2, OUTPUT);
digitalWrite(relay_1, LOW);
digitalWrite(relay_2, LOW);
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
Serial.println(F("Access Control v3.4"));
ShowReaderDetails();
if (digitalRead(wipeB) == LOW) {
Serial.println(F("Botao de limpeza pressionado"));
Serial.println(F("Voce tem 5 segundos para cancelar"));
Serial.println(F("Isso removera todos os registros sem volta!"));
delay(5000);
if (digitalRead(wipeB) == LOW) {
Serial.println(F("Iniciando Limpeza da EEPROM"));
for (int x = 0; x < EEPROM.length(); x = x + 1) {
if (EEPROM.read(x) == 0) {
}
else {
EEPROM.write(x, 0);
}
}
Serial.println(F("EEPROM limpa com sucesso!!!"));
tone(8, 600, 50);
delay(150);
}
else {
Serial.println(F("Limpeza Cancelada"));
tone(8, 900, 100);
}
}
if (EEPROM.read(1) != 143) {
Serial.println(F("Nenhum cartao Master foi definido"));
Serial.println(F("Digitalize um Cartao para Definir como Master"));
do {
successRead = getID();
tone(8, 1600, 250);
delay(300);
tone(8, 1600, 250);
}
while (!successRead);
for ( int j = 0; j < 4; j++ ) {
EEPROM.write( 2 + j, readCard[j] );
}
EEPROM.write(1, 143);
Serial.println(F("Cartao master Definido"));
}
Serial.println(F("-------------------"));
Serial.println(F("UID do Cartao master"));
for ( int i = 0; i < 4; i++ ) {
masterCard[i] = EEPROM.read(2 + i);
Serial.print(masterCard[i], HEX);
}
Serial.println("");
Serial.println(F("-----------"));
Serial.println(F("Tudo pronto"));
Serial.println(F("Aguarnando cartoes para serem escaneados...."));
cycleLeds();
}
// Main Loop
void loop () {
do {
successRead = getID();
if (digitalRead(wipeB) == LOW) {
Serial.println(F("Botao de limpeza pressionado"));
Serial.println(F("Cartao Master sera deletado em 5 segundos"));
delay(5000);
if (digitalRead(wipeB) == LOW) {
EEPROM.write(1, 0);
Serial.println(F("Reinicie o dispositivo para reprogramar o Cartao Master"));
while (1);
}
}
if (programMode) {
cycleLeds_2();
}
else {
normalModeOn();
}
}
while (!successRead);
if (programMode) {
if ( isMaster(readCard) ) {
Serial.println(F("Cartao Master Escaneado"));
Serial.println(F("Saindo do modo ADM"));
Serial.println(F("------"));
programMode = false;
return;
}
else {
if ( findID(readCard) ) {
Serial.println(F("Conheco este Cartao, removendo ..."));
deleteID(readCard);
Serial.println("------");
Serial.println(F("Escaneie um Cartao para ADICIONAR ou REMOVER da EEPROM"));
}
else {
Serial.println(F("Nao conheco este Cartao, adicionando..."));
writeID(readCard);
Serial.println(F("------"));
Serial.println(F("Escaneie um Cartao para ADICIONAR ou REMOVER da EEPROM"));
}
}
}
else {
if ( isMaster(readCard)) {
programMode = true;
Serial.println(F("Ola Cartao Master - Acessando modo ADM"));
int count = EEPROM.read(0);
Serial.print(F("Eu tenho "));
Serial.print(count);
Serial.print(F(" Registro(s) na EEPROM"));
Serial.println("");
Serial.println(F("Escaneie um Cartao para ADICIONAR ou REMOVER da EEPROM"));
Serial.println(F("Escaneie o Cartao master para sair do modo ADM"));
Serial.println(F("------"));
}
else {
if ( findID(readCard) ) {
Serial.println(F("Bem vindo, voce pode passar"));
granted();
}
else {
Serial.println(F("Acesso negado!"));
denied();
}
}
}
}
/// Access Granted
void granted () {
digitalWrite(relay_1, HIGH);
digitalWrite(relay_2, HIGH);
tone(8, 1600, 250);
delay(250);
tone(8, 1600, 250);
}
// Access Denied
void denied() {
tone(8, 1400, 400);
delay(1000);
}
/// Get PICC's UID
int getID() {
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return 0;
}
if ( ! mfrc522.PICC_ReadCardSerial()) {
return 0;
}
Serial.println(F("Cartao Escaneado UID:"));
for (int i = 0; i < 4; i++) {
readCard[i] = mfrc522.uid.uidByte[i];
Serial.print(readCard[i], HEX);
}
Serial.println("");
mfrc522.PICC_HaltA();
return 1;
}
void ShowReaderDetails() {
byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
Serial.print(F("MFRC522 Software Version: 0x"));
Serial.print(v, HEX);
if (v == 0x91)
Serial.print(F(" = v1.0"));
else if (v == 0x92)
Serial.print(F(" = v2.0"));
else
Serial.print(F(" (Desconhecido),Provavel copia chinesa?"));
Serial.println("");
if ((v == 0x00) || (v == 0xFF)) {
Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?"));
Serial.println(F("SYSTEM HALTED: Check connections."));
while (true);
}
}
/// Cycle Leds (Program Mode)
void cycleLeds() {
tone(8, 1800, 200);
delay(100);
tone(8, 1800, 200);
}
/// Cycle Leds (Program Mode)
void cycleLeds_2() {
tone(8, 1200, 200);
delay(100);
}
/// Normal Mode Led
void normalModeOn () {
}
/// Read an ID from EEPROM
void readID( int number ) {
int start = (number * 4 ) + 2;
for ( int i = 0; i < 4; i++ ) {
storedCard[i] = EEPROM.read(start + i);
}
}
void writeID( byte a[] ) {
if ( !findID( a ) ) {
int num = EEPROM.read(0);
int start = ( num * 4 ) + 6;
num++;
EEPROM.write( 0, num );
for ( int j = 0; j < 4; j++ ) {
EEPROM.write( start + j, a[j] );
}
successWrite();
Serial.println(F("Registro adicionado na EEPROM"));
}
else {
failedWrite();
Serial.println(F("Erro! Ha algo de errado com ID ou problemas na EEPROM"));
}
}
void deleteID( byte a[] ) {
if ( !findID( a ) ) {
failedWrite();
Serial.println(F("Failed! There is something wrong with ID or bad EEPROM"));
}
else {
int num = EEPROM.read(0);
int slot;
int start;
int looping;
int j;
int count = EEPROM.read(0);
slot = findIDSLOT( a );
start = (slot * 4) + 2;
looping = ((num - slot) * 4);
num--;
EEPROM.write( 0, num );
for ( j = 0; j < looping; j++ ) {
EEPROM.write( start + j, EEPROM.read(start + 4 + j));
}
for ( int k = 0; k < 4; k++ ) {
EEPROM.write( start + j + k, 0);
}
successDelete();
Serial.println(F("Sucesso, removido da EEPROM"));
}
}
boolean checkTwo ( byte a[], byte b[] ) {
if ( a[0] != NULL )
match = true;
for ( int k = 0; k < 4; k++ ) {
if ( a[k] != b[k] )
match = false;
}
if ( match ) {
return true;
}
else {
return false;
}
}
int findIDSLOT( byte find[] ) {
int count = EEPROM.read(0);
for ( int i = 1; i <= count; i++ ) {
readID(i);
if ( checkTwo( find, storedCard ) ) {
return i;
break;
}
}
}
boolean findID( byte find[] ) {
int count = EEPROM.read(0);
for ( int i = 1; i <= count; i++ ) {
readID(i);
if ( checkTwo( find, storedCard ) ) {
return true;
break;
}
else {
}
}
return false;
}
/// Write Success to EEPROM
void successWrite() {
tone(8, 1500, 200);
delay(150);
tone(8, 1500, 200);
delay(200);
}
/// Write Failed to EEPROM
void failedWrite() {
tone(8, 1400, 200);
delay(200);
tone(8, 1400, 200);
}
// Success Remove UID From EEPROM
void successDelete() {
delay(200);
tone(8, 1900, 100);
delay(200);
}
boolean isMaster( byte test[] ) {
if ( checkTwo( test, masterCard ) )
return true;
else
return false;
}