TimeAlarm library is not calling a function

Hi guys!

I am trying to code the sketch for a project I’m making (Arduino nano). A part of this requires me to activate a small water pump twice a day (using a transistor), but I’m having troubles with the TimeAlarm library. I thought I understood how it worked, but now that I can’t see where the mistake is I am actually quite confused. I have a couple of questions regarding the library and my sketch.

I have checked the electronic circuit for the pump and it is working normally, so I’m assuming the problem is in my code.

I have tested the arduino and the circuit with library’s example sketch (I will put this at the end of the post for reference) adding just a digitalWrite(4, HIGH) line in the morningAlarm() function, and it works fine. So I decided to copy the example sketch and delete every function I didn’t need. Then I just renamed the functions. The code is as simple as possible and I am expecting it to just call a function but it doesn’t do anything at all. I have added temporary lines of code to see if the “check” function was being called and it looked like it wasn’t. Have I deleted something that was necessary?

Here is my sketch:

#include <TimeLib.h>
#include <TimeAlarms.h>


void setup() {

  setTime(8,29,50,1,1,11);

  pinMode(4, OUTPUT);
  digitalWrite(4, LOW);

  Alarm.alarmRepeat(8,30,0, check);  // 8:30am every day

}

void loop() {

}

void check() {
  digitalWrite(4, HIGH);
  delay(10000);
  digitalWrite(4, LOW);
}

Here is TimeAlarms library example sketch:

#include <TimeLib.h>
#include <TimeAlarms.h>

AlarmId id;

void setup() {
  Serial.begin(9600);
  while (!Serial) ; // wait for Arduino Serial Monitor

  setTime(8,29,0,1,1,11); // set time to Saturday 8:29:00am Jan 1 2011

  // create the alarms, to trigger at specific times
  Alarm.alarmRepeat(8,30,0, MorningAlarm);  // 8:30am every day
  Alarm.alarmRepeat(17,45,0,EveningAlarm);  // 5:45pm every day
  Alarm.alarmRepeat(dowSaturday,8,30,30,WeeklyAlarm);  // 8:30:30 every Saturday

  // create timers, to trigger relative to when they're created
  Alarm.timerRepeat(15, Repeats);           // timer for every 15 seconds
  id = Alarm.timerRepeat(2, Repeats2);      // timer for every 2 seconds
  Alarm.timerOnce(10, OnceOnly);            // called once after 10 seconds
}

void loop() {
  digitalClockDisplay();
  Alarm.delay(1000); // wait one second between clock display
}

// functions to be called when an alarm triggers:
void MorningAlarm() {
  Serial.println("Alarm: - turn lights off");
}

void EveningAlarm() {
  Serial.println("Alarm: - turn lights on");
}

void WeeklyAlarm() {
  Serial.println("Alarm: - its Monday Morning");
}

void ExplicitAlarm() {
  Serial.println("Alarm: - this triggers only at the given date and time");
}

void Repeats() {
  Serial.println("15 second timer");
}

void Repeats2() {
  Serial.println("2 second timer");
}

void OnceOnly() {
  Serial.println("This timer only triggers once, stop the 2 second timer");
  // use Alarm.free() to disable a timer and recycle its memory.
  Alarm.free(id);
  // optional, but safest to "forget" the ID after memory recycled
  id = dtINVALID_ALARM_ID;
  // you can also use Alarm.disable() to turn the timer off, but keep
  // it in memory, to turn back on later with Alarm.enable().
}

void digitalClockDisplay() {
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.println();
}

void printDigits(int digits) {
  Serial.print(":");
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

I have also tried to upload the code from a different computer, without any positive results. Also, I am not getting any error messages of any kind.

Here are a couple of more questions related to the library:

  1. Does “TimeAlarms” library always need “Time” library too? If so, why?
  2. Is the “alarmRepeat” function appropriate for my purpose?

Thank you for reading my post. Any help would be really appreciated. Thanks!

Alarm.delay(1000);

The .delay() function is required. It does not need an actual value, but it is where the timers are checked.

Alarm.delay(milliseconds);
Alarms and Timers are only checks and their functions called when you use this delay function. You can pass 0 for minimal delay. This delay should be used instead of the normal Arduino delay(), for timely processing of alarms and timers.

https://www.pjrc.com/teensy/td_libs_TimeAlarms.html

Thank you cattledog!

That fixed everything. Great tip