Go Down

Topic: Time and TimeAlarms Libraries – Ask here for help or suggestions (Read 217154 times) previous topic - next topic

cattledog

The function you want is Alarm.getNextTrigger().
Code: [Select]
// returns the absolute time of the next scheduled alarm, or 0 if none
     time_t TimeAlarmsClass::getNextTrigger()
     {
     time_t nextTrigger = 0xffffffff;  // the max time value
     
        for(uint8_t id = 0; id < dtNBR_ALARMS; id++)
        {
          if(isAllocated(id) )
          {
        if(Alarm[id].nextTrigger <  nextTrigger)
       nextTrigger = Alarm[id].nextTrigger;
          }     
    }
        return nextTrigger == 0xffffffff ? 0 : nextTrigger; 
     }


Code: [Select]
#include <TimeLib.h>
#include <TimeAlarms.h>

AlarmId id;

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

  // create timers, to trigger relative to when they're created
  //float repeatInDays = 0.00004;
  float repeatInDays = 0.0004;
  Serial.println("Repeat Cycle value in days is :");
  Serial.println(repeatInDays);
  long daysToSeconds = repeatInDays * 86400;
  Serial.println("Repeat Cycle value in Seconds is :");
  Serial.println(daysToSeconds);

  Alarm.timerRepeat(daysToSeconds, Repeats);           // timer

}

void loop() {
  //digitalClockDisplay();
  Serial.print("Remaining Seconds = ");
  Serial.println(Alarm.getNextTrigger() - now());
  Alarm.delay(1000); // wait one second between clock display
}


void Repeats() {
  Serial.println("Triggered");
}



ayawatson

Thank you, it works perfectly.

Code: [Select]
#include <TimeLib.h>
#include <TimeAlarms.h>

AlarmId id;

void setup() {
  Serial.begin(115200);
  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 timers, to trigger relative to when they're created
  Alarm.timerRepeat(5, Repeats);           // timer for every 15 seconds

}

void loop() {
  Serial.println("Time right now");
  digitalClockDisplay();
  Serial.println("Next trigger time");
  triggerTimeDisplay();
  Alarm.delay(1000); // wait one second between clock display
}


void Repeats() {
  Serial.println(" ");
  Serial.println("TRIGGER");
  Serial.println(" ");
 
 

}




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

void triggerTimeDisplay() {
  Serial.print(day(Alarm.getNextTrigger()));
  Serial.print("/");
  Serial.print(month(Alarm.getNextTrigger()));
  Serial.print("/");
  Serial.println(year(Alarm.getNextTrigger()));
 
  Serial.print(hour(Alarm.getNextTrigger()));
  printDigits(minute(Alarm.getNextTrigger()));
  printDigits(second(Alarm.getNextTrigger()));
  Serial.println();
}

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

mbenitez01

all I get (tryng to compile) is following error...

collect2.exe: error: ld returned 1 exit status

UKHeliBob

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

waqaspak

Alarm.alarmRepeat(8,30,0, switchON);  // 8:30am every day
Alarm.alarmRepeat(17,45,0,switchOFF);  // 5:45pm every day


my goal is to keep the switch on between 8:30am to 5:45pm and off otherwise
the problem is when my arduino does not have power supply at 8:30am it does not make the switchON because it has missed its alarm, when my arduino gets power at 8:45am or something,the switch is not ON.
any suggestion plz help

UKHeliBob

If the Arduino is turned on after 08:30 then call the switchON() function from setup()
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

waqaspak

If the Arduino is turned on after 08:30 then call the switchON() function from setup()

If I do that then how come timealarms.h library useful to me then I can also do it without timealarms.h

Plus in my project there are so many scheduled functions for which timealarms.h is very good and important

I need something to call the missed alarams by timealarms.h

UKHeliBob

Quote
how come timealarms.h library useful to me then I can also do it without timealarms.h
You can do what you want without using TimeAlarms by checking the time from the RTC yourself

