Go Down

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

UKHeliBob

From the TimeAlarms readme
Quote
write(ID,  value);  -  write the value (and enable) the alarm for the given ID 
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

tochinet

Joining the thread. I was a bit surprised to see that including the  TimeAlarms.h library in my sketch directly adds more than 2000 bytes in the compiled program (but only 70 bytes of RAM) while my use of it is quite limited : I replace two delays by Alarm.delay and I create one alarm that fires at midnight.

The code is below.

Quote
void smallDelay() {
#ifdef TimeAlarms_h
  Alarm.delay(100); // Allows interrupts
#else
  delay(100);
#endif
}

#ifdef TimeAlarms_h
void MidnightAlarm(){ 
  // Serial.println("It's midnight");
  int diffTITH =0 ;
  for (byte i=0; i<sizeof(dtTI24); i++) {
    if (dtTI24 && dtTH24) diffTITH += dtTI24-dtTH24;   // diff=240/deg
    else { diffTITH=0; break; }
  }
  if (millis()>1000UL*3600*24) dtAnalogConfig[5]+= diffTITH /80 ; // scaled in 1/3 degrees at first
 
  // Replace by semaphore getRegister(REGI_ANALOGCONFIG)->getData(); // To check, changes only after min 1 day without reset
  if (day()<sizeof(dtSamples)) dtSamples[day()]=diffTITH /80;
}
#endif


 // and later

#ifdef TimeAlarms_h
  Alarm.alarmRepeat(0,0,0, MidnightAlarm);  // Midnight every day
#endif


#ifdef TimeAlarms_h
  Alarm.delay(1000L*loopTime - (1100+400*ADCstab));  // Sleep till txInterval, enabling alarms
#else
  delay(1000L*loopTime - (1100+400*ADCstab));  // Sleep till txInterval
#endif 
Commenting the midnight alarm part (i.e. only keeping the include and the replacement of delay by alarm.delay) will only "save" 200 bytes. Is there a logical/simple justification for such a large memory usage in EEPROM ?

aarg

Maybe it takes that amount of code to implement the functionality... did you look at the source?
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

marinbnm

If i want to see what day/hour/minute/sec will be after some time in past or future i use:

TimeSpan operator+(const TimeSpan& right);

But, what if i know dates like 12/07/2018 18:00 and is there function to calculate how much houres passed from that time till now.time()?

DKWatson

All modern (computer-based) timing uses Unix time which is a 32-bit value in seconds since 00:00 Jan 1, 1970. For the convenience of humans, this value is converted into something they understand. Underneath it all remains the base time that can be added to, subtracted from, up to you, just treat it as a long variable. Google unix time and you'll find heaps of stuff on how to play with it.
Live as if you were to die tomorrow. Learn as if you were to live forever. - Mahatma Gandhi

njotha

Hi,
I'm trying to localize the strftime() output to German, but setlocale (LC_ALL, "de_DE") has no effect at all. Aunt Google has many answers for php, but no help for arduino. I'm including time.h and locale.h.

It's for the Heltec WiFi Kit 32 with OLED display

      setlocale (LC_ALL, "");
      setlocale (LC_ALL, "de_DE"); // or "de_DE.UTF8"
      struct tm * tmPointer = localtime(& tvTimeValue.tv_sec);
      // Display the date.
     
      strftime(chBuffer, sizeof(chBuffer), "%A",  tmPointer); 
      u8g2.setFont(u8g2_font_9x15_tr);
      u8g2.drawStr(64 - (u8g2.getStrWidth(chBuffer) / 2), 0, chBuffer);
 
      strftime(chBuffer, sizeof(chBuffer), "%d. %B",  tmPointer);   
      u8g2.setFont(u8g2_font_9x15_tr);
      u8g2.drawStr(64 - (u8g2.getStrWidth(chBuffer) / 2), 18, chBuffer);

deliver the English names for days of the week and for months - I'd very much like to see the German ones.

Any help would be welcome.

odometer

deliver the English names for days of the week and for months - I'd very much like to see the German ones.

Any help would be welcome.
That's what happens when you use (relatively) high-level libraries written by someone else.

If you can get at the numeric month, you can use it as an index to an array with the month names, and use that. You can use a similar trick for the day of the week.

jsinghvirdi

How to set Alarm.alarmRepeat() from out side functions.

I am trying to set Alarm With LCD and Keypad but not able to do the same.

Please let me know how i can set Alarm parameters in other functions.


PaulS

Quote
How to set Alarm.alarmRepeat() from out side functions.
You can't call that method from outside a function.

Quote
I am trying to set Alarm With LCD and Keypad but not able to do the same.
Must be something wrong with your code. Posting your code using a zero point font was silly.

Quote
Please let me know how i can set Alarm parameters in other functions.
Exactly like you would in the "magic" functions setup() and loop().
The art of getting good answers lies in asking good questions.

jsinghvirdi

Sorry, my question is is it possible to create Alarm from other functions instead of setup() or loop() functions.

Like This :

void SetTimer(){

 Alarm.alarmRepeat(8,30,0, MorningAlarm);

}

Instead Of :

void setup(){

 Alarm.alarmRepeat(8,30,0, MorningAlarm);

}
You can't call that method from outside a function.
Must be something wrong with your code. Posting your code using a zero point font was silly.
Exactly like you would in the "magic" functions setup() and loop().

UKHeliBob

Quote
is it possible to create Alarm from other functions instead of setup() or loop() functions.
Yes. There is nothing special about the setup() and loop() functions when it comes to setting up alarms.

Of course, you could have just tried it for yourself.  If you did try and it failed then post your code here for help.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

jsinghvirdi

Thanks UKHeliBob.

I got it and now its working but i have another question:

When i am using setTime(12,41,50,1,1,11); function in setup(), all working fine but if i did not use it no alarm will works.

Means:

I have implemented code to set Time by using setTime(12,41,50,1,1,11); and program to arduino mega, at this time clock shows exact time 12:41:50

but when i commented the setTime() line and reprogram the arduino. Now what happened the clock shows 00:00:00

On every reset or restart the arduino time clock shown me 00:00:00 and alarm is not working.


So i have wright code for Set Clock, now after set Clock successfully the time will shown exact the updated time, but when i restart the arduino the clock will again starts from 00:00:00.

Please help me....

cattledog

Quote
So i have wright code for Set Clock, now after set Clock successfully the time will shown exact the updated time, but when i restart the arduino the clock will again starts from 00:00:00.

Please help me....
Your code to set the time is just referencing the Time library, which uses the the internal Arduino millis() function, to some outside time.  The Arduino will not remember this setting through powercycles/restarts. The internal millis() function will restart at 0 which the Time library interprets as 00:00:00.

It sounds like you want to use a Real Time Clock(RTC) hardware device if you want to remember the actual time through restarts. The DS3231 is a good one.

ayawatson

Hi,

I would like to display the remaining value before the next timer repeats itself.

So far I've managed to execute a function based on a timer, however I really struggle to find a way to display remaining time.

Here is the code I use (based on the great examples provided with the library).

I could really use some help.

Thanks




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;
  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();
  Alarm.delay(0); // wait one second between clock display
}


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







UKHeliBob

Have you looked at the low level functions referenced in the readme for the library ?

The read(ID) function may be of help but I have never investigated it.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up