Hi
im looking for some inspiration
i am trying to turn a light on and off at set times of the day.
I am currently doing this be having variables set up for my on and off times. there is then a few if statements to compare the clock time with the on and off time.
The if statements are done in such a way that the on or off transition happens at the on or off time is there any way to constantly monitor the time to switch
switching a light on and off
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: switching a light on and off
Hello,
You may have to do a scan through once you have setup the clock.
Eg if the time is set to 14:00 then I would maybe quickly scan through the time from 00:00 up to 14:00 for that day to see if there are any events that need processing.
If scanning from the start of the day may still miss events then maybe from the start of the previous day. Once you have done this you can assign your outputs etc correctly and then go back to actioning events as they occur.
You may have to do a scan through once you have setup the clock.
Eg if the time is set to 14:00 then I would maybe quickly scan through the time from 00:00 up to 14:00 for that day to see if there are any events that need processing.
If scanning from the start of the day may still miss events then maybe from the start of the previous day. Once you have done this you can assign your outputs etc correctly and then go back to actioning events as they occur.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Re: switching a light on and off
Thanks for the reply ben . using that method i can only get it to work from morning till night i.e 08:00 to 19:00.
I cant figure out how to get it to work for 10:00 to 08:00 aswell
The clock is a rtc.
I cant figure out how to get it to work for 10:00 to 08:00 aswell
The clock is a rtc.
- JonnyW
- Posts: 1230
- Joined: Fri Oct 29, 2010 9:13 am
- Location: Matrix Multimedia Ltd
- Has thanked: 63 times
- Been thanked: 290 times
- Contact:
Re: switching a light on and off
Hi. I think most of the electronic timers work on events. You only check states that lie between the last time you checked and the new time you check.
Remember the last time you did a check in a variable:
Have something continuously checking your minutes and when this changes flag an 'event' (change a variable from 0 to 1):
In a loop, check the 'event' flag. If this is set, clear it, check events in your 'window' and set the current time as the new last time:
This means that the lights will change so long as the time to change lies on a minute boundary, and you do not have to continuously check for all events. If your chip powercycles you would have to set the 'current' state manually, but even this can be fixed by storing the on/off state in EEPROM when it changes (only when it changes, to reduce wear on the EEPROM).
I probably haven't been clear, but I hope this is a help,
Jonny
Remember the last time you did a check in a variable:
Code: Select all
last_update = 0 // Initialised to 'not checked yet'
update_event = false
Code: Select all
if (minute_changed)
next_update = get_current_time()
update_event = true
Code: Select all
if (update_event)
change_states_between_last_and_next()
update_event = false
last_update = next_update
I probably haven't been clear, but I hope this is a help,
Jonny
Re: switching a light on and off
Ok, using the events method i seem to have cracked it.
I didnt do it as you described but i have come up with a pretty effective way of doing it.
Thanks for steering me in the right direction guys
I didnt do it as you described but i have come up with a pretty effective way of doing it.
Thanks for steering me in the right direction guys