Quote
in my project there are so many scheduled functions for which timealarms.h is very good and important
Note that unless you have made a change to TimeAlarms.h to increase the dtNBR_ALARMS constant then only 6 alarms can be active at a time.

Quote
I need something to call the missed alarams by timealarms.h
In the other thread where you posted the problem it was suggested that when an alarm is triggered that you write a flag to EEPROM that can be read at startup to determine whether each alarm had been triggered or not.  This seems to be a sensible suggestion.

It may also be possible to determine that an alarm has been missed by using the Alarm.read() function but I have never used it.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

MBC99

Hi there.
I'm currently working on a project to control the heating system on my home (link to the thread: http://forum.arduino.cc/index.php?topic=572413.0)
Now I'm on the final stage where I use the TimeAlarms to turn on or off the DHW tank at specific times.
I'm using DS3231RTC.h library.
To start with something easy I have used the existing example from the TimeAlarms and added the DS3231 part:
Code: [Select]
void setup() {
  Serial.begin(9600);
  while (!Serial) ; // wait for Arduino Serial Monitor
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if (timeStatus() != timeSet)
    Serial.println("Unable to sync with the RTC");
  else
    Serial.println("RTC has set the system time");

  setTime(9, 29, 45, 1, 1, 11);
  RTC.set(now());
 
  // create the alarms, to trigger at specific times
  Alarm.alarmRepeat(9, 30, 0, onProg);
}

void onProg()
{
  Serial.print("Success");
}


