// Include the Wire library to work with I2C #include "Wire.h" // I2C bus address #define DS1307_I2C_ADDRESS 0x68 // Static variables // Declare time variables byte second, minute, hour, weekDay, monthDay, month, year; // Declare the ledPin as a variable int wateringSystem = 13; // Function to convert normal decimal number to numbers 0-9 (BCD) byte decBcd(byte val) { return ( (val/10*16) + (val%10) ); } // Function to convert numbers 0-9 (BCD) to normal decimal number byte bcdDec(byte val) { return ( (val/16*10) + (val%16) ); } // Function to set the system clock void configureTime() { // 1) Set the date and time values second = 00; minute = 25; hour = 13; weekDay = 1; monthDay = 29; month = 10; year = 12; // 2) Commands to start up the clock Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.write(0x00); Wire.write(decBcd(second)); Wire.write(decBcd(minute)); Wire.write(decBcd(hour)); Wire.write(decBcd(weekDay)); Wire.write(decBcd(monthDay)); Wire.write(decBcd(month)); Wire.write(decBcd(year)); Wire.endTransmission(); } // Function to call up the system time void requestTime() { // Reset the pointer to the register Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.write(0x00); Wire.endTransmission(); // Call up the time and date Wire.requestFrom(DS1307_I2C_ADDRESS, 7); second = bcdDec(Wire.read() & 0x7f); minute = bcdDec(Wire.read()); hour = bcdDec(Wire.read() & 0x3f); weekDay = bcdDec(Wire.read()); monthDay = bcdDec(Wire.read()); month = bcdDec(Wire.read()); year = bcdDec(Wire.read()); // Print the date and time via serial monitor Serial.print(hour, DEC); Serial.print(":"); Serial.print(minute, DEC); Serial.print(":"); Serial.print(second, DEC); Serial.print(" "); Serial.print(monthDay, DEC); Serial.print("/"); Serial.print(month, DEC); Serial.print("/"); Serial.print(year, DEC); Serial.print(" "); } void setup() { // Initialize the I2C Wire.begin(); // Initialize the serial port Serial.begin(57600); // Initialize the LED pin as an output pinMode(wateringSystem, OUTPUT); digitalWrite(wateringSystem, LOW); // Set the time: ONLY THE FIRST TIME YOU RUN THE CODE!! configureTime(); } void loop() { // Small time delay: delay(2000); // Read the time and date every 2 seconds requestTime(); Serial.println(" "); // Based on the exact time if (hour == 13) { if (minute == 40) { // Turn on the watering system for the plants Serial.println("13:40 --> Time to water the plants"); digitalWrite(wateringSystem, HIGH); } } }