Them log check port serial ACM0
This commit is contained in:
parent
5931745257
commit
d702b4018a
421
Lock/Lock.ino
421
Lock/Lock.ino
|
@ -61,7 +61,7 @@
|
||||||
#define greenLed 6
|
#define greenLed 6
|
||||||
#define blueLed 5
|
#define blueLed 5
|
||||||
|
|
||||||
#define relay 8 // Set Relay Pin
|
#define relay A2 //8 // Set Relay Pin
|
||||||
#define wipeB 2 // Button pin for WipeMode
|
#define wipeB 2 // Button pin for WipeMode
|
||||||
#define button 7 // button exit
|
#define button 7 // button exit
|
||||||
bool relay_stt = false;
|
bool relay_stt = false;
|
||||||
|
@ -82,9 +82,12 @@ MFRC522 mfrc522(SS_PIN, RST_PIN);
|
||||||
bool Debug = false;
|
bool Debug = false;
|
||||||
|
|
||||||
///////////////////////////////////////// Setup ///////////////////////////////////
|
///////////////////////////////////////// Setup ///////////////////////////////////
|
||||||
void setup() {
|
void setup()
|
||||||
|
{
|
||||||
|
pinMode(13, OUTPUT);
|
||||||
|
digitalWrite(13, LOW);
|
||||||
//Arduino Pin Configuration
|
//Arduino Pin Configuration
|
||||||
pinMode(ledView,OUTPUT);
|
pinMode(ledView, OUTPUT);
|
||||||
pinMode(redLed, OUTPUT);
|
pinMode(redLed, OUTPUT);
|
||||||
pinMode(greenLed, OUTPUT);
|
pinMode(greenLed, OUTPUT);
|
||||||
pinMode(blueLed, OUTPUT);
|
pinMode(blueLed, OUTPUT);
|
||||||
|
@ -92,16 +95,24 @@ void setup() {
|
||||||
pinMode(relay, OUTPUT);
|
pinMode(relay, OUTPUT);
|
||||||
//Be careful how relay circuit behave on while resetting or power-cycling your Arduino
|
//Be careful how relay circuit behave on while resetting or power-cycling your Arduino
|
||||||
digitalWrite(relay, HIGH); // Make sure door is locked
|
digitalWrite(relay, HIGH); // Make sure door is locked
|
||||||
|
delay(2000);
|
||||||
|
digitalWrite(relay, LOW);
|
||||||
|
delay(2000);
|
||||||
|
digitalWrite(relay, HIGH); // Make sure door is locked
|
||||||
|
delay(2000);
|
||||||
digitalWrite(redLed, LED_OFF); // Make sure led is off
|
digitalWrite(redLed, LED_OFF); // Make sure led is off
|
||||||
digitalWrite(greenLed, LED_OFF); // Make sure led is off
|
digitalWrite(greenLed, LED_OFF); // Make sure led is off
|
||||||
digitalWrite(blueLed, LED_OFF); // Make sure led is off
|
digitalWrite(blueLed, LED_OFF); // Make sure led is off
|
||||||
digitalWrite(ledView,LOW);
|
digitalWrite(ledView, LOW);
|
||||||
|
|
||||||
//Protocol Configuration
|
//Protocol Configuration
|
||||||
Serial.begin(9600); // Initialize serial communications with PC
|
Serial.begin(9600); // Initialize serial communications with PC
|
||||||
|
|
||||||
|
/*
|
||||||
SPI.begin(); // MFRC522 Hardware uses SPI protocol
|
SPI.begin(); // MFRC522 Hardware uses SPI protocol
|
||||||
mfrc522.PCD_Init(); // Initialize MFRC522 Hardware
|
mfrc522.PCD_Init(); // Initialize MFRC522 Hardware
|
||||||
if(Debug ==true){
|
if (Debug == true)
|
||||||
|
{
|
||||||
//If you set Antenna Gain to Max it will increase reading distance
|
//If you set Antenna Gain to Max it will increase reading distance
|
||||||
mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);
|
mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);
|
||||||
Serial.println(F("Access Control Example v0.1")); // For debugging purposes
|
Serial.println(F("Access Control Example v0.1")); // For debugging purposes
|
||||||
|
@ -110,21 +121,27 @@ void setup() {
|
||||||
ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details
|
ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details
|
||||||
|
|
||||||
//Wipe Code - If the Button (wipeB) Pressed while setup run (powered on) it wipes EEPROM
|
//Wipe Code - If the Button (wipeB) Pressed while setup run (powered on) it wipes EEPROM
|
||||||
if (digitalRead(wipeB) == LOW) { // when button pressed pin should get low, button connected to ground
|
if (digitalRead(wipeB) == LOW)
|
||||||
|
{ // when button pressed pin should get low, button connected to ground
|
||||||
digitalWrite(redLed, LED_ON); // Red Led stays on to inform user we are going to wipe
|
digitalWrite(redLed, LED_ON); // Red Led stays on to inform user we are going to wipe
|
||||||
if (Debug ==1){
|
if (Debug == 1)
|
||||||
|
{
|
||||||
Serial.println(F("Wipe Button Pressed"));
|
Serial.println(F("Wipe Button Pressed"));
|
||||||
Serial.println(F("You have 10 seconds to Cancel"));
|
Serial.println(F("You have 10 seconds to Cancel"));
|
||||||
Serial.println(F("This will be remove all records and cannot be undone"));
|
Serial.println(F("This will be remove all records and cannot be undone"));
|
||||||
}
|
}
|
||||||
bool buttonState = monitorWipeButton(10000); // Give user enough time to cancel operation
|
bool buttonState = monitorWipeButton(10000); // Give user enough time to cancel operation
|
||||||
if (buttonState == true && digitalRead(wipeB) == LOW) { // If button still be pressed, wipe EEPROM
|
if (buttonState == true && digitalRead(wipeB) == LOW)
|
||||||
|
{ // If button still be pressed, wipe EEPROM
|
||||||
// Serial.println(F("Starting Wiping EEPROM"));
|
// Serial.println(F("Starting Wiping EEPROM"));
|
||||||
for (uint16_t x = 0; x < EEPROM.length(); x = x + 1) { //Loop end of EEPROM address
|
for (uint16_t x = 0; x < EEPROM.length(); x = x + 1)
|
||||||
if (EEPROM.read(x) == 0) { //If EEPROM address 0
|
{ //Loop end of EEPROM address
|
||||||
|
if (EEPROM.read(x) == 0)
|
||||||
|
{ //If EEPROM address 0
|
||||||
// do nothing, already clear, go to the next address in order to save time and reduce writes to EEPROM
|
// do nothing, already clear, go to the next address in order to save time and reduce writes to EEPROM
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
EEPROM.write(x, 0); // if not write 0 to clear, it takes 3.3mS
|
EEPROM.write(x, 0); // if not write 0 to clear, it takes 3.3mS
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -139,7 +156,8 @@ void setup() {
|
||||||
delay(200);
|
delay(200);
|
||||||
digitalWrite(redLed, LED_OFF);
|
digitalWrite(redLed, LED_OFF);
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
// Serial.println(F("Wiping Cancelled")); // Show some feedback that the wipe button did not pressed for 15 seconds
|
// Serial.println(F("Wiping Cancelled")); // Show some feedback that the wipe button did not pressed for 15 seconds
|
||||||
digitalWrite(redLed, LED_OFF);
|
digitalWrite(redLed, LED_OFF);
|
||||||
}
|
}
|
||||||
|
@ -148,42 +166,48 @@ void setup() {
|
||||||
// This also useful to just redefine the Master Card
|
// This also useful to just redefine the Master Card
|
||||||
// You can keep other EEPROM records just write other than 143 to EEPROM address 1
|
// You can keep other EEPROM records just write other than 143 to EEPROM address 1
|
||||||
// EEPROM address 1 should hold magical number which is '143'
|
// EEPROM address 1 should hold magical number which is '143'
|
||||||
if (EEPROM.read(1) != 143) {
|
if (EEPROM.read(1) != 143)
|
||||||
if (Debug ==1){
|
{
|
||||||
|
if (Debug == 1)
|
||||||
|
{
|
||||||
Serial.println(F("No Master Card Defined"));
|
Serial.println(F("No Master Card Defined"));
|
||||||
Serial.println(F("Scan A PICC to Define as Master Card"));
|
Serial.println(F("Scan A PICC to Define as Master Card"));
|
||||||
}
|
}
|
||||||
|
|
||||||
do {
|
do
|
||||||
|
{
|
||||||
successRead = getID(); // sets successRead to 1 when we get read from reader otherwise 0
|
successRead = getID(); // sets successRead to 1 when we get read from reader otherwise 0
|
||||||
digitalWrite(blueLed, LED_ON); // Visualize Master Card need to be defined
|
digitalWrite(blueLed, LED_ON); // Visualize Master Card need to be defined
|
||||||
delay(200);
|
delay(200);
|
||||||
digitalWrite(blueLed, LED_OFF);
|
digitalWrite(blueLed, LED_OFF);
|
||||||
delay(200);
|
delay(200);
|
||||||
}
|
} while (!successRead); // Program will not go further while you not get a successful read
|
||||||
while (!successRead); // Program will not go further while you not get a successful read
|
for (uint8_t j = 0; j < 4; j++)
|
||||||
for ( uint8_t j = 0; j < 4; j++ ) { // Loop 4 times
|
{ // Loop 4 times
|
||||||
EEPROM.write( 2 + j, readCard[j] ); // Write scanned PICC's UID to EEPROM, start from address 3
|
EEPROM.write(2 + j, readCard[j]); // Write scanned PICC's UID to EEPROM, start from address 3
|
||||||
}
|
}
|
||||||
EEPROM.write(1, 143); // Write to EEPROM we defined Master Card.
|
EEPROM.write(1, 143); // Write to EEPROM we defined Master Card.
|
||||||
if (Debug ==1){
|
if (Debug == 1)
|
||||||
|
{
|
||||||
Serial.println(F("Master Card Defined"));
|
Serial.println(F("Master Card Defined"));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
if (Debug ==1){
|
if (Debug == 1)
|
||||||
|
{
|
||||||
Serial.println(F("-------------------"));
|
Serial.println(F("-------------------"));
|
||||||
Serial.println(F("Master Card's UID"));
|
Serial.println(F("Master Card's UID"));
|
||||||
}
|
}
|
||||||
|
|
||||||
for ( uint8_t i = 0; i < 4; i++ ) { // Read Master Card's UID from EEPROM
|
for (uint8_t i = 0; i < 4; i++)
|
||||||
|
{ // Read Master Card's UID from EEPROM
|
||||||
masterCard[i] = EEPROM.read(2 + i); // Write it to masterCard
|
masterCard[i] = EEPROM.read(2 + i); // Write it to masterCard
|
||||||
if (Debug ==1){
|
if (Debug == 1)
|
||||||
|
{
|
||||||
Serial.print(masterCard[i], HEX);
|
Serial.print(masterCard[i], HEX);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
if (Debug ==1){
|
if (Debug == 1)
|
||||||
|
{
|
||||||
Serial.println("");
|
Serial.println("");
|
||||||
Serial.println(F("-------------------"));
|
Serial.println(F("-------------------"));
|
||||||
Serial.println(F("Everything is ready"));
|
Serial.println(F("Everything is ready"));
|
||||||
|
@ -193,65 +217,78 @@ void setup() {
|
||||||
cycleLeds(); // Everything ready lets give user some feedback by cycling leds
|
cycleLeds(); // Everything ready lets give user some feedback by cycling leds
|
||||||
// }
|
// }
|
||||||
// wdt_enable(WDTO_500MS);
|
// wdt_enable(WDTO_500MS);
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////// Main Loop ///////////////////////////////////
|
///////////////////////////////////////// Main Loop ///////////////////////////////////
|
||||||
void loop () {
|
void loop()
|
||||||
do {
|
{
|
||||||
|
do
|
||||||
|
{
|
||||||
ReadButton();
|
ReadButton();
|
||||||
SerialRead();
|
SerialRead();
|
||||||
|
/*
|
||||||
successRead = getID(); // sets successRead to 1 when we get read from reader otherwise 0
|
successRead = getID(); // sets successRead to 1 when we get read from reader otherwise 0
|
||||||
// When device is in use if wipe button pressed for 10 seconds initialize Master Card wiping
|
// When device is in use if wipe button pressed for 10 seconds initialize Master Card wiping
|
||||||
if (digitalRead(wipeB) == LOW) { // Check if button is pressed
|
if (digitalRead(wipeB) == LOW)
|
||||||
|
{ // Check if button is pressed
|
||||||
// Visualize normal operation is iterrupted by pressing wipe button Red is like more Warning to user
|
// Visualize normal operation is iterrupted by pressing wipe button Red is like more Warning to user
|
||||||
digitalWrite(redLed, LED_ON); // Make sure led is off
|
digitalWrite(redLed, LED_ON); // Make sure led is off
|
||||||
digitalWrite(greenLed, LED_OFF); // Make sure led is off
|
digitalWrite(greenLed, LED_OFF); // Make sure led is off
|
||||||
digitalWrite(blueLed, LED_OFF); // Make sure led is off
|
digitalWrite(blueLed, LED_OFF); // Make sure led is off
|
||||||
// Give some feedback
|
// Give some feedback
|
||||||
if (Debug ==1){
|
if (Debug == 1)
|
||||||
|
{
|
||||||
Serial.println(F("Wipe Button Pressed"));
|
Serial.println(F("Wipe Button Pressed"));
|
||||||
Serial.println(F("Master Card will be Erased! in 10 seconds"));
|
Serial.println(F("Master Card will be Erased! in 10 seconds"));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool buttonState = monitorWipeButton(10000); // Give user enough time to cancel operation
|
bool buttonState = monitorWipeButton(10000); // Give user enough time to cancel operation
|
||||||
if (buttonState == true && digitalRead(wipeB) == LOW) { // If button still be pressed, wipe EEPROM
|
if (buttonState == true && digitalRead(wipeB) == LOW)
|
||||||
|
{ // If button still be pressed, wipe EEPROM
|
||||||
EEPROM.write(1, 0); // Reset Magic Number.
|
EEPROM.write(1, 0); // Reset Magic Number.
|
||||||
if (Debug ==1){
|
if (Debug == 1)
|
||||||
|
{
|
||||||
Serial.println(F("Master Card Erased from device"));
|
Serial.println(F("Master Card Erased from device"));
|
||||||
Serial.println(F("Please reset to re-program Master Card"));
|
Serial.println(F("Please reset to re-program Master Card"));
|
||||||
}
|
}
|
||||||
while (1);
|
while (1)
|
||||||
|
;
|
||||||
}
|
}
|
||||||
// Serial.println(F("Master Card Erase Cancelled"));
|
// Serial.println(F("Master Card Erase Cancelled"));
|
||||||
}
|
}
|
||||||
if (programMode) {
|
if (programMode)
|
||||||
|
{
|
||||||
cycleLeds(); // Program Mode cycles through Red Green Blue waiting to read a new card
|
cycleLeds(); // Program Mode cycles through Red Green Blue waiting to read a new card
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
normalModeOn(); // Normal mode, blue Power LED is on, all others are off
|
normalModeOn(); // Normal mode, blue Power LED is on, all others are off
|
||||||
}
|
}
|
||||||
CheckButton();
|
CheckButton();
|
||||||
}
|
} while (!successRead); //the program will not go further while you are not getting a successful read
|
||||||
while (!successRead); //the program will not go further while you are not getting a successful read
|
|
||||||
|
|
||||||
|
if (programMode)
|
||||||
if (programMode) {
|
{
|
||||||
if ( isMaster(readCard) ) { //When in program mode check First If master card scanned again to exit program mode
|
if (isMaster(readCard))
|
||||||
|
{ //When in program mode check First If master card scanned again to exit program mode
|
||||||
// Serial.println(F("Master Card Scanned"));
|
// Serial.println(F("Master Card Scanned"));
|
||||||
// Serial.println(F("Exiting Program Mode"));
|
// Serial.println(F("Exiting Program Mode"));
|
||||||
// Serial.println(F("-----------------------------"));
|
// Serial.println(F("-----------------------------"));
|
||||||
programMode = false;
|
programMode = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
if ( findID(readCard) ) { // If scanned card is known delete it
|
{
|
||||||
|
if (findID(readCard))
|
||||||
|
{ // If scanned card is known delete it
|
||||||
// Serial.println(F("I know this PICC, removing..."));
|
// Serial.println(F("I know this PICC, removing..."));
|
||||||
deleteID(readCard);
|
deleteID(readCard);
|
||||||
// Serial.println("-----------------------------");
|
// Serial.println("-----------------------------");
|
||||||
// Serial.println(F("Scan a PICC to ADD or REMOVE to EEPROM"));
|
// Serial.println(F("Scan a PICC to ADD or REMOVE to EEPROM"));
|
||||||
}
|
}
|
||||||
else { // If scanned card is not known add it
|
else
|
||||||
|
{ // If scanned card is not known add it
|
||||||
// Serial.println(F("I do not know this PICC, adding..."));
|
// Serial.println(F("I do not know this PICC, adding..."));
|
||||||
writeID(readCard);
|
writeID(readCard);
|
||||||
// Serial.println(F("-----------------------------"));
|
// Serial.println(F("-----------------------------"));
|
||||||
|
@ -259,8 +296,10 @@ void loop () {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
if ( isMaster(readCard)) { // If scanned card's ID matches Master Card's ID - enter program mode
|
{
|
||||||
|
if (isMaster(readCard))
|
||||||
|
{ // If scanned card's ID matches Master Card's ID - enter program mode
|
||||||
programMode = true;
|
programMode = true;
|
||||||
// Serial.println(F("Hello Master - Entered Program Mode"));
|
// Serial.println(F("Hello Master - Entered Program Mode"));
|
||||||
uint8_t count = EEPROM.read(0); // Read the first Byte of EEPROM that
|
uint8_t count = EEPROM.read(0); // Read the first Byte of EEPROM that
|
||||||
|
@ -272,68 +311,78 @@ void loop () {
|
||||||
// Serial.println(F("Scan Master Card again to Exit Program Mode"));
|
// Serial.println(F("Scan Master Card again to Exit Program Mode"));
|
||||||
// Serial.println(F("-----------------------------"));
|
// Serial.println(F("-----------------------------"));
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
if ( findID(readCard) ) { // If not, see if the card is in the EEPROM
|
{
|
||||||
|
if (findID(readCard))
|
||||||
|
{ // If not, see if the card is in the EEPROM
|
||||||
// Serial.println(F("Welcome, You shall pass"));
|
// Serial.println(F("Welcome, You shall pass"));
|
||||||
relay_stt = true;
|
relay_stt = true;
|
||||||
granted(4000); // Open the door lock for 300 ms
|
granted(4000); // Open the door lock for 300 ms
|
||||||
ClearSerialdata();
|
ClearSerialdata();
|
||||||
}
|
}
|
||||||
else { // If not, show that the ID was not valid
|
else
|
||||||
|
{ // If not, show that the ID was not valid
|
||||||
// Serial.println(F("You shall not pass"));
|
// Serial.println(F("You shall not pass"));
|
||||||
denied();
|
denied();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
*/
|
||||||
// wdt_reset();
|
}
|
||||||
//}
|
// wdt_reset();
|
||||||
|
//}
|
||||||
|
|
||||||
///////////////////////////////////////// Access Granted ///////////////////////////////////
|
///////////////////////////////////////// Access Granted ///////////////////////////////////
|
||||||
void granted ( uint16_t setDelay) {
|
void granted(uint16_t setDelay)
|
||||||
|
{
|
||||||
digitalWrite(blueLed, LED_OFF); // Turn off blue LED
|
digitalWrite(blueLed, LED_OFF); // Turn off blue LED
|
||||||
digitalWrite(redLed, LED_OFF); // Turn off red LED
|
digitalWrite(redLed, LED_OFF); // Turn off red LED
|
||||||
digitalWrite(greenLed, LED_ON); // Turn on green LED
|
digitalWrite(greenLed, LED_ON); // Turn on green LED
|
||||||
digitalWrite(relay, LOW); // Unlock door!
|
digitalWrite(relay, LOW); // Unlock door!
|
||||||
digitalWrite(ledView,LOW);
|
digitalWrite(ledView, LOW);
|
||||||
delay(setDelay); // Hold door lock open for given seconds
|
delay(setDelay); // Hold door lock open for given seconds
|
||||||
digitalWrite(relay, HIGH); // Relock door
|
digitalWrite(relay, HIGH); // Relock door
|
||||||
digitalWrite(ledView,HIGH);
|
digitalWrite(ledView, HIGH);
|
||||||
delay(200); // Hold green LED on for a second
|
delay(200); // Hold green LED on for a second
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////// Access Denied ///////////////////////////////////
|
///////////////////////////////////////// Access Denied ///////////////////////////////////
|
||||||
void denied() {
|
void denied()
|
||||||
|
{
|
||||||
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
|
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
|
||||||
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
|
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
|
||||||
digitalWrite(redLed, LED_ON); // Turn on red LED
|
digitalWrite(redLed, LED_ON); // Turn on red LED
|
||||||
delay(1000);
|
delay(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////// Get PICC's UID ///////////////////////////////////
|
||||||
///////////////////////////////////////// Get PICC's UID ///////////////////////////////////
|
uint8_t getID()
|
||||||
uint8_t getID() {
|
{
|
||||||
// Getting ready for Reading PICCs
|
// Getting ready for Reading PICCs
|
||||||
if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
|
if (!mfrc522.PICC_IsNewCardPresent())
|
||||||
|
{ //If a new PICC placed to RFID reader continue
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get Serial and continue
|
if (!mfrc522.PICC_ReadCardSerial())
|
||||||
|
{ //Since a PICC placed get Serial and continue
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
// There are Mifare PICCs which have 4 byte or 7 byte UID care if you use 7 byte PICC
|
// There are Mifare PICCs which have 4 byte or 7 byte UID care if you use 7 byte PICC
|
||||||
// I think we should assume every PICC as they have 4 byte UID
|
// I think we should assume every PICC as they have 4 byte UID
|
||||||
// Until we support 7 byte PICCs
|
// Until we support 7 byte PICCs
|
||||||
// Serial.println(F("Scanned PICC's UID:"));
|
// Serial.println(F("Scanned PICC's UID:"));
|
||||||
for ( uint8_t i = 0; i < 4; i++) { //
|
for (uint8_t i = 0; i < 4; i++)
|
||||||
|
{ //
|
||||||
readCard[i] = mfrc522.uid.uidByte[i];
|
readCard[i] = mfrc522.uid.uidByte[i];
|
||||||
// Serial.print(readCard[i], HEX);
|
// Serial.print(readCard[i], HEX);
|
||||||
}
|
}
|
||||||
// Serial.println("");
|
// Serial.println("");
|
||||||
mfrc522.PICC_HaltA(); // Stop reading
|
mfrc522.PICC_HaltA(); // Stop reading
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
int k;
|
int k;
|
||||||
void ShowReaderDetails() {
|
void ShowReaderDetails()
|
||||||
|
{
|
||||||
// Get the MFRC522 software version
|
// Get the MFRC522 software version
|
||||||
byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
|
byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
|
||||||
// Serial.print(F("MFRC522 Software Version: 0x"));
|
// Serial.print(F("MFRC522 Software Version: 0x"));
|
||||||
|
@ -348,19 +397,22 @@ void ShowReaderDetails() {
|
||||||
// Serial.print(F(" (unknown),probably a chinese clone?"));
|
// Serial.print(F(" (unknown),probably a chinese clone?"));
|
||||||
// Serial.println("");
|
// Serial.println("");
|
||||||
// When 0x00 or 0xFF is returned, communication probably failed
|
// When 0x00 or 0xFF is returned, communication probably failed
|
||||||
if ((v == 0x00) || (v == 0xFF)) {
|
if ((v == 0x00) || (v == 0xFF))
|
||||||
|
{
|
||||||
// Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?"));
|
// Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?"));
|
||||||
// Serial.println(F("SYSTEM HALTED: Check connections."));
|
// Serial.println(F("SYSTEM HALTED: Check connections."));
|
||||||
// Visualize system is halted
|
// Visualize system is halted
|
||||||
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
|
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
|
||||||
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
|
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
|
||||||
digitalWrite(redLed, LED_ON); // Turn on red LED
|
digitalWrite(redLed, LED_ON); // Turn on red LED
|
||||||
while (true); // do not go further
|
while (true)
|
||||||
|
; // do not go further
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
///////////////////////////////////////// Cycle Leds (Program Mode) ///////////////////////////////////
|
///////////////////////////////////////// Cycle Leds (Program Mode) ///////////////////////////////////
|
||||||
void cycleLeds() {
|
void cycleLeds()
|
||||||
|
{
|
||||||
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
|
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
|
||||||
digitalWrite(greenLed, LED_ON); // Make sure green LED is on
|
digitalWrite(greenLed, LED_ON); // Make sure green LED is on
|
||||||
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
|
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
|
||||||
|
@ -373,112 +425,135 @@ void cycleLeds() {
|
||||||
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
|
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
|
||||||
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
|
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
|
||||||
delay(200);
|
delay(200);
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////////////////// Normal Mode Led ///////////////////////////////////
|
//////////////////////////////////////// Normal Mode Led ///////////////////////////////////
|
||||||
void normalModeOn () {
|
void normalModeOn()
|
||||||
|
{
|
||||||
digitalWrite(blueLed, LED_ON); // Blue LED ON and ready to read card
|
digitalWrite(blueLed, LED_ON); // Blue LED ON and ready to read card
|
||||||
digitalWrite(redLed, LED_OFF); // Make sure Red LED is off
|
digitalWrite(redLed, LED_OFF); // Make sure Red LED is off
|
||||||
digitalWrite(greenLed, LED_OFF); // Make sure Green LED is off
|
digitalWrite(greenLed, LED_OFF); // Make sure Green LED is off
|
||||||
digitalWrite(relay, HIGH); // Make sure Door is Locked
|
digitalWrite(relay, HIGH); // Make sure Door is Locked
|
||||||
digitalWrite(ledView,HIGH);
|
digitalWrite(ledView, HIGH);
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////////////////// Read an ID from EEPROM //////////////////////////////
|
//////////////////////////////////////// Read an ID from EEPROM //////////////////////////////
|
||||||
void readID( uint8_t number ) {
|
void readID(uint8_t number)
|
||||||
uint8_t start = (number * 4 ) + 2; // Figure out starting position
|
{
|
||||||
for ( uint8_t i = 0; i < 4; i++ ) { // Loop 4 times to get the 4 Bytes
|
uint8_t start = (number * 4) + 2; // Figure out starting position
|
||||||
|
for (uint8_t i = 0; i < 4; i++)
|
||||||
|
{ // Loop 4 times to get the 4 Bytes
|
||||||
storedCard[i] = EEPROM.read(start + i); // Assign values read from EEPROM to array
|
storedCard[i] = EEPROM.read(start + i); // Assign values read from EEPROM to array
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////// Add ID to EEPROM ///////////////////////////////////
|
///////////////////////////////////////// Add ID to EEPROM ///////////////////////////////////
|
||||||
void writeID( byte a[] ) {
|
void writeID(byte a[])
|
||||||
if ( !findID( a ) ) { // Before we write to the EEPROM, check to see if we have seen this card before!
|
{
|
||||||
|
if (!findID(a))
|
||||||
|
{ // Before we write to the EEPROM, check to see if we have seen this card before!
|
||||||
uint8_t num = EEPROM.read(0); // Get the numer of used spaces, position 0 stores the number of ID cards
|
uint8_t num = EEPROM.read(0); // Get the numer of used spaces, position 0 stores the number of ID cards
|
||||||
uint8_t start = ( num * 4 ) + 6; // Figure out where the next slot starts
|
uint8_t start = (num * 4) + 6; // Figure out where the next slot starts
|
||||||
num++; // Increment the counter by one
|
num++; // Increment the counter by one
|
||||||
EEPROM.write( 0, num ); // Write the new count to the counter
|
EEPROM.write(0, num); // Write the new count to the counter
|
||||||
for ( uint8_t j = 0; j < 4; j++ ) { // Loop 4 times
|
for (uint8_t j = 0; j < 4; j++)
|
||||||
EEPROM.write( start + j, a[j] ); // Write the array values to EEPROM in the right position
|
{ // Loop 4 times
|
||||||
|
EEPROM.write(start + j, a[j]); // Write the array values to EEPROM in the right position
|
||||||
}
|
}
|
||||||
successWrite();
|
successWrite();
|
||||||
// Serial.println(F("Succesfully added ID record to EEPROM"));
|
// Serial.println(F("Succesfully added ID record to EEPROM"));
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
failedWrite();
|
failedWrite();
|
||||||
// Serial.println(F("Failed! There is something wrong with ID or bad EEPROM"));
|
// Serial.println(F("Failed! There is something wrong with ID or bad EEPROM"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////// Remove ID from EEPROM ///////////////////////////////////
|
///////////////////////////////////////// Remove ID from EEPROM ///////////////////////////////////
|
||||||
void deleteID( byte a[] ) {
|
void deleteID(byte a[])
|
||||||
if ( !findID( a ) ) { // Before we delete from the EEPROM, check to see if we have this card!
|
{
|
||||||
|
if (!findID(a))
|
||||||
|
{ // Before we delete from the EEPROM, check to see if we have this card!
|
||||||
failedWrite(); // If not
|
failedWrite(); // If not
|
||||||
// Serial.println(F("Failed! There is something wrong with ID or bad EEPROM"));
|
// Serial.println(F("Failed! There is something wrong with ID or bad EEPROM"));
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
uint8_t num = EEPROM.read(0); // Get the numer of used spaces, position 0 stores the number of ID cards
|
uint8_t num = EEPROM.read(0); // Get the numer of used spaces, position 0 stores the number of ID cards
|
||||||
uint8_t slot; // Figure out the slot number of the card
|
uint8_t slot; // Figure out the slot number of the card
|
||||||
uint8_t start; // = ( num * 4 ) + 6; // Figure out where the next slot starts
|
uint8_t start; // = ( num * 4 ) + 6; // Figure out where the next slot starts
|
||||||
uint8_t looping; // The number of times the loop repeats
|
uint8_t looping; // The number of times the loop repeats
|
||||||
uint8_t j;
|
uint8_t j;
|
||||||
uint8_t count = EEPROM.read(0); // Read the first Byte of EEPROM that stores number of cards
|
uint8_t count = EEPROM.read(0); // Read the first Byte of EEPROM that stores number of cards
|
||||||
slot = findIDSLOT( a ); // Figure out the slot number of the card to delete
|
slot = findIDSLOT(a); // Figure out the slot number of the card to delete
|
||||||
start = (slot * 4) + 2;
|
start = (slot * 4) + 2;
|
||||||
looping = ((num - slot) * 4);
|
looping = ((num - slot) * 4);
|
||||||
num--; // Decrement the counter by one
|
num--; // Decrement the counter by one
|
||||||
EEPROM.write( 0, num ); // Write the new count to the counter
|
EEPROM.write(0, num); // Write the new count to the counter
|
||||||
for ( j = 0; j < looping; j++ ) { // Loop the card shift times
|
for (j = 0; j < looping; j++)
|
||||||
EEPROM.write( start + j, EEPROM.read(start + 4 + j)); // Shift the array values to 4 places earlier in the EEPROM
|
{ // Loop the card shift times
|
||||||
|
EEPROM.write(start + j, EEPROM.read(start + 4 + j)); // Shift the array values to 4 places earlier in the EEPROM
|
||||||
}
|
}
|
||||||
for ( uint8_t k = 0; k < 4; k++ ) { // Shifting loop
|
for (uint8_t k = 0; k < 4; k++)
|
||||||
EEPROM.write( start + j + k, 0);
|
{ // Shifting loop
|
||||||
|
EEPROM.write(start + j + k, 0);
|
||||||
}
|
}
|
||||||
successDelete();
|
successDelete();
|
||||||
// Serial.println(F("Succesfully removed ID record from EEPROM"));
|
// Serial.println(F("Succesfully removed ID record from EEPROM"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////// Check Bytes ///////////////////////////////////
|
///////////////////////////////////////// Check Bytes ///////////////////////////////////
|
||||||
bool checkTwo ( byte a[], byte b[] ) {
|
bool checkTwo(byte a[], byte b[])
|
||||||
for ( uint8_t k = 0; k < 4; k++ ) { // Loop 4 times
|
{
|
||||||
if ( a[k] != b[k] ) { // IF a != b then false, because: one fails, all fail
|
for (uint8_t k = 0; k < 4; k++)
|
||||||
|
{ // Loop 4 times
|
||||||
|
if (a[k] != b[k])
|
||||||
|
{ // IF a != b then false, because: one fails, all fail
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////// Find Slot ///////////////////////////////////
|
///////////////////////////////////////// Find Slot ///////////////////////////////////
|
||||||
uint8_t findIDSLOT( byte find[] ) {
|
uint8_t findIDSLOT(byte find[])
|
||||||
|
{
|
||||||
uint8_t count = EEPROM.read(0); // Read the first Byte of EEPROM that
|
uint8_t count = EEPROM.read(0); // Read the first Byte of EEPROM that
|
||||||
for ( uint8_t i = 1; i <= count; i++ ) { // Loop once for each EEPROM entry
|
for (uint8_t i = 1; i <= count; i++)
|
||||||
|
{ // Loop once for each EEPROM entry
|
||||||
readID(i); // Read an ID from EEPROM, it is stored in storedCard[4]
|
readID(i); // Read an ID from EEPROM, it is stored in storedCard[4]
|
||||||
if ( checkTwo( find, storedCard ) ) { // Check to see if the storedCard read from EEPROM
|
if (checkTwo(find, storedCard))
|
||||||
|
{ // Check to see if the storedCard read from EEPROM
|
||||||
// is the same as the find[] ID card passed
|
// is the same as the find[] ID card passed
|
||||||
return i; // The slot number of the card
|
return i; // The slot number of the card
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////// Find ID From EEPROM ///////////////////////////////////
|
///////////////////////////////////////// Find ID From EEPROM ///////////////////////////////////
|
||||||
bool findID( byte find[] ) {
|
bool findID(byte find[])
|
||||||
|
{
|
||||||
uint8_t count = EEPROM.read(0); // Read the first Byte of EEPROM that
|
uint8_t count = EEPROM.read(0); // Read the first Byte of EEPROM that
|
||||||
for ( uint8_t i = 1; i < count; i++ ) { // Loop once for each EEPROM entry
|
for (uint8_t i = 1; i < count; i++)
|
||||||
|
{ // Loop once for each EEPROM entry
|
||||||
readID(i); // Read an ID from EEPROM, it is stored in storedCard[4]
|
readID(i); // Read an ID from EEPROM, it is stored in storedCard[4]
|
||||||
if ( checkTwo( find, storedCard ) ) { // Check to see if the storedCard read from EEPROM
|
if (checkTwo(find, storedCard))
|
||||||
|
{ // Check to see if the storedCard read from EEPROM
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else { // If not, return false
|
else
|
||||||
|
{ // If not, return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////// Write Success to EEPROM ///////////////////////////////////
|
///////////////////////////////////////// Write Success to EEPROM ///////////////////////////////////
|
||||||
// Flashes the green LED 3 times to indicate a successful write to EEPROM
|
// Flashes the green LED 3 times to indicate a successful write to EEPROM
|
||||||
void successWrite() {
|
void successWrite()
|
||||||
|
{
|
||||||
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
|
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
|
||||||
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
|
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
|
||||||
digitalWrite(greenLed, LED_OFF); // Make sure green LED is on
|
digitalWrite(greenLed, LED_OFF); // Make sure green LED is on
|
||||||
|
@ -493,11 +568,12 @@ void successWrite() {
|
||||||
delay(200);
|
delay(200);
|
||||||
digitalWrite(greenLed, LED_ON); // Make sure green LED is on
|
digitalWrite(greenLed, LED_ON); // Make sure green LED is on
|
||||||
delay(200);
|
delay(200);
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////// Write Failed to EEPROM ///////////////////////////////////
|
///////////////////////////////////////// Write Failed to EEPROM ///////////////////////////////////
|
||||||
// Flashes the red LED 3 times to indicate a failed write to EEPROM
|
// Flashes the red LED 3 times to indicate a failed write to EEPROM
|
||||||
void failedWrite() {
|
void failedWrite()
|
||||||
|
{
|
||||||
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
|
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
|
||||||
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
|
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
|
||||||
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
|
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
|
||||||
|
@ -512,11 +588,12 @@ void failedWrite() {
|
||||||
delay(200);
|
delay(200);
|
||||||
digitalWrite(redLed, LED_ON); // Make sure red LED is on
|
digitalWrite(redLed, LED_ON); // Make sure red LED is on
|
||||||
delay(200);
|
delay(200);
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////// Success Remove UID From EEPROM ///////////////////////////////////
|
///////////////////////////////////////// Success Remove UID From EEPROM ///////////////////////////////////
|
||||||
// Flashes the blue LED 3 times to indicate a success delete to EEPROM
|
// Flashes the blue LED 3 times to indicate a success delete to EEPROM
|
||||||
void successDelete() {
|
void successDelete()
|
||||||
|
{
|
||||||
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
|
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
|
||||||
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
|
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
|
||||||
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
|
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
|
||||||
|
@ -531,68 +608,83 @@ void successDelete() {
|
||||||
delay(200);
|
delay(200);
|
||||||
digitalWrite(blueLed, LED_ON); // Make sure blue LED is on
|
digitalWrite(blueLed, LED_ON); // Make sure blue LED is on
|
||||||
delay(200);
|
delay(200);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////// Check readCard IF is masterCard ///////////////////////////////////
|
////////////////////// Check readCard IF is masterCard ///////////////////////////////////
|
||||||
// Check to see if the ID passed is the master programing card
|
// Check to see if the ID passed is the master programing card
|
||||||
bool isMaster( byte test[] ) {
|
bool isMaster(byte test[])
|
||||||
|
{
|
||||||
return checkTwo(test, masterCard);
|
return checkTwo(test, masterCard);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool monitorWipeButton(uint32_t interval) {
|
bool monitorWipeButton(uint32_t interval)
|
||||||
|
{
|
||||||
uint32_t now = (uint32_t)millis();
|
uint32_t now = (uint32_t)millis();
|
||||||
while ((uint32_t)millis() - now < interval) {
|
while ((uint32_t)millis() - now < interval)
|
||||||
|
{
|
||||||
// check on every half a second
|
// check on every half a second
|
||||||
if (((uint32_t)millis() % 500) == 0) {
|
if (((uint32_t)millis() % 500) == 0)
|
||||||
|
{
|
||||||
if (digitalRead(wipeB) != LOW)
|
if (digitalRead(wipeB) != LOW)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
// ADD Code
|
// ADD Code
|
||||||
|
|
||||||
int stt;
|
int stt;
|
||||||
int stt_old;
|
int stt_old;
|
||||||
String input;
|
String input;
|
||||||
void ReadButton()
|
void ReadButton()
|
||||||
{
|
{
|
||||||
stt = digitalRead(button);
|
stt = digitalRead(button);
|
||||||
Serial.println(stt);
|
Serial.println(stt);
|
||||||
if (stt == 0 && stt_old == 1) {
|
if (stt == 0 && stt_old == 1)
|
||||||
|
{
|
||||||
relay_stt = true;
|
relay_stt = true;
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
relay_stt = false;
|
relay_stt = false;
|
||||||
}
|
}
|
||||||
stt_old = stt;
|
stt_old = stt;
|
||||||
}
|
}
|
||||||
// SD MASTER: 2B429622
|
// SD MASTER: 2B429622
|
||||||
// SD TEST: C9B09B6E
|
// SD TEST: C9B09B6E
|
||||||
|
|
||||||
void SerialRead()
|
void SerialRead()
|
||||||
{
|
{
|
||||||
while (Serial.available() > 0) {
|
while (Serial.available() > 0)
|
||||||
|
{
|
||||||
input = Serial.readStringUntil('\r');
|
input = Serial.readStringUntil('\r');
|
||||||
Serial.println(input);
|
Serial.println(input);
|
||||||
if (input == "1")
|
if (input == "1")
|
||||||
{
|
{
|
||||||
Serial.println("open");
|
Serial.println("open");
|
||||||
if (relay_stt ==false){
|
if (relay_stt == false)
|
||||||
|
{
|
||||||
relay_stt = true;
|
relay_stt = true;
|
||||||
|
digitalWrite(13, HIGH);
|
||||||
}
|
}
|
||||||
ClearSerialdata();
|
ClearSerialdata();
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
digitalWrite(13, LOW);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ClearSerialdata();
|
ClearSerialdata();
|
||||||
}
|
}
|
||||||
void ClearSerialdata()
|
void ClearSerialdata()
|
||||||
{
|
{
|
||||||
while (Serial.available() > 0) {
|
while (Serial.available() > 0)
|
||||||
|
{
|
||||||
char c = Serial.read();
|
char c = Serial.read();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void CheckButton()
|
void CheckButton()
|
||||||
{
|
{
|
||||||
if (relay_stt == true)
|
if (relay_stt == true)
|
||||||
{
|
{
|
||||||
granted(4000); // Open the door lock for 300 ms
|
granted(4000); // Open the door lock for 300 ms
|
||||||
|
@ -602,8 +694,7 @@ void CheckButton()
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
digitalWrite(relay, HIGH); // Unlock door!
|
digitalWrite(relay, HIGH); // Unlock door!
|
||||||
digitalWrite(ledView,HIGH);
|
digitalWrite(ledView, HIGH);
|
||||||
relay_stt = false;
|
relay_stt = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user