Setting Variable with "Date & Time Edit" Box


Recommended Posts

A warning ahead: I'm afraid, that you will hear from me often in the near future, since I truly need assistance. 'Sorry in case I'm wasting your time!


here's my issue at the moment: I am trying to create a quite simple time switch. I have setup 2 "Date & Time Edit" boxes. One is meant for setting the time to start (global ontime) and one is meant to tell how long the load is switched on (global duration). I understand that both are strings and therfore need to be converted in order to be used for further needs. The ontime is not so much of a problem because I figured out how to use the formattime function. But the duration variable does my head in! If I understand it right, the formattime function applied to this does return a numerical value, but it is based on the seconds since 1970 (am I correct?) so obviously it is not what I need. I need a way to set a value in a HH:mm:ss format and cnvert this to an absolute value -> between 0 and 86400sec. The edit box seem to be perfect for this as it is automaticaly limited to 24:59:59 (obviously it would be much easier to use an edit box into which a numerical value is entered, but if a the future user enters a value above 24h ( / 86400 secs) it would hang again. So in that case I would need a way to limit the value entered to a max of 24:59:59... Either way I don't know how to do it :-(

Please Help!

The code I want to use should look somewhat like this:

global lamp_priority
global duration
global ontime

while(1) // as long the start/stop botton lets the sequence run
   // the following loop should cycle
   if (systime() < ontime()) // assures load doesn't switch 
	   //until the desired time is reached
	  waituntil (ontime())
	  while (systime() > ontime()) 
		 if (lamp_priority < 2) // this is what would be called a marker in some plcs.
							  // the purpose is to switch loads in reverse order at every cycle 
			lamp_1 = 1
			lamp_priority = 3
			// assures the other load switches first at next cyle(tomorrow)
			delay (5m) // switches load 5 mins after the first
			lamp_1 = 1
		 delay (duration()) 
		 // i want to have the load on for as many hours as are set in the edit box
		 lamp_1 = 0
   waituntil (ontime() + 86400) // assures repetition at the same time everyday
	  delay(1) //just to make sure it doesn't hang

I have a second identical sequence running paralell to this one - except the values for the marker are reversed. This way the load starting first reverses every day.

So much for the theory.

I'm pretty sure you can see straight away why it doesn't work; please help me see too!

Thanks in advance!

Link to comment
Share on other sites

First, ontime is a variable so you don't want "ontime()" in your sequences. That would indicate that ontime is a function. Same with duration.

Second, I'm not sure delay(5m) works. Use delay(300) instead.

Next, I'm not sure you can put a space after a function and before the (. So delay(300) not delay (300)

Finally, time is always a number (not a string as you indicated) and is seconds since 1970. The date/time edit always returns this value even if used for duration. If you use it for duration, the max duration is 24 hours. To convert the time returned by the edit control to actually duration in seconds, just do: duration % 86400 which is the remainder of duration / 86400, which would be the number of seconds in the day, ignoring the date.

When you use formattime() it basically uses just the seconds in the day part, so you can use it to properly display the duration.

Link to comment
Share on other sites


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