Help on a time-based code

Hi,
I’m trying to write some code for a simple program to operate the opening and closing of an overhead door (via push button & relay) that would only work between the hours of 8am and 5pm.
I’m thinking the best way to go about it is to make a function for the button operations and then have that function only be available/activated between the hours of 8-5, thru another function for the “time”

My questions are; is that a proper way of doing this and also I cant seem to figure out how the “time” function on the Photon works.
Thanks.

These 2 posts by @ScruffR might be a good starting point.
I'm sure there are many others.
Give it a try and then folks can help you figure out specific problems in your Code.

1 Like

Thank you! I’ll check them out.

What do you mean by “a proper way”?

A simple logic for the loop() could be something like this:

if (button_pressed AND time >= 8am AND time <=5pm) energise relay for short period

To support that there needs to be:

  1. Debounce of the button - simple software debounce algorithm to provide a clean boolean button_pressed value.
  2. time - to have the Photon provide this it does need to have been Cloud connected to sync the Real-Time Clock and you would also need to ensure that the correct local time is set for your locale (DST and TimeZone). The program should also re-sync the time once per day (because the RTC drifts) and should check that the time is sync’d before using it with Time.isValid();
  3. A way to energise the relay for a short period. In a very simple logic you can use something like relay on, delay(), relay off. A better approach is to start a timer when relay turned on and then have the timer ended handler function turn it off. This can be achieved using a software timer or checking millis() - millis when turned on.

Thanks Armor…that’s what I meant by a proper way, a simple logic like the one that you recommended.
I’m relatively new to programming but I think I can work with that.

Thanks again!