#include <Wire.h> // Include the Wire library for I2C communication
#include <RTClib.h> // Include the RTClib library for DS1307
// Define the serial port connected to the POS58 printer
//#define PRINTER_SERIAL Serial
RTC_DS1307 rtc; // Create an instance of the RTC_DS1307 class
void setup() {
//PRINTER_SERIAL.begin(9600); // Initialize serial communication with the printer
Serial.begin(9600); // Initialize serial communication for debugging
Serial.println("Start");
// Initialize DS1307
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
// Uncomment the following line if you want to set the time manually
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
void loop() {
// Check if the RTC has been initialized
if (!rtc.isrunning()) {
Serial.println("RTC is not running!");
// Uncomment the following line if you want to set the time manually
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
delay(1000); // Delay to avoid continuous printing
return;
}
// Get current date and time
DateTime now = rtc.now();
// Prepare the date string (dd/mm/yyyy format)
String dateString = String(now.day()) + "/" + String(now.month()) + "/" + String(now.year());
// Prepare the time string (hh:mm:ss AM/PM format)
String timeString = "";
int hour12 = now.hour() % 12;
if (hour12 == 0) {
hour12 = 12; // 12 AM
}
timeString += String(hour12) + ":" + (now.minute() < 10 ? "0" : "") + String(now.minute()) + ":" + (now.second() < 10 ? "0" : "") + String(now.second()) + (now.hour() < 12 ? " AM" : " PM");
// Print the date and time strings to the printer
Serial.println("Date: " + dateString);
Serial.println("Time: " + timeString);
Serial.println(); // Adding an empty line for separation
delay(1000); // Wait for 1 second
}