// #include <Wire.h> // Include the Wire library for I2C communication
// #include <RTClib.h> // Include the RTClib library for accessing the RTC
// RTC_DS1307 rtc; // Create an instance of the RTC_DS1307 class
// void setup() {
// Serial.begin(9600); // Initialize serial communication
// Wire.begin(21, 22); // Initialize I2C communication with the specified pins
// // Check if the RTC is running and set the time if necessary
// if (!rtc.begin()) {
// Serial.println("Couldn't find RTC");
// while (1);
// }
// if (!rtc.isrunning()) {
// Serial.println("RTC is NOT running!");
// // Set the time to the time the sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// }
// }
// void loop() {
// DateTime now = rtc.now(); // Get the current date and time from the RTC
// // Check if it's 8AM, 1PM, or 6PM
// if (now.hour() == 2 && now.minute() == 14 && now.second() == 0) {
// Serial.println("It's 2:14 AM");
// } else if (now.hour() == 2 && now.minute() == 15 && now.second() == 0) {
// Serial.println("It's 2:15 PM");
// } else if (now.hour() == 2 && now.minute() == 16 && now.second() == 0) {
// Serial.println("It's 2:16 PM");
// }
// delay(1000); // Wait for 1 second before checking again
// }
// ------------------------------------------------------ //
// ------------------------------------------------------ //
// #include <SD.h>
// #define CS_PIN 5
// File root;
// void setup() {
// Serial.begin(115200);
// Serial.print("Initializing SD card... ");
// if (!SD.begin(CS_PIN)) {
// Serial.println("Card initialization failed!");
// while (true);
// }
// Serial.println("initialization done.");
// Serial.println("Files in the card:");
// root = SD.open("/");
// printDirectory(root, 0);
// Serial.println("");
// // Example of reading file from the card:
// File textFile = SD.open("/wokwi.txt");
// if (textFile) {
// Serial.print("wokwi.txt: ");
// while (textFile.available()) {
// Serial.write(textFile.read());
// }
// textFile.close();
// } else {
// Serial.println("error opening wokwi.txt!");
// }
// }
// void loop() {
// delay(100);
// // nothing happens after setup finishes.
// }
// void printDirectory(File dir, int numTabs) {
// while (true) {
// File entry = dir.openNextFile();
// if (! entry) {
// // no more files
// break;
// }
// for (uint8_t i = 0; i < numTabs; i++) {
// Serial.print('\t');
// }
// Serial.print(entry.name());
// if (entry.isDirectory()) {
// Serial.println("/");
// printDirectory(entry, numTabs + 1);
// } else {
// // files have sizes, directories do not
// Serial.print("\t\t");
// Serial.println(entry.size(), DEC);
// }
// entry.close();
// }
// }
// ------------------------------------------------------ //
// ------------------------------------------------------------- //
#include <Wire.h> // Include the Wire library for I2C communication
#include <RTClib.h> // Include the RTClib library for accessing the RTC
#include <SPI.h> // Include the SPI library for SPI communication
#include <SD.h> // Include the SD library for SD card operations
RTC_DS1307 rtc; // Create an instance of the RTC_DS1307 class
File myFile; // Create a File object to handle file operations
const int chipSelect = 5; // CS pin for the SD card module
void setup() {
Serial.begin(9600); // Initialize serial communication
Wire.begin(21, 22); // Initialize I2C communication with the specified pins
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
// Initialize SD card
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
while (1);
}
Serial.println("Card initialized.");
// Create or open the text file
myFile = SD.open("timeLog.txt", FILE_WRITE);
if (myFile) {
myFile.println("Starting time log...");
myFile.close();
} else {
Serial.println("Error opening timeLog.txt");
}
}
void loop() {
DateTime now = rtc.now(); // Get the current date and time from the RTC
// Check if it's specific times and log them
if (now.hour() == 2 && now.minute() == 38 && now.second() == 0) {
logTime("It's 2:38 AM");
} else if (now.hour() == 2 && now.minute() == 39 && now.second() == 0) {
logTime("It's 2:39 AM");
} else if (now.hour() == 2 && now.minute() == 40 && now.second() == 0) {
logTime("It's 2:40 AM");
}
delay(1000); // Wait for 1 second before checking again
}
void logTime(const String& message) {
Serial.println(message); // Log to serial for debugging
myFile = SD.open("timeLog.txt", FILE_WRITE);
if (myFile) {
myFile.println(message);
myFile.close(); // Close the file to save data
} else {
Serial.println("Error opening timeLog.txt");
}
}