Enable an output in a time range

Dear forum ,

I would like your help for a function I am creating. I want to set an output to true between a period of time and reset it in between another period of time.

the time values I am going to send them to the particle through the web. What is the easiest way to do this?

Is there any library for setting an Alarm to run between two time periods? For example to set output true from 5:00-6:00 AM.

Regards
Johny

You can use the existing Time functions to check if the current time falls within the range. No fancy libraries needed.

1 Like

A library isn’t necessary, but I created the DailyTimerSpark library just for the use-case you have. I have not ported to to Libs 2.0 but you can just copy the files into your project file.

your code would look like this:

//Program Start
#include "DailyTimerSpark.h"

struct deviceTime{
  int hour;
  int minute;
};

deviceTime startTime  = {17, 0};
deviceTime endTime = {18, 0};

DailyTimer timer1(startTime.hour, startTime.minute, endTime.hour, endTime.minute, EVERY_DAY);
bool timerState;

void setup()
{
  Particle.variable("Alarm", timerState);
  timer1.begin();
}

void loop()
{
  timerState = timer1.isActive();
}

FYI, that library will likely get re-factored when it goes up to the new library version, I’ve done some improvements to the one I am using…

2 Likes

You are awesome ! this is very helpful. I will check it out

1 Like