Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <SD.h>
- #include <Wire.h>
- #include <TimeLib.h>
- #include <DS1307RTC.h>
- #include <LiquidCrystal_I2C.h>
- File myFile;
- LiquidCrystal_I2C lcd(0x27, 20, 4);
- const char *monthName[12] = {
- "Jan", "Feb", "Mar", "Apr", "May", "Jun",
- "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
- };
- tmElements_t tm;
- void setup() {
- bool parse = false;
- bool config = false;
- // get the date and time the compiler was run
- if (getDate(__DATE__) && getTime(__TIME__)) {
- parse = true;
- // and configure the RTC with this info
- if (RTC.write(tm)) {
- config = true;
- }
- }
- if (parse && config) {
- Serial.print("DS1307 configured Time=");
- Serial.print(__TIME__);
- Serial.print(", Date=");
- Serial.println(__DATE__);
- } else if (parse) {
- Serial.println("DS1307 Communication Error :-{");
- Serial.println("Please check your circuitry");
- } else {
- Serial.print("Could not parse info from the compiler, Time=\"");
- Serial.print(__TIME__);
- Serial.print("\", Date=\"");
- Serial.print(__DATE__);
- Serial.println("\"");
- }
- Serial.begin(9600);
- lcd.init();
- while (!Serial) ; // wait for serial
- Serial.println("Initializing SD card...");
- if (!SD.begin(4)) {
- Serial.println("initialization failed!");
- while (1);
- }
- }
- void loop() {
- String data;
- if (RTC.read(tm)) {
- myFile = SD.open("test.txt", FILE_WRITE); // test.txt durch RFID Tag ersetzen FileName = String(RfidTag)+".txt"
- if (myFile) {
- data = checkDigits(tm.Hour) + ":" + checkDigits(tm.Minute) + ":" + checkDigits(tm.Second);
- Serial.println(data);
- myFile.println(data);
- lcd.backlight();
- lcd.clear();
- lcd.print(data);
- myFile.close();
- data = "";
- } else {
- // if the file didn't open, print an error:
- Serial.println("error opening test.txt");
- }
- } else {
- Serial.println("Uhr läuft nicht");
- }
- delay(1000);
- myFile = SD.open("test.txt");
- if (myFile) {
- Serial.println("File opened");
- // read from the file until there's nothing else in it:
- while (myFile.available()) {
- Serial.write(myFile.read());
- }
- // close the file:
- myFile.close();
- } else {
- // if the file didn't open, print an error:
- Serial.println("error opening test.txt");
- }
- }
- String checkDigits(int number) {
- String DtZeit;
- DtZeit = String (number);
- if (number >= 0 && number < 10) {
- DtZeit = "0" + String(number);
- }
- return DtZeit;
- }
- bool getTime(const char *str)
- {
- int Hour, Min, Sec;
- if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
- tm.Hour = Hour;
- tm.Minute = Min;
- tm.Second = Sec;
- return true;
- }
- bool getDate(const char *str)
- {
- char Month[12];
- int Day, Year;
- uint8_t monthIndex;
- if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
- for (monthIndex = 0; monthIndex < 12; monthIndex++) {
- if (strcmp(Month, monthName[monthIndex]) == 0) break;
- }
- if (monthIndex >= 12) return false;
- tm.Day = Day;
- tm.Month = monthIndex + 1;
- tm.Year = CalendarYrToTm(Year);
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement