KRITSADA icon

Data Logger

KRITSADA | PRO | 02/24/21 11:16:40 PM UTC | 0 ⭐ | 1499 👁️ | Never ⏰ | []
C++ |

12.29 KB

|

None

|

0 👍

/

0 👎

#include <SPI.h> //
#include <SD.h> //
#include <Wire.h> //
#include "RTClib.h" //
#include <MFRC522.h> //
#include <LiquidCrystal_I2C.h>
 
LiquidCrystal_I2C lcd(0x27,16,2);
#define SS_PIN 8 // 10 for shield // 8 for normal small module
#define RST_PIN 9 // FOR RFID READER
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
 
 
//-------------------------------------------------------------------------USER # 1
String user001ID = "09 2A 13 A3";
boolean user001Status = true;
//-------------------------------------------------------------------------
 
//-------------------------------------------------------------------------USER # 2
String user002ID = "D4 A0 30 2A";
boolean user002Status = true;
//-------------------------------------------------------------------------
 
 
// A simple data logger for the Arduino analog pins
 
// how many milliseconds between grabbing data and logging it. 1000 ms is once a second
#define LOG_INTERVAL  1000 // mills between entries (reduce to take more/faster data)
 
// how many milliseconds before writing the logged data permanently to disk
// set it to the LOG_INTERVAL to write each time (safest)
// set it to 10*LOG_INTERVAL to write all data every 10 datareads, you could lose up to
// the last 10 reads if power is lost but it uses less power and is much faster!
#define SYNC_INTERVAL 1000 // mills between calls to flush() - to write data to the card
uint32_t syncTime = 0; // time of last sync()
int buttonReading = 1;
#define ECHO_TO_SERIAL   1 // echo data to serial port
#define WAIT_TO_START    0 // Wait for serial input in setup()
 
 
RTC_DS1307 RTC; // define the Real Time Clock object
 
// for the data logging shield, we use digital pin 10 for the SD cs line
const int chipSelect = 10;
 
// the logging file
File logfile;
 
void error(char *str)
{
  Serial.print("error: ");
  Serial.println(str);
 
  while (1);
}
 
void setup(void)
{
  lcd.init();
  lcd.init();
  lcd.backlight();
  lcd.print("Connecting...");
  Serial.begin(9600);
  Serial.println();
  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
  pinMode(7, OUTPUT);
#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,Log");
#if ECHO_TO_SERIAL
  Serial.println("Date-Time,Status");
#endif //ECHO_TO_SERIAL
 
  delay(2000);
  lcd.clear();
  lcd.print("System Ready");
  delay(2500);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Log:");
}
 
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;
  buzz();
  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;
      //Serial.println("Shahad Just Signed IN");
      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(",");
      logfile.print(now.hour(), DEC);
      logfile.print(":");
      logfile.print(now.minute(), DEC);
      logfile.print(":");
      logfile.print(now.second(), DEC);
#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);
 
      logfile.print(", ");
      logfile.print("Shahad Just Signed IN");
#if ECHO_TO_SERIAL
      Serial.print(", ");
      Serial.print("Shahad Just Signed IN");
      lcd.setCursor(0, 1);
      lcd.print("Shahad-Sign IN");
      delay(3000);
      lcd.setCursor(0, 1);
      lcd.print("                ");
#endif //ECHO_TO_SERIAL
      logfile.println();
#if ECHO_TO_SERIAL
      Serial.println();
#endif // ECHO_TO_SERIAL
      // Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
      // which uses a bunch of power and takes time
      if ((millis() - syncTime) < SYNC_INTERVAL) return;
      syncTime = millis();
      // blink LED to show we are syncing data to the card & updating FAT!
      logfile.flush();
      delay(1000);
    }
    else if (user001Status == false) {
      user001Status = true;
      //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(",");
      logfile.print(now.hour(), DEC);
      logfile.print(":");
      logfile.print(now.minute(), DEC);
      logfile.print(":");
      logfile.print(now.second(), DEC);
#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);
 
      logfile.print(", ");
      logfile.print("Shahad Just Signed OUT");
#if ECHO_TO_SERIAL
      Serial.print(", ");
      Serial.print("Shahad Just Signed OUT");
      lcd.setCursor(0, 1);
      lcd.print("Shahad-Sign OUT");
      delay(3000);
      lcd.setCursor(0, 1);
      lcd.print("                ");
#endif //ECHO_TO_SERIAL
      logfile.println();
#if ECHO_TO_SERIAL
      Serial.println();
#endif // ECHO_TO_SERIAL
      // Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
      // which uses a bunch of power and takes time
      if ((millis() - syncTime) < SYNC_INTERVAL) return;
      syncTime = millis();
      // blink LED to show we are syncing data to the card & updating FAT!
      logfile.flush();
      delay(1000);
    }
  }
  ///////////////////////////////////////////////////////////////////////// User #001 /end
 
  ///////////////////////////////////////////////////////////////////////// User #002 /start       //***** [1]
 
  if (content.substring(1) == user002ID)                              //***** [2]
  {
    if (user002Status == true) {                                      //***** [3]
      user002Status = false;                                          //***** [4]
 
      DateTime now;
      now = RTC.now();
      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(",");
      logfile.print(now.hour(), DEC);
      logfile.print(":");
      logfile.print(now.minute(), DEC);
      logfile.print(":");
      logfile.print(now.second(), DEC);
#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);
      logfile.print(", ");
      logfile.print("Ayat Alshaabani Just Signed IN");                           //***** [5]
#if ECHO_TO_SERIAL
      Serial.print(", ");
      Serial.print("Ayat Just Signed IN");                            //***** [6]
      lcd.setCursor(0, 1);
      //        "1234567891234567"    16x characters only #######
      lcd.print("Ayat-Sign IN ");                                      //***** [7]
      delay(3000);
      lcd.setCursor(0, 1);
      lcd.print("                ");
#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();
      delay(1000);
    }
    else if (user002Status == false) {                                //***** [8]
      user002Status = true;                                           //***** [9]
      DateTime now;
      now = RTC.now();
      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(",");
      logfile.print(now.hour(), DEC);
      logfile.print(":");
      logfile.print(now.minute(), DEC);
      logfile.print(":");
      logfile.print(now.second(), DEC);
#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);
      logfile.print(", ");
      logfile.print("Ayat Alshaabani Just Signed OUT");                          //***** [10]
#if ECHO_TO_SERIAL
      Serial.print(", ");
      Serial.print("Ayat Just Signed OUT");                           //***** [11]
      lcd.setCursor(0, 1);
      //        "1234567891234567"    16x characters only #######
      lcd.print("Ayat-Sign OUT");                                      //***** [12]
      delay(3000);
      lcd.setCursor(0, 1);
      lcd.print("                ");
#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();
      delay(1000);
    }
  }
  ///////////////////////////////////////////////////////////////////////// User #002 /end      //***** [13]
 
}// end of LOOP
 
 
void buzz() {
  digitalWrite(7, 1);
  delay(100);
  digitalWrite(7, 0);
  delay(50);
 
}

Comments