Repeat problem...
Hello and thank for any help. I have the following code. The problem is that for some reason went current its = to target i want outa 3 and 4 to go off wait 60 seconds and next outa 5 need to go off. but instead the program wait about 6 sec and put off all at the same time...
Thanks
if ac == 2
if current > target
repeat 1
outa[5] := %1 ' fan on
outa[4] := %0
waitcnt (clkfreq * 20 + cnt)
outa[3] := %1 ' a/c on
elseif current == target
repeat 1
outa[3] := %0
outa[4] := %0
waitcnt (clkfreq * 60 + cnt)
outa[5] := %0
elseif current < target
outa[5] := %0
outa[3] := %0
outa[4] := %0
Thanks
Comments
From the Prop manual, page 219 version 1.1:
IMPORTANT: Since WAITCNT pauses the cog until the System Counter matches the given value, care must be taken to ensure that the given value was not already surpassed by the System Counter. If the System Counter already passed the given value before the wait hardware activated then the cog will appear to have halted permanently when, in fact, it is waiting for the counter to exceed 32 bits and wrap around to the given value. Even at 80 MHz, it takes over 53 seconds for the 32-bit System Counter to wrap around!
In a sense, I think you're encountering something like the opposite of what that warning pertains to. You're hitting that system clock value before the 53 seconds wrap around has happened.
You're using too large a wait period. You could break it into two 30 second waits or you could use one of the clock objects to keep track of time. (I personally think two waits would be easier in this case.)
repeat 1
;o)
If you have more than one time where you need to wait > 53 seconds you could create a function which waits for seconds:
PUB waits( seconds ) : counter counter := cnt repeat seconds waitcnt( counter+=clkfreq )
PUB controles dira[3..5] := %111 repeat if fan == 1 'fam on outa[5] := %1 else 'fam auto of outa[5] := %0 if ac == 2 if current > target repeat 1 outa[5] := %1 ' fan on wait 10 sec outa[4] := %0 waitcnt (clkfreq + cnt) outa[3] := %1 ' a/c on elseif current == target repeat 1 outa[3] := %0 ' check heat off, a/c compressor go off outa[4] := %0 waitcnt (clkfreq + cnt) ' wait 10 sec (HERE IS THE PROBLEM) outa[5] := %0 'fan go off elseif current < target outa[5] := %0 outa[3] := %0 outa[4] := %0 if ac == 3 ' heat on outa[4] := %1 outa[5] := %1 outa[3] := %0 if ac == 4 ' heat emerg outa[4] := %1 outa[5] := %1 outa[3] := %0 if ac == 1 ' heat on outa[4] := %0 outa[5] := %0 outa[3] := %0
Can current jump past the target value? I'm just noticing that 3 through 5 all get turned off at the same time if "current < target".
Could the problem be in a different area of code? (i.e. where the value of current is set)
Are the values of current and target in integer form? Sometimes people forget that the > and < operators only work for integers and they try to do comparisons with real numbers, etc.
[video=youtube_share;toYoGafc-lA]
outa[5] := 1 ' fan is on
...when
... is so much more obvious -- and doesn't require comments. CONstants are your friends!
Here's a stab at reformatting your method to make it more readable -- without constant names as I don't know what all pins do.
PUB controles dira[3..5] := %111 repeat if (fan) outa[5] := 1 else outa[5] := 0 case ac 2: if (current > target) outa[5] := 1 outa[4] := 0 waitcnt(cnt + (10 * clkfreq)) outa[3] := 1 elseif (current == target) outa[5] := 0 outa[4] := 0 waitcnt(cnt + (10 * clkfreq)) outa[3] := 0 else outa[5]:= %0 outa[3]:= %0 outa[4]:= %0 3: ' heat on outa[4] := %1 outa[5] := %1 outa[3] := %0 4: ' heat emerg outa[4] := %1 outa[5] := %1 outa[3] := %0 1: ' heat on outa[4] := %0 outa[5] := %0 outa[3] := %0