ColeH:
If anyone is still willing, this is my improved code
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
//Begin RTC
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;
//End RTC
//Begin RFID
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // initialize the library with the numbers of the interface pins
#define TAG_LEN 12
char tag[12] = {'3', '6', '0', '0', '7', '8', '9', '9', '8', '7'}; //tag 1
char tag2[12] = {'3', '6', '0', '0', '7', '8', '9', 'B', 'B', '5'}; //tag 2
char code[12];
int bytesread = 0;
//Connections to be made:
int ledPin = 13; // Connect LED to pin 13
int rfidPin = 2; // RFID enable pin connected to digital pin 2
//Connect SOUT Pin to pin 0, does not need to be defined because 0 is the default RX pin
int val=0;
//End RFID
//Begin SD
#include <SD.h>
File myFile;
//End SD
void setup () {
Serial.begin(9600);
Wire.begin();
RTC.begin();
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
// uncomment it & upload to set the time, date and start run the RTC!
//RTC.adjust(DateTime(DATE, TIME));
}
pinMode(10, OUTPUT);
}
void loop () {
DateTime now = RTC.now();
//Begin RFID
digitalWrite(ledPin,LOW); //LED off
digitalWrite(rfidPin, LOW); // Activate the RFID reader
lcd.clear(); //Clears LCD
lcd.setCursor(0, 0);
lcd.print(" Please Swipe");
lcd.setCursor(0, 1);
lcd.print(" Your Card");
if(Serial.available() > 0) { // if data available from reader
if((val = Serial.read()) == 10) { // check for header
bytesread = 0;
while(bytesread<10) { // read 10 digit code
if( Serial.available() > 0) {
val = Serial.read();
if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading
break; // stop reading
}
code[bytesread] = val; // add the digit
bytesread++; // ready to read next digit
digitalWrite(rfidPin, HIGH); //Turns off RFID reader, THIS IS IMPORTANT so it doesn't double read the card
}
}
//End RFID
if(bytesread >= 10) { // if 10 digit read is complete
if(strcmp(code, tag) == 0) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Access Granted");
lcd.setCursor(0, 1);
lcd.println(code);
delay (1500);
lcd.setCursor(0, 1);
lcd.print("Jonnyblanch ");
digitalWrite(ledPin,HIGH);
Serial.print("Tag matches: ");
Serial.println(code);
//digitalWrite(ledPin,HIGH);
digitalWrite(rfidPin, HIGH);
myFile = SD.open("log.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to log.txt...");
myFile.print("Jonnyblanch");
myFile.print(now.month(), DEC);
myFile.print('/');
myFile.print(now.day(), DEC);
myFile.print(' ');
myFile.print(now.hour(), DEC);
myFile.print(':');
myFile.print(now.minute(), DEC);
myFile.print(':');
myFile.print(now.second(), DEC);
myFile.println();
// close the file:
myFile.close();
delay(2000); // wait for a second or two
return; //Go to the top, skip the bottom
}
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Access Denied");
lcd.setCursor(0, 1);
lcd.println(code);
Serial.print(code);
Serial.println(" does not match");
digitalWrite(rfidPin, HIGH);
}
}
bytesread = 0;
delay(2000); // wait for a second
digitalWrite(ledPin,LOW);
}
}
}