How to set an alarm with the DS1302 Real Time Clock

Hi,

I am using the simple example code for the DS1302 Real Time Clock, I want to perform an action at a specific date & time but cant get the syntax right, I tried the simple if statement below but this does not work, any pointers appreciated.

if (datestring == '06/02/2019 18:16:00')
{
Serial.println("Bingo");
}

// CONNECTIONS:
// DS1302 CLK/SCLK --> 5
// DS1302 DAT/IO --> 4
// DS1302 RST/CE --> 2
// DS1302 VCC --> 3.3v - 5v
// DS1302 GND --> GND

#include <ThreeWire.h>  
#include <RtcDS1302.h>

//ThreeWire myWire(4,5,2); // IO, SCLK, CE
ThreeWire myWire(8,2,10); // IO, SCLK, CE
RtcDS1302<ThreeWire> Rtc(myWire);
char datestring[20];

void setup () 

//
// Real Time Clock Setup
//
{
    Serial.begin(9600);

    Serial.print("compiled: ");
    Serial.print(__DATE__);
    Serial.println(__TIME__);

    Rtc.Begin();

    RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
    printDateTime(compiled);
    Serial.println();

    if (!Rtc.IsDateTimeValid()) 
    {
        // Common Causes:
        //    1) first time you ran and the device wasn't running yet
        //    2) the battery on the device is low or even missing

        Serial.println("RTC lost confidence in the DateTime!");
        Rtc.SetDateTime(compiled);
    }

    if (Rtc.GetIsWriteProtected())
    {
        Serial.println("RTC was write protected, enabling writing now");
        Rtc.SetIsWriteProtected(false);
    }

    if (!Rtc.GetIsRunning())
    {
        Serial.println("RTC was not actively running, starting now");
        Rtc.SetIsRunning(true);
    }

    RtcDateTime now = Rtc.GetDateTime();
    if (now < compiled) 
    {
        Serial.println("RTC is older than compile time!  (Updating DateTime)");
        Rtc.SetDateTime(compiled);
    }
    else if (now > compiled) 
    {
        Serial.println("RTC is newer than compile time. (this is expected)");
    }
    else if (now == compiled) 
    {
        Serial.println("RTC is the same as compile time! (not expected but all is fine)");
    }
}

void loop () 
{
    RtcDateTime now = Rtc.GetDateTime();

    printDateTime(now);
    Serial.println();
    //Serial.println (datestring);
    if (datestring == '06/02/2019 18:16:00')
    {
      Serial.println("Bingo");
    }


    if (!now.IsValid())
    {
        // Common Causes:
        //    1) the battery on the device is low or even missing and the power line was disconnected
        Serial.println("RTC lost confidence in the DateTime!");
    }

    delay(1000); // ten seconds
}

#define countof(a) (sizeof(a) / sizeof(a[0]))

void printDateTime(const RtcDateTime& dt)
{
    //char datestring[20];

    snprintf_P(datestring, 
            countof(datestring),
            PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
            dt.Month(),
            dt.Day(),
            dt.Year(),
            dt.Hour(),
            dt.Minute(),
            dt.Second() );
    Serial.print(datestring);
}

Are there not examples in the library for this ? I’ve used the DS3231 ( cheap
And very accurate) and it has alarm registers built in that you just set from the Arduino.

hammy:
Are there not examples in the library for this ?

Not for the 1302, no...
Just not sure how I should code this...

Use strcmp.

With this global declaration and your 19 character format, datestring will be null terminated c- string filled by the printDateTime(now) function.

char datestring[20];

Then test for a match with

if(strcmp(datestring, "06/02/2019 18:16:00")==0)

Working with a character datestring of the date/time is an unusual approach with RTC's, but in your case is should work. I'm not familiar with the library you are using, but I expect there may be "unix time" values available, or else work directly with the time date component values as bytes or ints.

cattledog:
if(strcmp(datestring, "06/02/2019 18:16:00")==0)

This works !
Thank you for your assistance, greatly appreciated.

There are libraries !!