Time and TimeAlarms Libraries – Ask here for help or suggestions

mem:
I hope that gets you going in the right direction

Michael

Michael, thanks a lot for the fragment example,

.cop.

mem, your sketch works great.
But, when I add your sketch to my sketch and add EEPROM library there are something wrong.
Can I place this code (below) in void loop()? not in void setup().

  Alarm.alarmRepeat(19,54,0, MorningAlarm);
  Alarm.alarmRepeat(19,55,30,EveningAlarm);   
  repeatTimer = Alarm.timerRepeat( 1 * SECS_PER_MIN, RepeatTask);

my purpose to place the code in void loop() are i can change "start, interval and stop alarm" parameters from VB anytime and save it to EEPROM.
So, if Arduino Power is OFF I don't have to set "start, interval and stop alarm" parameters.
I have tried my idea and the alarm can't be disabled.

this is the part of my sketch:

#include <Time.h>
#include <TimeAlarms.h>
#include <Wire.h>  
#include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t
#include <EEPROM.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 7, 13, 12, 11, 10);

AlarmID_t doRingIntS1;

void loop()
{
  compareAlarmInt();
  Alarm.delay(0);
  ....
}

void compareAlarmInt()
{
  char c = EEPROM.read (200); //hourEEPROM    ==>1//START HOUR
  char d = EEPROM.read (201); //minuteEEPROM  ==>1//START MINUTE
  char e = EEPROM.read (202); //hourEEPROM    ==>1//STOP HOUR
  char f = EEPROM.read (203); //minuteEEPROM  ==>1//STOP MINUTE
  char g = EEPROM.read (204); //hourEEPROM    ==>1//INTERVAL HOUR
  char h = EEPROM.read (205); //minuteEEPROM  ==>1//INTERVAL MINUTE
  Alarm.timerRepeat(c, d, 0, StIntSlave1); //==start alarm mode interval slave 1
  Alarm.timerRepeat(e, f, 0, EndIntSlave1); //==End alarm mode interval slave 1
  g = g * 60;
  h = h + g;
  alarmIntervalSlave1 =  Alarm.timerRepeat( h * SECS_PER_MIN, doRingIntS1 );
}
/*====================INTERVAL ALARM SLAVE 1, START and STOP/END =================*/
void StIntSlave1(){
  Alarm.enable(alarmIntervalSlave1);
}
void EndIntSlave1(){
  Alarm.disable(alarmIntervalSlave1);
}
/*====================INTERVAL SLAVE 1 COMMAND=================*/
void doRingIntS1(){
  lcd.setCursor(0,1);
  lcd.print("bip..bipp..bip..");
  delay(3000);
  lcd.clear();
}
...
...
...

any suggestion,mem?

thanks,

.nug.

First important bug:
timerRepeat and alarmRepeat ==> my fault
:disappointed_relieved: :disappointed_relieved: :disappointed_relieved:

Hi copet_pitik,

What is the purpose of the campareAlarmInt function?

If you want to save alarm values in eeprom so you they can be read when starting up then you don't need to compare.
Read the eeprom values in setup to set the alarms.
In loop you can check for serial messages and if you get a request to change an an alarm value you can write that to eeprom and update the alarm value.

As I suggested earlier, ignore the eeprom functionality for now.

First get the the alarms responding to serial commands. When that is fully tested and working it should be easy to add the eeprom code.

Michael

mem:
What is the purpose of the campareAlarmInt function?

my purpose for compareAlarmInt is to scan the alarm value in loop function.

mem:
If you want to save alarm values in eeprom so you they can be read when starting up then you don't need to compare.
Read the eeprom values in setup to set the alarms.
In loop you can check for serial messages and if you get a request to change an an alarm value you can write that to eeprom and update the alarm value.

mem,
when I send serial messages, then Arduino will reset automatically?

thanks for your guidance..

.nug.

my purpose for compareAlarmInt is to scan the alarm value in loop function.

That does not describe a purpose, it says how you want to achieve a purpose but not why.
What do you want to achieve when you scan the alarm value in loop?

when I send serial messages, then Arduino will reset automatically?

You said above that you wanted to send VB messages to set the alarm. If you need help with aspects of your application that are not specific to the TimeAlarms library then its best to start a new thread so that the content of this one is focused on specific help with TimeAlarms.

I suggest your create a new thread and post a link here so I and others interested in your application can help.

Michael

Thanks for your advice, mem.
.nug.

happy to help. If you have questions on the TimerAlarms library ask here. Post a link to a new thread if you have other questions on your application.

for those needing a full solution for scheduling daily / weekly start/stop of output, I started a new thread with my project.
http://arduino.cc/forum/index.php/topic,67127.0.html

It heavily use the timealarm lib albeit a bit modified.

regards

mem:
I suggest your create a new thread and post a link here so I and others interested in your application can help.

mem,here is my next question about my project, hope you can help

No errors were reported with the the beta code so the playground download has been updated.

Check my project at http://arduino.cc/forum/index.php/topic,67127.0.html

I javel thé sketch and à sérial interface also have a jquery web interface

mem:
Time and TimeAlarms are libraries for handling time and time based tasks on Arduino. The code can be found here.

