Вы находитесь на странице: 1из 9

//RFID System

#include <Wire.h>
#include <RTClib.h>
#include <stdio.h>
#include <constants.h>
#include <vfd.h>
#include <vfd.c>
#include <SPI.h>
#include <SD.h>
#include <Adafruit_PN532.h>

File myFile;

RTC_DS1307 rtc;

int wgnd = 18; // RTC Breakout board doesn't require much juice. Repurposing pin 18 &
19 as GND & +5V output.
int w5v = 19;

int refresh_rate = 5000; // Default Refresh rate of 5 seconds

const int ledPin = 16; // The Green LED


const int ledPin1 = 17; // The RED LED
const int chipSelect = 53;

const int numCodes = 5; // the number of RFID tags allocated to sketch

const char *codes[] // RFID UID codes

{
"4D398796", // Tag 1
"8B2A6474", // Tag 2
"9BF36174", // Tag 3
"0B426B74", // Tag 4
"3BF46B74" // Tag 5
};

Adafruit_PN532 nfc(2, 3); // IRQ, RESET pins : Reset pin not connected by default on the NFC
Shield

char code[16]; // hex string of code

/*******************************************************************************/

void setup(void)
{
pinMode(ledPin, OUTPUT);
pinMode(ledPin1, OUTPUT);

/********************** First, set up the VFD Display... **********************/


VFD_Inititialise();
delay(10);

VFD_ResetDevice();
ARDUINO_delay(2); // Delay to allow the VFD device to reset clean
delay(10);

VFD_WriteInstruction(CLEAR_LCD);
delay(1000);
VFD_WriteInstruction(SETLINE0);
VFD_WriteString(" SupaLec");
delay(1000);
VFD_WriteInstruction(SETLINE1);
VFD_WriteString(" SupaClokka Mk1");
delay(1000);

Serial.begin(9600); // Open serial communications and wait for port to open:


Serial.println("VFD initialized...");
// Serial.println(F("VFD initialized...")); // The F in (F(blah!)); means the info will be saved in FLASH
and free up memory or summit...
Serial.println("");

/********************** Next, set up the Real Time Clock Chip... **********************/

pinMode(wgnd, OUTPUT); // this sets pin as GND output for RTC Breakout board
digitalWrite(wgnd, LOW);
pinMode(w5v, OUTPUT); // this sets pin as +5V output for RTC Breakout board
digitalWrite(w5v, HIGH);

Serial.println(F("Initializing RTC..."));
#ifdef AVR
Wire.begin();
#else
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif

rtc.begin();

/*
if (! rtc.isrunning())
{
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
rtc.adjust(DateTime(2016, 22, 1, 23, 24, 0));
}
*/
if (!rtc.isrunning())
{
VFD_WriteInstruction(CLEAR_LCD);
delay(100);
VFD_WriteInstruction(SETLINE0);
VFD_WriteString(" RTC failure!");
VFD_WriteInstruction(SETLINE1);
VFD_WriteString(" Call Technician!");

Serial.println(F("RTC is NOT running!"));


//rtc.adjust(DateTime(__DATE__, __TIME__)); // following line sets the RTC to the date &
time this sketch was compiled
}
else
{
Serial.println(F("RTC initialised"));
Serial.println(F(""));
}

/********************** Next, set up the SD Card... **********************/

Serial.println(F("Initializing SD card..."));
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(SS, OUTPUT);

if (!SD.begin(chipSelect))
{
VFD_WriteInstruction(CLEAR_LCD);
delay(100);
VFD_WriteInstruction(SETLINE0);
VFD_WriteString(" SD Card failure!");
VFD_WriteInstruction(SETLINE1);
VFD_WriteString(" Call Technician!");

Serial.println(F("SD Card initialization failed!"));


return;
}

Serial.println(F("SD Card initialized..."));


Serial.println(F(""));

/********************** Next, set up how often data is saved on SD card...


**********************/

File commandFile = SD.open("speed.txt"); //Read the configuration information (speed.txt) to


control how often data is saved on SD card
if (commandFile)
{
Serial.println("Reading Command File");
while(commandFile.available())
{
refresh_rate = commandFile.parseInt();
}

Serial.print("Refresh Rate = ");


Serial.print(refresh_rate);
Serial.println("ms");
commandFile.close(); // Close the file when finished.
}
else
{
Serial.println(F("Couldn't read command File."));
return;
}

/********************** Next, set up how Tag data is checked on SD card...


**********************/

myFile = SD.open("myFile.txt"); //Read the Tag information (myFile.txt) to check tag data on SD
card
if (myFile)
{
Serial.println("myFile.txt");

while(myFile.available())
{
Serial.write(myFile.read());
}

Serial.print("Student Number: ");


Serial.print(code);
myFile.close(); // Close the file when finished.

}
else
{
Serial.println(F("Error opening myFile.txt."));
return;
}

/********************** Next, set up the RFID Shield... **********************/

nfc.begin();

uint32_t versiondata = nfc.getFirmwareVersion();


if (! versiondata)
{
VFD_WriteInstruction(CLEAR_LCD);
delay(100);
VFD_WriteInstruction(SETLINE0);
VFD_WriteString("PN53x board failure!");
VFD_WriteInstruction(SETLINE1);
VFD_WriteString(" Call Technician!");
Serial.print("Didn't find PN53x board");

while (1); // halt


}

Serial.print("Found chip PN5");


Serial.println((versiondata>>24) & 0xFF, HEX); // Got ok data, print it out!
Serial.print("Firmware ver. ");
Serial.print((versiondata>>16) & 0xFF, DEC);
Serial.print('.');
Serial.println((versiondata>>8) & 0xFF, DEC);
Serial.println("");

nfc.SAMConfig(); // configure board to read RFID tags

// Set the max number of retry attempts to read from a card


// This prevents us from waiting forever for a card, which is
// the default behaviour of the PN532.
nfc.setPassiveActivationRetries(0xFF);

VFD_WriteInstruction(CLEAR_LCD);
delay(100);

VFD_WriteInstruction(SETLINE0);
VFD_WriteString("Swipe your student card...");
Serial.println("Swipe your student card...");

//delay(100);
}