Once this was working I tried adding the code to my code project (I only attach the setup(), if you consider that the rest of the code is needed ther's no problem, but the code is long and the time part is short):

Code: [Select]
void setup() {
  Serial.begin(9600);

  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if (timeStatus() != timeSet)
    Serial.println("Unable to sync with the RTC");
  else
    Serial.println("RTC has set the system time");

  pinMode(PINACS, OUTPUT);
  pinMode(PINCALE, OUTPUT);
  pinMode(PINFAT, OUTPUT);
  pinMode(PINCALDERA, OUTPUT);
  pinMode(PINTERMOSTAT, INPUT_PULLUP);
  pinMode(PINBOMBACIR, INPUT_PULLUP);
  pinMode(PINTERMOFAT, INPUT_PULLUP);
  termostatActiu = false;
  canviEstat = false;
  ACSON = false;

  sensors.begin();
  sensors.setResolution(ACS, resolution);

  sensors.setWaitForConversion(false);
  sensors.requestTemperatures();

  delayInMillis = 750 / (1 << (12 - resolution));
  lastTempRequest = millis();

  setTime(9, 29, 40, 1, 1, 2019);
  RTC.set(now());

  Alarm.alarmRepeat(9, 30, 0, onProg);
  Alarm.alarmRepeat(14, 0, 0, offProg);
  Alarm.alarmRepeat(dowMonday , 16, 0, 0, onProg);
  Alarm.alarmRepeat(dowTuesday , 16, 0, 0, onProg);
  Alarm.alarmRepeat(dowWednesday , 16, 0, 0, onProg);
  Alarm.alarmRepeat(dowThursday , 16, 0, 0, onProg);
  Alarm.alarmRepeat(dowFriday , 16, 0, 0, onProg);
  Alarm.alarmRepeat(dowSaturday , 16, 0, 0, onProg);
  Alarm.alarmRepeat(19, 30, 0, offProg);
  Alarm.alarmRepeat(23, 0, 0, offProg);
  Alarm.timerRepeat(15, onProg);
 
  joystickBtns.begin();
  pinMode(LED_BUILTIN, OUTPUT);
  Wire.begin();
  oled.begin(&Adafruit128x64, OLED_I2C_ADDRESS); //check config
  oled.setFont(menuFont);
  oled.clear();
}


To keep things simple the onProg method is as follows:
Code: [Select]
void onProg()
{
  Serial.print("Success");
}


With all these I think the alarm should be trigered, but it doesn't.
On loop() I've added this:
Code: [Select]
Serial.print(hour());
  Serial.print("\t");
  Serial.print(minute());
  Serial.print("\t");
  Serial.println(second());


In order to see the time and make sure it works; it does. The time sets up to what I've put on the setup(), the problem is that at 9:30 the alarm doesn't trigger...

PaulS

Have you read the part about there being a limit on the number of timers you can set? Have you made the appropriate changes needed to support the number of timers you are trying to use?

Why DO you need to use this library? Having the Arduino do something at the same time every day is challenging without a RTC. With one, it is trivial.
The art of getting good answers lies in asking good questions.

MBC99

Have you read the part about there being a limit on the number of timers you can set? Have you made the appropriate changes needed to support the number of timers you are trying to use?

No, I have realised this mistake while I was looking 3 posts above mine. However, I've reduced the alarms to one (only the 9:30 one) and it still doesen't work:
Code: [Select]
void setup() {
  Serial.begin(9600);

  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if (timeStatus() != timeSet)
    Serial.println("Unable to sync with the RTC");
  else
    Serial.println("RTC has set the system time");

  pinMode(PINACS, OUTPUT);
  pinMode(PINCALE, OUTPUT);
  pinMode(PINFAT, OUTPUT);
  pinMode(PINCALDERA, OUTPUT);
  pinMode(PINTERMOSTAT, INPUT_PULLUP);
  pinMode(PINBOMBACIR, INPUT_PULLUP);
  pinMode(PINTERMOFAT, INPUT_PULLUP);
  termostatActiu = false;
  canviEstat = false;
  ACSON = false;

  sensors.begin();
  sensors.setResolution(ACS, resolution);

  sensors.setWaitForConversion(false);
  sensors.requestTemperatures();

  delayInMillis = 750 / (1 << (12 - resolution));
  lastTempRequest = millis();

  setTime(9, 29, 40, 1, 1, 2019);
  RTC.set(now());

  Alarm.alarmRepeat(9, 30, 0, onProg);
  /*Alarm.alarmRepeat(14, 0, 0, offProg);
  Alarm.alarmRepeat(dowMonday , 16, 0, 0, onProg);
  Alarm.alarmRepeat(dowTuesday , 16, 0, 0, onProg);
  Alarm.alarmRepeat(dowWednesday , 16, 0, 0, onProg);
  Alarm.alarmRepeat(dowThursday , 16, 0, 0, onProg);
  Alarm.alarmRepeat(dowFriday , 16, 0, 0, onProg);
  Alarm.alarmRepeat(dowSaturday , 16, 0, 0, onProg);
  Alarm.alarmRepeat(19, 30, 0, offProg);
  Alarm.alarmRepeat(23, 0, 0, offProg);
  Alarm.timerRepeat(15, onProg);*/
 
  joystickBtns.begin();
  pinMode(LED_BUILTIN, OUTPUT);
  Wire.begin();
  oled.begin(&Adafruit128x64, OLED_I2C_ADDRESS); //check config
  oled.setFont(menuFont);
  oled.clear();
}


Why DO you need to use this library? Having the Arduino do something at the same time every day is challenging without a RTC. With one, it is trivial.
I think I'm not following you. Do you mean that I could use the integrated alarms on the RTC? If so it's kind of impossible because it only has two alarms.
Or do you mean to just hard-code this part rather than use TimeAlarms?

PaulS

Quote
Or do you mean to just hard-code this part rather than use TimeAlarms?
Read the time from the RTC. If it is time to do something, do it. If not, don't.

The TimeAlarm class does take care of making sure that the thing to be done is done only once, when the time is right, so you'll need to deal with that, too.
The art of getting good answers lies in asking good questions.

UKHeliBob

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

PaulS

I'd also read from the RTC after setting the time, just as a sanity check, to make sure that you have the correct number of values, in the correct order. If you are setting the time to 9:29, but the time that gets read after you do that is 255:255, well, something isn't right.
The art of getting good answers lies in asking good questions.

cattledog

You have not used
Quote
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.
Full documentation of the library is here
https://github.com/PaulStoffregen/TimeAlarms

Both the number of alarms issue and the use of alarm.delay() are covered in the Read Me section.

Go Up