This thread is for help on how to use these libraries and suggestions for future improvements.

A thread specifically for discussing issues relating to updates in the beta test version can be found here (that thread will be closed after the beta testing is completed.

Hello mem,
I needed to use DS1307 Real-Time Clock on a project and saw the Time library, but I thought it was too big for what I needed (just to know day, month, year, hour, minute and second from DS1307), so I decided to create a new, simple library to achieve this task and its code is at GitHub:

I think we should integrate it, maybe creating a "driver interface" (just some conventions) so we can create drivers for many RTC chips and use the same code. What do you think?

Other thing I think that should be changed in Time library is the namespace of functions. Maybe using Time.hour(), Time.day() etc. instead of directly hour(), day() etc.

alvarojusten:
Hello mem,
I needed to use DS1307 Real-Time Clock on a project and saw the Time library, but I thought it was too big for what I needed (just to know day, month, year, hour, minute and second from DS1307), so I decided to create a new, simple library to achieve this task and its code is at GitHub:

GitHub - turicas/DS1307: Arduino library for reading and setting date and time for the DS1307 Real-Time Clock IC

I think we should integrate it, maybe creating a "driver interface" (just some conventions) so we can create drivers for many RTC chips and use the same code. What do you think?

Other thing I think that should be changed in Time library is the namespace of functions. Maybe using Time.hour(), Time.day() etc. instead of directly hour(), day() etc.

Hi alvarojusten

Thank you for sharing your library. I have seen many DS1037 Arduino libraries and yours is one of the simplest (that’s a compliment). It does use less flash memory than the Time library but it actually seems to use more data (your sketch uses 540 bytes of RAM, the Time library code posted below uses 482 bytes of RAM).

/*
 *  A simple sketch to display time from a DS1307 RTC
 * 
 */

#include <Time.h>  
#include <Wire.h>  
#include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t

char dateTime[20];

void setup()  {
  Serial.begin(9600);     
}

void loop()
{
   setTime(RTC.get());
   sprintf(dateTime, "%4d-%02d-%02d %02d:%02d:%02d", year(),
            month(), day(), hour(),minute(), second()) ;
    Serial.print(dateTime);
    Serial.print(" - day of week: ");
    Serial.println(dayStr(weekday()));

   delay(1000);
}

Here are some advantage of using the Time library version:

  • You can easily calculate the difference between two times
  • The time format is based on a standard that almost every programming platform can handle
  • The same sketch code can be used with a selection of time sources such as RTC, NTP( internet time standards), Radio Clocks, GPS …
  • You can use the TimeAlarms library to add timers and time of day alarms.

Hi [again], Mem.

how to convert now() function (=>unixtime) to character array (char[11])?Is it possible to do?

Many C compilers have a function named ctime that produces a date string but I don't think its available with the arduino tools. I have always used multiple print statements to display the data, is there a reason you can't do that for your application?

ctime is not found in - avr-libc: AVR Libc -

maybe this link helps - time.c - sanos source -

Rob, did you try to use it in the Arduino IDE?
If so, a brief example would be useful.

No,

however I did use the RTC lib for the DS1307 - - GitHub - adafruit/RTClib: A fork of Jeelab's fantastic RTC Arduino library - It includes a DateTime Class that has all needed to implement a time2string(...) function
Note DateTime is year 2000 based, the essential code is in the DateTime constructor that can be transformend quite easily, see below:

void setup()
{
  Serial.begin(115200);
  Serial.println("UnixTime simulator :) ");
}

void loop()
{
  char buffer[20];
  time2string(millis(), buffer);
  Serial.print(millis());
  Serial.print(" ==>  ");
  Serial.println(buffer);
}

uint8_t daysInMonth [] = { 31,28,31,30,31,30,31,31,30,31,30,31 };

void time2string(unsigned long t, char *buf)
{
  uint8_t yOff, m, d, hh, mm, ss;

  ss = t % 60;
  t /= 60;
  mm = t % 60;
  t /= 60;
  hh = t % 24;
  uint16_t days = t / 24;
  uint8_t leap;
  for (yOff = 0; ; ++yOff) {
        leap = yOff % 4 == 0;
        if (days < 365 + leap)
            break;
        days -= 365 + leap;
  }
  for (m = 1; ; ++m) {
    uint8_t daysPerMonth = daysInMonth[ m - 1];
    if (leap && m == 2)
      ++daysPerMonth;
    if (days < daysPerMonth)
        break;
    days -= daysPerMonth;
  }
  d = days + 1;
  sprintf(buf,"%4d-%02d-%02d %02d:%02d:%02d", 1970+yOff, m,d, hh,mm,ss);
}

thanks Rob, Mem..

Many C compilers have a function named ctime that produces a date string but I don't think its available with the arduino tools. I have always used multiple print statements to display the data, is there a reason you can't do that for your application?

My Goal is sending unixtime via Wireless, then I have data in char array (char[11])
so when i try to send data unixtime which it is unsigned long, the compiler give error message:

incompatible types in assignment of 'time_t' to 'char [11]'

Rob, I use Mem's library (Time, TimeAlarm, DS1307RTC)..Any suggestion how to do it for DS1307RTC library?thanks anyway for the help.

thanks a lot.