/*******************************************************************************/

void loop()
{
digitalWrite(ledPin, HIGH); // The GREEN LED comes on ready for card swipe

if (scanCode())

digitalWrite(ledPin, LOW); // turn the GREEN LED off (LOW is the voltage level)
digitalWrite(ledPin1, HIGH); // turn the RED LED on (HIGH is the voltage level)

VFD_WriteInstruction(SETLINE1);
VFD_WriteString("Checking, please wait...");
Serial.println("Checking, please wait...");
Serial.println("");
delay (2000);
{
checkCode();
}
delay(2000);
}

/*******************************************************************************/

boolean scanCode()
{
boolean success;
byte uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
byte uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)

success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);

if (success)
{
//digitalWrite(ledPin, LOW); // turn the GREEN LED off (LOW is the voltage level)
//digitalWrite(ledPin1, HIGH); // turn the RED LED on (HIGH is the voltage level)

VFD_WriteInstruction(CLEAR_LCD);
delay(100);
VFD_WriteInstruction(SETLINE0);
VFD_WriteString("Student number: ");
Serial.print("Student number: ");

for (uint8_t i=0; i < uidLength; i++)


{
char hexDigits[3];
sprintf(hexDigits, "%02X", uid[i]);
code[i*2] = hexDigits[0];
code[i*2+1] = hexDigits[1];
}
code[uidLength*2] = '\0';
VFD_WriteString(code);
Serial.println(code);

Serial.println("");
}
return success;
}

/*******************************************************************************/

void checkCode()
{
boolean codeValid = false;
for (int i = 0; i < numCodes; i++)
{
if (strcmp(code, codes[i]) == 0)
{
codeValid = true;
}
}

if (codeValid)
{
VFD_WriteInstruction(CLEAR_LCD);
delay(100);
VFD_WriteInstruction(SETLINE0);
VFD_WriteString("Welcome, please enter.");
Serial.println("Welcome, please enter.");
Serial.println("");
Serial.println("-----------------------------");
delay(1000);
VFD_WriteInstruction(CLEAR_LCD);
delay(100);
digitalWrite(ledPin, HIGH); // turn the GREEN LED on by making the voltage HIGH
digitalWrite(ledPin1, LOW); // turn the RED LED off by making the voltage LOW
VFD_WriteInstruction(SETLINE0);
VFD_WriteString("Swipe your student card...");
Serial.println("Swipe your student card...");
Serial.println("");
}

else
{
VFD_WriteInstruction(CLEAR_LCD);
delay(100);
VFD_WriteInstruction(SETLINE0);
VFD_WriteString("Unauthorised student card, please inform Reception..");
Serial.println("Unauthorised student card, please inform Reception...");
Serial.println("");
Serial.println("-----------------------------");
delay(1000);
VFD_WriteInstruction(CLEAR_LCD);
delay(100);

digitalWrite(ledPin, HIGH); // turn the GREEN LED on by making the voltage HIGH
digitalWrite(ledPin1, LOW); // turn the RED LED off by making the voltage LOW

VFD_WriteInstruction(SETLINE0);
VFD_WriteString("Swipe your student card...");
Serial.println("Swipe your student card...");
Serial.println("");

//delay(refresh_rate);
}
}

/*****************************************************************************/

void ARDUINO_DigitalWrite(int PinNumber, int PinState)


{
digitalWrite(PinNumber, PinState);
}

/*****************************************************************************/

int ARDUINO_ReadDigitalPin(int InputPin)


{
return digitalRead(InputPin);
}

/*****************************************************************************/

void ARDUINO_SetPinDirection(int PinNumber, int PinState)


{
pinMode(PinNumber, PinState);
}

/*****************************************************************************/

void ARDUINO_delay(int DelayTime)


{
delay(DelayTime);
}

/*****************************************************************************/

void ARDUINO_delay_us(int DelayTime)


{
delayMicroseconds(DelayTime);
}

/*****************************************************************************/

void ARDUINO_DisableInterrupts()
{
noInterrupts();
}

/*****************************************************************************/

void ARDUINO_EnableInterrupts()
{
interrupts();
}

/*****************************************************************************/

/* bzero() is an old familiar friend. It clears an area of memory so that it's contents
can be guaranteed to be free of all corrupting data. We take two parameters, the address
of the area to be cleared and a char length parameter indicating the size in characters
of the area to be cleared.
*/

void bzero(unsigned char *StartAddress, unsigned int Length)


{
while (--Length > 0)
*StartAddress++ = 0;
}

/*********************************************************************************
**********/

/* bcopy() is another old friend. Here, we copy one area of memory to another. Nice 'n' slick...
*/

void bcopy(unsigned char *SourceAddress, unsigned char *DestinationAddress, unsigned int Length)
{
while (--Length > 0)
*DestinationAddress++ = *SourceAddress++;
}

/*********************************************************************************
**********/

Вам также может понравиться