Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SD.h>
- #include <SPI.h>
- #include <Wire.h>
- #include <MFRC522.h>
- #include <LiquidCrystal.h>
- #include "RTClib.h"
- #define SS_PIN 8
- #define RST_PIN 9
- #define ECHO_TO_SERIAL 1
- #define WAIT_TO_START 0
- #define LOG_INTERVAL 1000
- #define SYNC_INTERVAL 1000
- uint32_t syncTime = 0;
- int buttonReading = 1;
- const int chipSelect = 10;
- MFRC522 mfrc522(SS_PIN, RST_PIN);
- LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
- String user001ID = "64 EC 6A 85";
- boolean user001Status = true;
- String user002ID = "66 51 06 12";
- boolean user002Status = true;
- RTC_DS1307 RTC;
- File logfile;
- void error(char *str)
- {
- Serial.print("error: ");
- Serial.println(str);
- while (1);
- }
- ///////////////////////////////////////////////////////////// Setup() Start /////////////
- void setup(void)
- {
- Serial.begin(9600);
- lcd.begin(16, 2);
- scanning();
- Serial.println();
- SPI.begin(); // Initiate SPI bus
- mfrc522.PCD_Init(); // Initiate MFRC522
- #if WAIT_TO_START
- Serial.println("Type any character to start");
- while (!Serial.available());
- #endif //WAIT_TO_START
- // initialize the SD card
- Serial.print("Initializing SD card...");
- // make sure that the default chip select pin is set to
- // output, even if you don't use it:
- pinMode(10, OUTPUT);
- // pinMode(buttonPin, INPUT);
- // see if the card is present and can be initialized:
- if (!SD.begin(chipSelect)) {
- error("Card failed, or not present");
- }
- Serial.println("card initialized.");
- // create a new file
- char filename[] = "LOGGER00.CSV";
- for (uint8_t i = 0; i < 100; i++) {
- filename[6] = i / 10 + '0';
- filename[7] = i % 10 + '0';
- if (! SD.exists(filename)) {
- // only open a new file if it doesn't exist
- logfile = SD.open(filename, FILE_WRITE);
- break; // leave the loop!
- }
- }
- if (! logfile) {
- error("couldnt create file");
- }
- Serial.print("Logging to: ");
- Serial.println(filename);
- // connect to RTC
- Wire.begin();
- if (!RTC.begin()) {
- logfile.println("RTC failed");
- #if ECHO_TO_SERIAL
- Serial.println("RTC failed");
- #endif //ECHO_TO_SERIAL
- }
- logfile.println("Date-Time,Status");
- #if ECHO_TO_SERIAL
- Serial.println("Date-Time,Status");
- #endif //ECHO_TO_SERIAL
- }
- ///////////////////////////////////////////////////////////// Setup END /////////////
- ///////////////////////////////////////////////////////////// Loop() Start //////////
- void loop()
- {
- // Look for new cards
- if ( ! mfrc522.PICC_IsNewCardPresent())
- {
- return;
- }
- // Select one of the cards
- if ( ! mfrc522.PICC_ReadCardSerial())
- {
- return;
- }
- //Show UID on serial monitor
- String content = "";
- byte letter;
- for (byte i = 0; i < mfrc522.uid.size; i++)
- {
- Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
- Serial.print(mfrc522.uid.uidByte[i], HEX);
- content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
- content.concat(String(mfrc522.uid.uidByte[i], HEX));
- }
- Serial.println();
- content.toUpperCase();
- ///////////////////////////////////////////////////////////////////////// User #001 /start
- if (content.substring(1) == user001ID)
- {
- if (user001Status == true) {
- user001Status = false;
- user001IN();
- dateAndTime();
- logUser001IN();
- delay(3000);
- scanning();
- }
- else if (user001Status == false) {
- user001Status = true;
- user001OUT();
- dateAndTime();
- logUser001OUT();
- delay(3000);
- scanning();
- }
- }
- ///////////////////////////////////////////////////////////////////////// User #001 /end
- ///////////////////////////////////////////////////////////////////////// User #002 /start
- if (content.substring(1) == user002ID)
- {
- if (user002Status == true) {
- user002Status = false;
- Serial.println("User 002 Just Signed IN");
- delay(1000);
- }
- else if (user002Status == false) {
- user002Status = true;
- Serial.println("User 002 Just Signed OUT");
- delay(1000);
- }
- }
- ///////////////////////////////////////////////////////////////////////// User #002 /end
- }
- ///////////////////////////////////////////////////////////// Loop() END /////////////////////
- ///////////////////////////////////////////////////////////////////////// User #001 Show on LCD Screen + Save data in Log file
- void user001IN() {
- lcd.setCursor(0, 0);
- lcd.print("(Card Found) ");
- lcd.setCursor(0, 1);
- lcd.print("User 001 SIGN IN");
- }
- void user001OUT() {
- lcd.setCursor(0, 0);
- lcd.print("(Card Found) ");
- lcd.setCursor(0, 1);
- lcd.print("User 001 is OUT");
- }
- void logUser001IN() {
- logfile.print(", ");
- logfile.print("User 001 Just Signed IN");
- #if ECHO_TO_SERIAL
- Serial.print(", ");
- Serial.print("User 001 Just Signed IN");
- #endif //ECHO_TO_SERIAL
- logfile.println();
- #if ECHO_TO_SERIAL
- Serial.println();
- #endif // ECHO_TO_SERIAL
- if ((millis() - syncTime) < SYNC_INTERVAL) return;
- syncTime = millis();
- logfile.flush();
- }
- void logUser001OUT() {
- logfile.print(", ");
- logfile.print("User 001 Just Signed OUT");
- #if ECHO_TO_SERIAL
- Serial.print(", ");
- Serial.print("User 001 Just Signed OUT");
- #endif //ECHO_TO_SERIAL
- logfile.println();
- #if ECHO_TO_SERIAL
- Serial.println();
- #endif // ECHO_TO_SERIAL
- if ((millis() - syncTime) < SYNC_INTERVAL) return;
- syncTime = millis();
- logfile.flush();
- }
- ///////////////////////////////////////////////////////////////////////// User #001 Show on LCD Screen + Save data in Log file /end
- void scanning() {
- lcd.setCursor(0, 0);
- lcd.print("Scanning... ");
- lcd.setCursor(0, 1);
- lcd.print(" ");
- }
- void dateAndTime() {
- //Serial.println("User 001 Just Signed OUT");
- DateTime now;
- // fetch the time
- now = RTC.now();
- // log time
- logfile.print('"');
- logfile.print(now.year(), DEC);
- logfile.print("/");
- logfile.print(now.month(), DEC);
- logfile.print("/");
- logfile.print(now.day(), DEC);
- logfile.print(" ");
- logfile.print(now.hour(), DEC);
- logfile.print(":");
- logfile.print(now.minute(), DEC);
- logfile.print(":");
- logfile.print(now.second(), DEC);
- logfile.print('"');
- #if ECHO_TO_SERIAL
- Serial.print('"');
- Serial.print(now.year(), DEC);
- Serial.print("/");
- Serial.print(now.month(), DEC);
- Serial.print("/");
- Serial.print(now.day(), DEC);
- Serial.print(" ");
- Serial.print(now.hour(), DEC);
- Serial.print(":");
- Serial.print(now.minute(), DEC);
- Serial.print(":");
- Serial.print(now.second(), DEC);
- Serial.print('"');
- #endif //ECHO_TO_SERIAL
- delay(10);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement