PDA

View Full Version : Logic



Scampy
- 16th November 2013, 16:38
Hi Guys,

Needs a little more advise to help sort out a small logic issue that I can't get my head round

Following on from my previous threads, I use a word variable counter1 to count the minutes from midnight, and use other variables that are either < = > this counter to turn lights on etc. As the main outputs will come on and off in the same day this counter method works fine, but where the logic falls over is if the on times are pre-midnight, and the off times are post midnight as the counter resets back to zero at midnight. I've therefore tried adding a day counter so that at midnight the variable changes from 0 to 1, and then is reset at mid-day.

This output would normally be on say from any time between 8pm and 11pm one day and then go off any time the next day, for example on at 8pm then off at 12.00 noon next day, so the on time would equal 1200 and off time would be 720. This is what I've tried, but still fails



if counter1 => sumplighton then
high sump_light
Endif

If sumplightoff < sumplighton and daycount=0 then
LOW sump_light
endif

If sumplightoff < sumplighton and daycount=1 then
high sump_light
endif


IF counter1 => sumplightoff and daycount=0 then
LOw sump_light
Endif


Any help would be appreciated

HenrikOlsson
- 16th November 2013, 17:03
This doesn't really answer your question but why don't you have one ON-time and one OFF-time?

For example

If Output1 = OFF THEN
If Counter1 = Output1OnTime Then
Output1 = ON
ENDIF

ELSE ' Output is ON

If Counter1 = Output1offTime Then
Output1 = OFF
ENDIF

ENDIF
Now, there are 1440 minutes in 24hours.
If Output1OnTime = 1438 and OutputOffTime = 2 the output will turn on at 23:58 and off at 00:02.
If OutputOnTime = 2 and OutputOffTime = 1438 the output will turn on at 00:02 and off 23:58.

If you have multiple outputs then you can preferably use arrays to store states, on and off times and simply index thru them each time your minutes counter "tick" to see if any needs changing state.

/Henrik.

Scampy
- 16th November 2013, 18:24
Must be the cold in my head that's causing me to make things too complicated...



If daycount = 0 THEN
If Counter1 = sumplighton Then
high sump_light
daycount = 1
ENDIF
ENDIF

IF daycount = 1 THEN
If Counter1 = sumplightoff Then
low sump_light
daycount = 0
ENDIF
ENDIF


based on your example I came up with the above which works just fine !

Thanks Henrik

richard
- 16th November 2013, 21:12
sumplight off time = sumplight on time + sumplight duration
if sumplight off time > 1440 then sumplight off time = sumplight off time - 1440 and set next day flag

...
then we have a flag (slf) that is 1 when sumplight is on else 0 and a next day flag (ndf) that is set to 1 when offtime overflows else 0
the ndf flag is cleared at midnight in you time routine , the chklight sub is called once every minute

chklight: check light subroutine
if slf=0 then
if count >= on time then
slf=1
offtime = count+duration
ndf=0
if offtime >1440 then
offtime=offtime -1440
ndf=1
endif
turn on light
endif

else
if ndf=1 then return
if count >= offtime then
slf=0
turn off light
endif
endif
return




return

Scampy
- 17th November 2013, 13:12
Richard, thanks for the input.

I've made one modification to include a > in the first line of the code I posted above and it's been running fine. But thanks as always for your contribution

Malcolm