switching a light on and off

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 2 and 3.

Moderators: Benj, Mods

cobra1
Posts: 175
Joined: Thu Feb 04, 2010 7:44 am
Has thanked: 3 times
Been thanked: 3 times
Contact:

switching a light on and off

Postby cobra1 » Mon Mar 05, 2012 11:27 am

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

User avatar
Benj
Matrix Staff
Posts: 14471
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4536 times
Been thanked: 4204 times
Contact:

Re: switching a light on and off

Postby Benj » Mon Mar 05, 2012 12:57 pm

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.

cobra1
Posts: 175
Joined: Thu Feb 04, 2010 7:44 am
Has thanked: 3 times
Been thanked: 3 times
Contact:

Re: switching a light on and off

Postby cobra1 » Mon Mar 05, 2012 2:05 pm

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.

User avatar
JonnyW
Posts: 1230
Joined: Fri Oct 29, 2010 9:13 am
Location: Matrix Multimedia Ltd
Has thanked: 64 times
Been thanked: 306 times
Contact:

Re: switching a light on and off

Postby JonnyW » Mon Mar 05, 2012 4:00 pm

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:

Code: Select all

  last_update = 0 // Initialised to 'not checked yet'
  update_event = false


Have something continuously checking your minutes and when this changes flag an 'event' (change a variable from 0 to 1):

Code: Select all

  if (minute_changed)
    next_update = get_current_time()
    update_event = true


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:

Code: Select all

  if (update_event)
    change_states_between_last_and_next()
    update_event = false
    last_update = next_update


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

cobra1
Posts: 175
Joined: Thu Feb 04, 2010 7:44 am
Has thanked: 3 times
Been thanked: 3 times
Contact:

Re: switching a light on and off

Postby cobra1 » Mon Mar 05, 2012 11:38 pm

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