RTC alarm to trigger function at different times for multiple instances.

Greetings,

I am trying to make an alarm clock that triggers an event at a different time each 'day' for a predetermined number of days*. I have a DS1307 RTC connected to a NANO and can read the time correctly but I am having trouble comparing the RTC time and my times that I have saved in two arrays.

Basically i want something like this for example:

At 06;30 on day 1 perform function (blink led in this example)
At 06;35 on day 2 perform function (blink led in this example)
At 06;40 on day 3 perform function (blink led in this example)
At 06;45 on day 4 perform function (blink led in this example)
At 07;21 on day 5 perform function (blink led in this example)
end

P.s. I realize my code isn't waiting days but for testing i obviously want the times to be close together in the same day. the blinking LED will be replaced with a function that moves a servo for 11 hours so that.

Any help would be greatly appreciated.

thanks

#include "RTClib.h"

RTC_DS1307 rtc;

const int ledPin = 3; 

///////////////////////////////////
// alarm times defined here 
// eg first alarm @hours[0], minutes[0] = 17:59
// second alarm @hours[1], minutes[1] = 18:00
int hours[] = {17,18,18,18,18,18};
int minutes[] = {59,0,1,3,5,7};


int hour_;
int minute_;


void setup () {
  
  pinMode(ledPin, OUTPUT);
  
  while (!Serial);

  Serial.begin(57600);
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
  }
  // following line sets the RTC to the date & time this sketch was compiled
  // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  // This line sets the RTC with an explicit date & time, for example to set
  // January 21, 2014 at 3am you would call:
  // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}

void loop () {
    DateTime now = rtc.now();


    Serial.print(now.hour(), DEC);      // debug print
    Serial.print(':');
    Serial.println(now.minute(), DEC);
    
    hour_ = now.hour();                 // store current hour as integer 
    minute_ = now.minute();             // store current minute as integer
  
    Serial.print(hour_);                // debug print
    Serial.print(':');
    Serial.println(minute_);


for (int day_count = 1;day_count <= 7; day_count++)
{
Serial.print("DAY COUNT: ");
Serial.println(day_count);

 if (hour_== hours[day_count])
 {
    if(minute_== minutes[day_count])
    {
      Serial.println("...................");
      Serial.println("the time is now!");
      digitalWrite(ledPin, HIGH);
      delay(1000);                          // debug deay short for testing, real function will take ~11 hours.  
      digitalWrite(ledPin, LOW);    
    }
  }
 }
}

I see three things that look wrong.

There are 7 days in a week, but you have only six alarm times.

The six alarm times have zero based indexes. That is the index numbers for the elements are 0 through 5

The minutes match will be true for many passes through loop, and your function will repeat. You can use a boolean control variable to only do it once.

Greetings,

thanks for the help.

I have adjusted my code and this is the result that seems to work well

#include "RTClib.h"

RTC_DS3231 rtc;

const int ledPin = 3; 

///////////////////////////////////
// alarm times defined here 
// eg first alarm @hours[0], minutes[0] = 17:59
// second alarm @hours[1], minutes[1] = 18:00
int hours[] = {15,15,15,15,15,15};
int minutes[] = {03,4,5,7,8,10};

int number_of_stored_values_in_hours_array = sizeof(hours)/sizeof(int);

int hour_;
int minute_;
int day_count = 0;
bool alreadyRun = false; 

void setup () 
{
  
  pinMode(ledPin, OUTPUT);

#ifndef ESP8266
  while (!Serial);                                  // for Leonardo/Micro/Zero
#endif

  Serial.begin(9600);

  delay(3000);                                     // wait for console opening

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, lets set the time!");
    // If the RTC have lost power it will sets the RTC to the date & time this sketch was compiled in the following line
    //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }
}

void loop () {
 
 while(alreadyRun == false)
 {   
    DateTime now = rtc.now();
    Serial.print(now.hour(), DEC);      // debug print
    Serial.print(':');
    Serial.println(now.minute(), DEC);
    
    hour_ = now.hour();                 // store current hour as integer 
    minute_ = now.minute();             // store current minute as integer
  
    Serial.print(hour_);                // debug print
    Serial.print(':');
    Serial.println(minute_);

Serial.print("DAY COUNT: ");
Serial.println(day_count);

 if (hour_== hours[day_count])
 {
    if(minute_== minutes[day_count])
    {
      if (alreadyRun == false){
        Serial.println("...................");    // do this once per day at the desired time. 
        Serial.println("the time is now!");
        digitalWrite(ledPin, HIGH); 
        delay(5000);                          // debug deay short for testing, real function will take ~11 hours.  
        digitalWrite(ledPin, LOW); 
        alreadyRun= true;  
      }  
    }
  }
 }

day_count++; 
if(day_count == number_of_stored_values_in_hours_array)
{
  while(1)
  {
    Serial.println("ALL DONE!... waiting here.");
  }
}
alreadyRun = false; 
}