Conditional sequence depending on time of day


Kris

Recommended Posts

Hello,

I'm trying to write a sequence where the IF condition depends on time. Something like: if systime > 8:00 then condition1 and if systime > 22:00 then condition2. So between 8.00 and 22:00 there should be condition1 and between 22:00 and 8:00 (the next day) there should be condition2.

I' can't seem to find the right expression for this.

Thanks in advance.

Kris

Link to comment
Share on other sites

OK, there are a lot of variations here, so you'll first really need to determine what you want to happen when. For example, if you start the program at 9:00 do you want the action that happens at 8 to occur immediately or wait until tomorrow?

That said, here is the general principals: DAQFactory supports the generation of time constants using what we call hms notation. We use the notation because its universal around the world, as opposed to 02/06/71 which could be either feb 6th, or jun 2nd depending on where you are. Most of this is described in the help under expressions, but here is a quick summary. To demonstrate, lets assume you want feb 6th, 2007 at 18 hours, 32 minutes, 12.34 seconds. In HMS notation it would be (note, no spaces):

7y2m6d18h32m12.34

This gets converted directly into the number 1170786732.340 which is DAQFactory time for the same thing and thus the same format as systime() and everything else time in DAQFactory.

Now, to make things easier, you can drop certain parts. For example, if you don't specify the year, this year is assumed. But more useful, if you don't specify the date part at all, today is assumed. So:

8h

is 8am this morning. What "this morning" is depends on when the statement is executed, so if you do:

if (systime() > 8h)

this statement will return true if, at the time of executing the if statement, the current time of day is later than 8am.

Easy enough. The trick comes when you want to wait until 8am tomorrow instead. You would then simply need to add a day to the time, remembering that it gets translated to DAQFactory time which is seconds since 1970, so:

if (systime() > 8h + 86400)

Truthfully, this is a useless statement, since it will never be later than 8am tomorrow, because when tomorrow comes, its no longer tomorrow, etc.. This sort of statement is really only useful when using the waituntil() function:

waituntil(8h + 86400)

In this case, 8am tomorrow is calculated once, today, and then the waituntil executes waiting until that time. It does not reevaluate the expression, so tomorrow doesn't become today like it does in the if() example and the wait executes properly.

So, you have two choices for your needs:

1) you can create two sequences, one for each condition, both looking something like this:

while (1)
   waituntil(8h + 86400)
   .. do whatever you wanted to do at 8am
endwhile

2) you can create a single sequence, and be really smart with your ifs. Here you have to decide what happens if the sequence starts after 8am (code written in 5.73):

private done1 = false
while (1)
   if ((systime() > 8h) && (!done1))
	  .. do first condition
	  done1 = true
   endif
   if ((systime() > 22h) && (done1))
	  .. do second condition
   endif
   delay(1)
endif

3) oops, I guess I should have read you question better. My mouse isn't working so I'm driving my computer solely by keyboard :). It appears you want to do first item if the time is between 08 and 22, and second item if its between 22 and 08. Well then you don't have to worry so much about the day stuff, just do:

while (1)
   if ((systime() > 8h) && (systime() <= 22h))
	  .. do first item
   else
	  .. do second item
   endif
   delay(1) // or whatever interval you want the loop to run
endwhile

Link to comment
Share on other sites

Ok, i created this as a result of your suggestion and it works great!

while (1)
  if ((systime() > DayTime) && (systime() <= SunDown) && counter<1)   
	  Beginseq (DimmerUp)	  
	   counter++
   else 
	 if ((systime() > SunDown) && counter>0)   
		 Beginseq (DimmerDown)
		  counter--
	 endif
   endif	
   delay(1) 
endwhile

DayTime and Sundown are global variables and the counter variable takes care of the dimmer not going into a loop. Dimmerup and Dimmerdown are sequences that do what their name says.

It is used to controll dimmable TL lights in a terrarium. The tropical frogs will be greatfull!

Kris

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.