SOLVED-- RTC DS1307-- cannot trigger event with day, month, year, dayOfWeek

hello,

I’ve been beating on this for days-- I want to trigger audio files and other events according to a schedule. I hacked up a simple sketch for testing, and it reliably prints the text string when the second, minute, or hour match, but not day, dayOfWeek, month, or year. When I try to match day, dayOfWeek, month, or year it does nothing. I hope someone can help; I’ve spent so much time trying to find an answer online it’s all becoming a blur. Thanks in advance!

I’m using an Arduino Uno R2 with a Wave shield
Arduino IDE 1.01
Adafruit RTClib Downloads · adafruit/RTClib · GitHub
Adafruit DS1307 RTC DS1307 RTC tutorial

#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 RTC;

int second = 0;       
int minute = 0;   
int hour = 0;      
int day = 0;    
int month = 0;   
int year = 0; 
int dayOfWeek = 0;
 
void setup () {
    Serial.begin(9600);
    Serial.print("\nThe current date and time will be updated every three seconds:\n\n");
    Wire.begin();
    RTC.begin();   
}

void loop() {
  
    get_date();
    get_time(); 
  
  /* Print the current dayOfWeek, date and time */

    DateTime now = RTC.now();
    
    Serial.print(now.dayOfWeek(), DEC);
    Serial.print(" day of week\n");    
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println(); 
    delay(3000);   

    
    /* simple if statement that triggers printing a sentence 
    /* on matching year, month, day, dayOfWeek, hour, minute, or  second */

     if (hour == 12){
    Serial.print("\nhello, we have a match. \n"); 
    delay(3000);
     }

}

void get_date() 
  {
   Wire.beginTransmission(104); 
   Wire.write(0);
   Wire.endTransmission();
   Wire.requestFrom(104, 3);
   dayOfWeek  = bcdToDec(Wire.read());
   day   = bcdToDec(Wire.read());
   month = bcdToDec(Wire.read());
   year  = bcdToDec(Wire.read());
  }
  
void get_time()
  {
   Wire.beginTransmission(104); 
   Wire.write(0);//set register to 0
   Wire.endTransmission();
   Wire.requestFrom(104, 3);
   second = bcdToDec(Wire.read() & 0x7f);
   minute = bcdToDec(Wire.read());
   hour = bcdToDec(Wire.read() & 0x3f);
  }
     
byte bcdToDec(byte val)
  {
   return ( (val/16*10) + (val%16) );
  }
  
byte decToBcd(byte val)
{
  return ( (val/10*16) + (val%10) );
}

This is the output from matching the second, minute, or hour:

The current date and time will be updated every three seconds:

4 day of week
2012/8/9 12:9:0

hello, we have a match.

Output from trying to match day, dayOfWeek, month, or year:

The current date and time will be updated every three seconds:

4 day of week
2012/8/9 12:16:37
4 day of week
2012/8/9 12:16:40

Not sure why you are getting the date and time two different ways? Perhaps you should use only one way:

#include <Wire.h>
#include "RTClib.h"

int second = 0;       
int minute = 0;   
int hour = 0;      
int day = 0;    
int month = 0;   
int year = 0; 
int dayOfWeek = 0;
 
void setup () {
    Serial.begin(9600);
    Serial.print("\nThe current date and time will be updated every three seconds:\n\n");
    Wire.begin();
}

void loop() {
  
    get_date();
    get_time(); 
  
  /* Print the current dayOfWeek, date and time */
   
    Serial.print(dayOfWeek, DEC);
    Serial.print(" day of week\n");    
    Serial.print(year, DEC);
    Serial.print('/');
    Serial.print(month, DEC);
    Serial.print('/');
    Serial.print(day, DEC);
    Serial.print(' ');
    Serial.print(hour, DEC);
    Serial.print(':');
    Serial.print(minute, DEC);
    Serial.print(':');
    Serial.print(second, DEC);
    Serial.println(); 
    delay(3000);   

    
    /* simple if statement that triggers printing a sentence 
    /* on matching year, month, day, dayOfWeek, hour, minute, or  second */

     if (hour == 12){
    Serial.print("\nhello, we have a match. \n"); 
    delay(3000);
     }

}

void get_date() 
  {
   Wire.beginTransmission(104); 
   Wire.write(0);
   Wire.endTransmission();
   Wire.requestFrom(104, 3);
   dayOfWeek  = bcdToDec(Wire.read());
   day   = bcdToDec(Wire.read());
   month = bcdToDec(Wire.read());
   year  = bcdToDec(Wire.read());
  }
  
void get_time()
  {
   Wire.beginTransmission(104); 
   Wire.write(0);//set register to 0
   Wire.endTransmission();
   Wire.requestFrom(104, 3);
   second = bcdToDec(Wire.read() & 0x7f);
   minute = bcdToDec(Wire.read());
   hour = bcdToDec(Wire.read() & 0x3f);
  }
     
byte bcdToDec(byte val)
  {
   return ( (val/16*10) + (val%16) );
  }
  
byte decToBcd(byte val)
{
  return ( (val/10*16) + (val%10) );
}

As usual, as soon as I ask for help I find the answer. The get_date and get_time functions are unnecessary, and this is the correct syntax for getting the current time or date for the if statement:

if ((now.dayOfWeek() == n)

So this sketch works:

#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 RTC;

int second = 0;       
int minute = 0;   
int hour = 0;      
int day = 0;    
int month = 0;   
int year = 0; 
int dayOfWeek = 0;
 
void setup () {
    Wire.begin();
    RTC.begin(); 
    Serial.begin(9600);
    Serial.print("\nThe current date and time will be updated every three seconds:\n\n");
  }

void loop() {
  
    DateTime now = RTC.now();
    
    Serial.print(now.dayOfWeek(), DEC);
    Serial.print(" day of week\n");    
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println(); 
    delay(3000);
     
/* compare dayOfWeek, year, month, day, hour, minute, or second */
      if (now.dayOfWeek() == 4) {
    Serial.print("\nhello, we have a match. \n"); 
    }
    else {
      Serial.print("\nSorry, your numbers don't match. \n");
    }  
     
}