TimeAlarms, using Functions with variables

I would like to know, how can i pass variables to the functions i use in the Alarms..

Something like:

AlarmRepeat(1,1,1,Function1(30));

void Function1(int data){

//Do something

}

I would like to know, how can i pass variables to the functions i use in the Alarms..

Write your own library. The TimeAlarms class does not, and rightly so, support passing arguments to callbacks.

PaulS:
Write your own library. The TimeAlarms class does not, and rightly so, support passing arguments to callbacks.

Wow… that was of great help…

So, if i want to really create(or modify the current library) where should i start?

So, if i want to really create(or modify the current library) where should i start?

With an explicit statement of, and justification for, the requirements.

Think about this, though. The doorbell rings. You (the callback) need to answer the door. What do you need to know this time (the argument) that is different from last time?

If that information is known when the code is compiled, so that you can supply it to the callback declaration function, why can't the information simply be declared in the callback function?

Or you could simply check if it's time do to whatever you need to do...
The Time Alarms is a very nice library, but it's very easy to do the same only with the Time library, and the maintance are alot easier when you made all the code...

if (hour() == 12 && minute() == 0 && second() == 0) {
  // answer the door
}

Or maybe, depending on what you really want to do you could even use millis() that is an arduino based "clock", which resets everytime you reset the arduino.

if (millis() == 10000) {
  // answer the door
}

I know it is quite old topic, but I was trying to achieve similar thing and my search led me here. I wanted to create dynamic alarm to switch color of led strip (ws2812) which can be set remotely from other device (mobile app for example).

I didn’t find here the answer I was hoping for, but I found a simple work around. Each alarm has its own ID, which can be simply obtained inside callback. Same ID is returned when alarm is created. What you need to do is to create any structure which bonds this ID to variables you need. I used array of structs because I need to store more values.

// ID stored inside Struct

#include <TimeAlarms.h>  
#include <Adafruit_NeoPixel.h>

struct LedTrigger{
    int hour;
    int minute;
    int second;
    AlarmId alarmId;
    uint32_t color;
};

LedTrigger defaultTrigger = {
    .hour = 20, 
    .minute = 30, 
    .second = 40, 
    .alarmId = 0, 
    .color = led.Color(200,100,0),
};


LedTrigger triggers[40];
int triggerCount = 0;

triggers[triggerCount++] = defaultTrigger;

Then I can create alarm and save its ID

triggers[i].alarmId = Alarm.alarmRepeat(settings.triggers[i].hour, settings.triggers[i].minute, settings.triggers[i].second, ledTrigger);

And inside callback i can obtain ID and use it to ge

void ledTrigger(){
    AlarmId id = Alarm.getTriggeredAlarmId();
    uint32_t color = getTriggerColor(id);
    settings.ledColor = color;
    setOneColor(color);
}

void setOneColor(uint32_t color){
    settings.ledColor = color;
    for (int i = 0; i<LED_COUNT;i++){
        led.setPixelColor(i, color);
    }
    led.show();
}

uint32_t getTriggerColor(AlarmId id){
    for (int i = 0; i<settings.triggerCount;i++){
        if (settings.triggers[i].alarmId == id) return settings.triggers[i].color;
    }
    return led.Color(0,0,0);
}