#include <SD.h>
#include <RTClib.h>
#include <EEPROM.h>
#include <U8g2lib.h>
#include <Wire.h>
//U8G2_SSD1306_128X64_NONAME_F_HW_I2C disp(U8G2_R0, U8X8_PIN_NONE);
U8G2_SSD1306_128X64_NONAME_F_HW_I2C disp(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
#define PIN_SPI_CS 5
#define coininbt 17
#define dollout 16
#define ledstate 2
#define headerfileperdaytext " Date Time Coin in Doll out Coin total Doll total"
#define headerfilepermonthtext " Date Coin total Doll total"
int
addr1 = 1,
addr2 = 2;
bool
valA1 = 1,
valB1 = 1,
valA2 = 1,
valB2 = 1,
valBT1 = 1,
valBT2 = 1,
headerfileperday,
headerfilepermonth;
long
coininvalPday,
dolloutvalPday,
coininvalPmonth,
dolloutvalPmonth,
coininvalFS, // FS = From the Start
dolloutvalFS;
RTC_DS1307 rtc;
File myFile;
int year, month, day, hour, minute, second ;
char filename_perday[] = "/yyyymmdd.txt"; // filename_perday (without extension) should not exceed 8 chars
char filename_permonth[] = "/mmnyyyy.txt";
void setup() {
disp.begin();
Serial.begin(115200);
EEPROM.begin(100);
pinMode(coininbt, INPUT_PULLUP);
pinMode(dollout, INPUT_PULLUP);
pinMode(ledstate, OUTPUT);
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!");
//rtc.adjust(DateTime(__DATE__, __TIME__));
}
Serial.println(F("SD CARD INITIALIZED."));
if (EEPROM.read(99) == 0) {} else {
writeValtoEEPROM();
}
EEPROM.write(99, 0);
readValfromEEPROM();
EEPROM.commit();
disp.clearBuffer();
disp.setFont(u8g2_font_6x12_mf);
disp.setCursor(15, 35);
disp.print("Made In Thailand");
disp.sendBuffer();
delay(2000);
disp.clearBuffer();
}
void loop() {
disp.clearBuffer();
digitalWrite(ledstate, (millis() / 1000) % 2);
if ('w' == Serial.read()) {
rtc.adjust(DateTime(2022, 10, 06, 18, 47, 40));
}
dolloutvalPday = EEPROM.read(addr1);
coininvalPday = EEPROM.read(addr2);
valB1 = digitalRead(coininbt);
valB2 = digitalRead(dollout);
if (valA1 == 1 && valB1 == 0) {
coininvalPday++;
valBT1 = 1;
writeValtoEEPROM();
} else {
valBT1 = 0;
}
if (valA2 == 1 && valB2 == 0) {
dolloutvalPday++;
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_perday
filename_perday[1] = (year / 1000) + '0';
filename_perday[2] = ((year % 1000) / 100) + '0';
filename_perday[3] = ((year % 100) / 10) + '0';
filename_perday[4] = (year % 10) + '0';
filename_perday[5] = (month / 10) + '0';
filename_perday[6] = (month % 10) + '0';
filename_perday[7] = (day / 10) + '0';
filename_perday[8] = (day % 10) + '0';
// update filename_permonth
filename_permonth[1] = (month / 10) + '0';
filename_permonth[2] = (month % 10) + '0';
filename_permonth[4] = (year / 1000) + '0';
filename_permonth[5] = ((year % 1000) / 100) + '0';
filename_permonth[6] = ((year % 100) / 10) + '0';
filename_permonth[7] = (year % 10) + '0';
if (!SD.exists(filename_perday)) {
headerfileperday = 1;
} else {
headerfileperday = 0;
}
if (!SD.exists(filename_permonth)) {
headerfilepermonth = 1;
}
writeDatatoSD_perday();
readDatafromSD_perday();
writeDatatoSD_permonth();
readDatafromSD_permonth();
totalcostnow_display();
oled_mainheader();
valA1 = valB1;
valA2 = valB2;
disp.sendBuffer();
}
void writeDatatoSD_perday() {
myFile = SD.open(filename_perday, FILE_APPEND);
if (myFile) {
if (headerfileperday == 1) { // เช็คไฟล์ว่า header ถูกสร้างแล้วหรือยัง
myFile.println(headerfileperdaytext);
//headerfileperday = 0;
}
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(" ");
if (hour < 10) {
myFile.print("0");
}
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 (coininvalPday < 10) {
myFile.print("0");
}
if (coininvalPday < 100) {
myFile.print("0");
}
if (coininvalPday < 1000) {
myFile.print("0");
}
myFile.print(coininvalPday);
myFile.print(" ");
if (dolloutvalPday < 10) {
myFile.print("0");
}
if (dolloutvalPday < 100) {
myFile.print("0");
}
if (dolloutvalPday < 1000) {
myFile.print("0");
}
myFile.print(dolloutvalPday);
myFile.println(""); // new line
}
}
myFile.close();
}
void readDatafromSD_perday() {
myFile = SD.open(filename_perday);
if (myFile) {
if (valBT1 == 1 || valBT2 == 1) {
while (myFile.available()) {
Serial.write(myFile.read());
}
}
}
myFile.close();
}
void writeDatatoSD_permonth() {
myFile = SD.open(filename_permonth, FILE_APPEND);
if (myFile) {
if (headerfilepermonth == 1) { // เช็คไฟล์ว่า header ถูกสร้างแล้วหรือยัง
myFile.println(headerfilepermonthtext);
headerfilepermonth = 0;
}
if (headerfileperday == 1) {
//if(coininvalPday > 0 && dolloutvalPday > 0){
if (day < 10) {
myFile.print("0");
}
myFile.print(day, DEC);
myFile.print("/");
myFile.print(month, DEC);
myFile.print("/");
myFile.print(year, DEC);
myFile.print(" ");
//}
if (coininvalPday < 10) {
myFile.print("0");
}
if (coininvalPday < 100) {
myFile.print("0");
}
if (coininvalPday < 1000) {
myFile.print("0");
}
myFile.print(coininvalPday);
myFile.print(" ");
if (dolloutvalPday < 10) {
myFile.print("0");
}
if (dolloutvalPday < 100) {
myFile.print("0");
}
if (dolloutvalPday < 1000) {
myFile.print("0");
}
myFile.print(dolloutvalPday);
myFile.println("");
}
}
myFile.close();
}
void readDatafromSD_permonth() {
myFile = SD.open(filename_permonth);
if (myFile) {
if ('a' == Serial.read()) {
while (myFile.available()) {
Serial.write(myFile.read());
}
}
}
myFile.close();
}
void writeValtoEEPROM() {
EEPROM.write(addr1, dolloutvalPday);
EEPROM.write(addr2, coininvalPday);
EEPROM.commit();
}
void readValfromEEPROM() {
dolloutvalPday = EEPROM.read(addr1);
coininvalPday = EEPROM.read(addr2);
EEPROM.commit();
}
void oled_mainheader() {
disp.setCursor(5, 10);
disp.print("Cost Management V1.0");
disp.drawLine(0, 15, 128, 15);
}
void oled_setupheader() {
}
void totalcostnow_display() {
disp.setCursor(15, 26);
disp.print("Total Cost Today");
disp.drawLine(0, 30, 128, 30);
disp.setCursor(10,43);
disp.print("Coin In");
disp.drawLine(64, 30, 64, 128);
disp.setCursor(75,43);
disp.print("Doll Out");
}