#include <EEPROM.h>
#include <SD.h>
#include <RTClib.h>
#define PIN_SPI_CS 4
#define coininbt 2
#define dollout 3
int addr1 = 1;
int addr2 = 2;
bool valA1 = 1, valB1 = 1, valA2 = 1, valB2 = 1, valBT1 = 1, valBT2 = 1, headerfile;
long dolloutval, coininval;
RTC_DS1307 rtc;
File myFile;
int year, month, day, hour, minute, second ;
char filename[] = "yyyymmdd.txt"; // filename (without extension) should not exceed 8 chars
void setup() {
Serial.begin(115200);
pinMode(coininbt, INPUT_PULLUP);
pinMode(dollout, INPUT_PULLUP);
if(!rtc.begin()) {Serial.println(F("Couldn't find RTC")); for(;;);}
if(!SD.begin(PIN_SPI_CS)) {Serial.println(F("SD CARD FAILED, OR NOT PRESENT!")); for(;;);}
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(__DATE__, __TIME__));
//rtc.adjust(DateTime(2022, 10, 03, 12, 30, 40)); // <----------------------SET TIME AND DATE: YYYY,MM,DD,HH,MM,SS
}
Serial.println(F("SD CARD INITIALIZED."));
if(EEPROM.read(50) == 0){}else{
writeValtoEEPROM();
}
EEPROM.write(50,0);
readValfromEEPROM();
}
void loop() {
dolloutval = EEPROM.read(addr1);
coininval = EEPROM.read(addr2);
valB1 = digitalRead(coininbt);
valB2 = digitalRead(dollout);
if(valA1 == 1 && valB1 == 0){
coininval++;
valBT1 = 1;
writeValtoEEPROM();
}else{
valBT1 = 0;
}
if(valA2 == 1 && valB2 == 0){
dolloutval++;
valBT2 = 1;
writeValtoEEPROM();
}else{
valBT2 = 0;
}
DateTime now = rtc.now();
year = now.year();
month = now.month();
day = now.day();
hour = now.hour();
minute = now.minute();
second = now.second();
// update filename
filename[0] = (year / 1000) + '0';
filename[1] = ((year % 1000) / 100) + '0';
filename[2] = ((year % 100) / 10) + '0';
filename[3] = (year % 10) + '0';
filename[4] = (month / 10) + '0';
filename[5] = (month % 10) + '0';
filename[6] = (day / 10) + '0';
filename[7] = (day % 10) + '0';
if(!SD.exists(filename)){headerfile = 1;}//else{headerfile = 1;}
writeDatatoSD();
readDatafromSD();
valA1 = valB1;
valA2 = valB2;
}
void writeDatatoSD(){
myFile = SD.open(filename, FILE_WRITE);
if(myFile){// write timestamp
if(headerfile == 1){ // เช็คไฟล์ว่า header ถูกสร้างแล้วหรือยัง
myFile.println(" Date Time Coin in Doll out Coin total Doll total");
headerfile = 0; // เช็คโดยเก็บสถานะ header file ลงใน EEPROM
}
if(valBT1 == 1 || valBT2 == 1){
Serial.println(F("Writing to SD..."));
if(day<10){myFile.print("0");}
myFile.print(day, DEC);
myFile.print("/");
myFile.print(month, DEC);
myFile.print("/");
myFile.print(year, DEC);
myFile.print(" ");
myFile.print(hour, DEC);
myFile.print(':');
if(minute<10){myFile.print("0");}
myFile.print(minute, DEC);
myFile.print(':');
if(second<10){myFile.print("0");}
myFile.print(second, DEC);
myFile.print(" ");
myFile.print(valBT1);
myFile.print(" ");
myFile.print(valBT2);
myFile.print(" ");
if(coininval<10){myFile.print("0");}
if(coininval<100){myFile.print("0");}
if(coininval<1000){myFile.print("0");}
myFile.print(coininval);
myFile.print(" ");
if(dolloutval<10){myFile.print("0");}
if(dolloutval<100){myFile.print("0");}
if(dolloutval<1000){myFile.print("0");}
myFile.print(dolloutval);
myFile.println(""); // new line
}
}
myFile.close();
}
void readDatafromSD(){
myFile = SD.open(filename);
if(myFile){if(valBT1 == 1 || valBT2 == 1){while(myFile.available()){Serial.write(myFile.read());}}}
myFile.close();
}
void writeValtoEEPROM(){
EEPROM.write(addr1,dolloutval);
EEPROM.write(addr2,coininval);
}
void readValfromEEPROM(){
dolloutval = EEPROM.read(addr1);
coininval = EEPROM.read(addr2);
}