Help with setting Photon Schedules

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.

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);
  }
}

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

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

1 Like

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.

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.

In setup() there is a digitalWrite:

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

I’m working on this too.

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 :slight_smile:

What times are these actually supposed to be?

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

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

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.

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

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

1 Like

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

it is 16 times 3600
41 times 60
etc.

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.

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! :slight_smile:

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.

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).

Here is the full code, correctly formatted:

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
  }
} 
1 Like

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

#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);
    }
}
1 Like

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.

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

2 Likes