# Help with setting Photon Schedules

**URL:** <https://community.particle.io/t/help-with-setting-photon-schedules/41267>\
**Category:** Firmware\
**Created:** [April 19, 2018, 9:01pm UTC](https://community.particle.io/t/help-with-setting-photon-schedules/41267 "2018-04-19T21:01:48Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Exit2Studios](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/exit2studios/32/17135_2.png) [@Exit2Studios](https://community.particle.io/u/Exit2Studios)\
**Post date:** [April 19, 2018, 9:01pm UTC](https://community.particle.io/t/help-with-setting-photon-schedules/41267/1 "2018-04-19T21:01:48Z")

</div>

I am new to Photon and new to coding. I am working on a DIY Pool Controller, and have made quite a bit of progress.

I am looking to set up some schedules that will run certain pumps, lights, valves, etc at various times and created the following code as a test.

The LED comes on via the relay it’s attached to at the set time, however, it does not turn off. I’m hoping someone can easily spot where I’ve gone wrong.

Also, is there a cleaner, easier way to accomplish the same thing? This code seems like it might get pretty hairy as I build out more complex schedules.

```cpp
const int timeActionOn = 15*3600 + 59*60 + 00; // hh+mm+ss
const int timeActionOff = 17*3600 + 28*60 + 00;
int previousDay = 0;
#define led1 D7

void setup() {
  Time.zone(-5);
pinMode(led1, OUTPUT); 
digitalWrite(led1, HIGH);
}
void loop() {
  if (previousDay != Time.day() && Time.local() % 86400 >= timeActionOn) {
    previousDay = Time.day();
    digitalWrite(led1, LOW);
  }
  else if (previousDay != Time.day() && Time.local() % 86400 >= timeActionOff) {
      previousDay = Time.day();
      digitalWrite(led1, HIGH);
  }
}

```

---

<div class="post-metadata">

**Author:** ![bko](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bko/32/20704_2.png) [@bko](https://community.particle.io/u/bko)\
**Post date:** [April 19, 2018, 9:43pm UTC](https://community.particle.io/t/help-with-setting-photon-schedules/41267/2 "2018-04-19T21:43:53Z")

</div>

You should have a look at the port of the Time Alarms library that I did. It might be useful for you.

---

<div class="post-metadata">

**Author:** ![Pescatore](https://avatars.discourse-cdn.com/v4/letter/p/a9a28c/32.png) [@Pescatore](https://community.particle.io/u/Pescatore)\
**Post date:** [April 19, 2018, 9:47pm UTC](https://community.particle.io/t/help-with-setting-photon-schedules/41267/3 "2018-04-19T21:47:38Z")

</div>

Search the forum for keywords like “alarm”, “timer”, etc.  
This library may be adaptable to your application:

> **[BulldogLowell/DailyTimerSpark](https://github.com/BulldogLowell/DailyTimerSpark)**
>
> DailyTimerSpark - Particle Library for setting daily timers

---

<div class="post-metadata">

**Author:** ![Exit2Studios](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/exit2studios/32/17135_2.png) [@Exit2Studios](https://community.particle.io/u/Exit2Studios)\
**Post date:** [April 19, 2018, 9:48pm UTC](https://community.particle.io/t/help-with-setting-photon-schedules/41267/4 "2018-04-19T21:48:49Z")

</div>

I actually looked at that library, but being a noob and by no means a coder, I wasn’t sure where to start. If you have some sample code or examples to point me towards, that would be great.

I basically just need to turn on/off various relays at certain times each day and add some logic to do some things based on weather, etc.

---

<div class="post-metadata">

**Author:** ![Ric](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ric/32/514_2.png) [@Ric](https://community.particle.io/u/Ric)\
**Post date:** [April 19, 2018, 9:50pm UTC](https://community.particle.io/t/help-with-setting-photon-schedules/41267/5 "2018-04-19T21:50:11Z")

</div>

> [@Exit2Studios](#):
>
> const int timeActionOn = 153600 + 5960 + 00;  
> const int timeActionOff = 173600 + 2860 + 00;

What's with these large numbers? I don't understand how the LED ever turned on in the first place, since **Time.local() % 86400** will never be greater than either of those numbers.

---

<div class="post-metadata">

**Author:** ![nrobinson2000](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/nrobinson2000/32/15624_2.png) [@nrobinson2000](https://community.particle.io/u/nrobinson2000)\
**Post date:** [April 19, 2018, 9:51pm UTC](https://community.particle.io/t/help-with-setting-photon-schedules/41267/6 "2018-04-19T21:51:22Z")

</div>

> [@Ric](#):
>
> I don’t understand how the LED ever turned on in the first place, since Time.local() % 86400 will never be greater than either of those numbers.

In `setup()` there is a `digitalWrite`:

```auto
void setup() {
Time.zone(-5);
pinMode(led1, OUTPUT);
digitalWrite(led1, HIGH);
}

```

I'm working on this too.

---

<div class="post-metadata">

**Author:** ![Exit2Studios](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/exit2studios/32/17135_2.png) [@Exit2Studios](https://community.particle.io/u/Exit2Studios)\
**Post date:** [April 19, 2018, 9:52pm UTC](https://community.particle.io/t/help-with-setting-photon-schedules/41267/7 "2018-04-19T21:52:22Z")

</div>

That looks interesting, thanks. I’m not quite sure how to incorporate libraries (.h or .cpp), so I guess figuring that out would be a good start 🙂

---

<div class="post-metadata">

**Author:** ![nrobinson2000](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/nrobinson2000/32/15624_2.png) [@nrobinson2000](https://community.particle.io/u/nrobinson2000)\
**Post date:** [April 19, 2018, 9:53pm UTC](https://community.particle.io/t/help-with-setting-photon-schedules/41267/8 "2018-04-19T21:53:37Z")

</div>

> [@Exit2Studios](#):
>
> const int timeActionOn = 153600 + 5960 + 00; // hh+mm+ss
> 
> const int timeActionOff = 173600 + 2860 + 00;

What times are these actually supposed to be?

---

<div class="post-metadata">

**Author:** ![Exit2Studios](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/exit2studios/32/17135_2.png) [@Exit2Studios](https://community.particle.io/u/Exit2Studios)\
**Post date:** [April 19, 2018, 9:53pm UTC](https://community.particle.io/t/help-with-setting-photon-schedules/41267/9 "2018-04-19T21:53:40Z")

</div>

The code didn’t copy correctly. It’s actually:

```cpp
const int timeActionOn = 16*3600 + 41*60 + 00; // hh+mm+ss
const int timeActionOff = 16*3600 + 42*60 + 00;

```

---

<div class="post-metadata">

**Author:** ![Exit2Studios](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/exit2studios/32/17135_2.png) [@Exit2Studios](https://community.particle.io/u/Exit2Studios)\
**Post date:** [April 19, 2018, 9:54pm UTC](https://community.particle.io/t/help-with-setting-photon-schedules/41267/10 "2018-04-19T21:54:57Z")

</div>

Actually, since I’m using a separate relay, the HIGH and LOW are switched, so the digitalWrite in setup() is actually to turn OFF the relay.

It DOES turn on at the correct time per the code, but doesn’t turn off.

---

<div class="post-metadata">

**Author:** ![nrobinson2000](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/nrobinson2000/32/15624_2.png) [@nrobinson2000](https://community.particle.io/u/nrobinson2000)\
**Post date:** [April 19, 2018, 9:55pm UTC](https://community.particle.io/t/help-with-setting-photon-schedules/41267/11 "2018-04-19T21:55:57Z")

</div>

Okay, but what times (hours, minutes, seconds) are you actually trying to implement?

---

<div class="post-metadata">

**Author:** ![Ric](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ric/32/514_2.png) [@Ric](https://community.particle.io/u/Ric)\
**Post date:** [April 19, 2018, 9:58pm UTC](https://community.particle.io/t/help-with-setting-photon-schedules/41267/12 "2018-04-19T21:58:51Z")

</div>

> [@Exit2Studios](#):
>
> const int timeActionOn = 163600 + 4160 + 00; // hh+mm+ss
> 
> const int timeActionOff = 163600 + 4260 + 00;

Those numbers still won't work. Doing **Time.local() % 86400** will always be less than 86400.

---

<div class="post-metadata">

**Author:** ![Exit2Studios](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/exit2studios/32/17135_2.png) [@Exit2Studios](https://community.particle.io/u/Exit2Studios)\
**Post date:** [April 19, 2018, 10:01pm UTC](https://community.particle.io/t/help-with-setting-photon-schedules/41267/13 "2018-04-19T22:01:44Z")

</div>

I’m not sure why the asterisk isn’t coming through:

it is 16 times 3600  
41 times 60  
etc.

---

<div class="post-metadata">

**Author:** ![nrobinson2000](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/nrobinson2000/32/15624_2.png) [@nrobinson2000](https://community.particle.io/u/nrobinson2000)\
**Post date:** [April 19, 2018, 10:03pm UTC](https://community.particle.io/t/help-with-setting-photon-schedules/41267/14 "2018-04-19T22:03:13Z")

</div>

Ohh, the formatting is messing with you.

Try this:

`````  
`your code`  
`````

But what times are you trying to do?

EDIT: It will be clear once your code is formatted right.

---

<div class="post-metadata">

**Author:** ![Exit2Studios](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/exit2studios/32/17135_2.png) [@Exit2Studios](https://community.particle.io/u/Exit2Studios)\
**Post date:** [April 19, 2018, 10:06pm UTC](https://community.particle.io/t/help-with-setting-photon-schedules/41267/15 "2018-04-19T22:06:53Z")

</div>

```auto
const int timeActionOn = 16*3600 + 41*60 + 00; // hh+mm+ss
const int timeActionOff = 16*3600 + 42*60 + 00;

```

The actual times are fairly irrelevant, as I’m just testing the code. I’m changing it to be a few minutes after I flash the firmware just to make sure it’s working.

In the above case it was 4:41pm ON and 4:42pm OFF (not sure how to accommodate for Daylight savings, so I switched -6 to -5)

The relay is turning ON, but not OFF at the prescribed time. Sorry about the formatting! 🙂

If there is a better (i.e. easier) way to format the time, please let me know. I’m assuming that is what the time libraries accomplish.

---

<div class="post-metadata">

**Author:** ![Ric](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ric/32/514_2.png) [@Ric](https://community.particle.io/u/Ric)\
**Post date:** [April 19, 2018, 10:09pm UTC](https://community.particle.io/t/help-with-setting-photon-schedules/41267/16 "2018-04-19T22:09:35Z")

</div>

In your if clause you set **previousDay = Time.day()**, but the else-if won’t execute unless **previousDay != Time.day()**. That’s why it never turns off (I assume the LED goes on when you write LOW).

---

<div class="post-metadata">

**Author:** ![Exit2Studios](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/exit2studios/32/17135_2.png) [@Exit2Studios](https://community.particle.io/u/Exit2Studios)\
**Post date:** [April 19, 2018, 10:12pm UTC](https://community.particle.io/t/help-with-setting-photon-schedules/41267/17 "2018-04-19T22:12:22Z")

</div>

Here is the full code, correctly formatted:

```auto
const int timeActionOn = 16*3600 + 41*60 + 00; // hh+mm+ss
const int timeActionOff = 16*3600 + 42*60 + 00;
int previousDay = 0;
#define led1 D7

void setup() {
  Time.zone(-5);
pinMode(led1, OUTPUT); 
digitalWrite(led1, HIGH); //using a relay, so HIGH is actually OFF
}
void loop() {
  if (previousDay != Time.day() && Time.local() % 86400 >= timeActionOn) {
    previousDay = Time.day();
    digitalWrite(led1, LOW); //using a relay so LOW turns LED on
  }
  else if (previousDay != Time.day() && Time.local() % 86400 >= timeActionOff) {
      previousDay = Time.day();
      digitalWrite(led1, HIGH); //using a relay so HIGH turns LED off
  }
} 

```

---

<div class="post-metadata">

**Author:** ![nrobinson2000](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/nrobinson2000/32/15624_2.png) [@nrobinson2000](https://community.particle.io/u/nrobinson2000)\
**Post date:** [April 19, 2018, 10:14pm UTC](https://community.particle.io/t/help-with-setting-photon-schedules/41267/18 "2018-04-19T22:14:57Z")

</div>

See if this works. I don’t think the previous day stuff is even necessary.

```cpp
#define led1 D7

const int timeActionOn = 16*3600 + 41*60 + 00; // hh+mm+ss
const int timeActionOff = 17*3600 + 32*60 + 00;

// timeActionOff > timeActionOn

void setup()
{
    Time.zone(-5);
    pinMode(led1, OUTPUT);
    digitalWrite(led1, HIGH);
}

void loop() 
{
    if (Time.local() % 86400 >= timeActionOff) {
        digitalWrite(led1, HIGH);
    }
    else if (Time.local() % 86400 >= timeActionOn) {
        digitalWrite(led1, LOW);
    }
}

```

---

<div class="post-metadata">

**Author:** ![Ric](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ric/32/514_2.png) [@Ric](https://community.particle.io/u/Ric)\
**Post date:** [April 19, 2018, 10:17pm UTC](https://community.particle.io/t/help-with-setting-photon-schedules/41267/19 "2018-04-19T22:17:39Z")

</div>

You are correct that it will get hairier as you add more things to turn on and off. I would go with @bko 's suggestion, and use the Time Alarms library. There is an example .ino file with the library that shows you how to use it. It is very simple.

---

<div class="post-metadata">

**Author:** ![bko](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bko/32/20704_2.png) [@bko](https://community.particle.io/u/bko)\
**Post date:** [April 19, 2018, 10:19pm UTC](https://community.particle.io/t/help-with-setting-photon-schedules/41267/20 "2018-04-19T22:19:57Z")

</div>

Just for the record, I went back and reformatted most of your code postings in this thread @Exit2Studios.

[Next page](https://community.particle.io/t/help-with-setting-photon-schedules/41267.md?page=2)
