PDA

View Full Version : More IF_THEN dumb questions.



ronjodu
- 18th February 2005, 23:26
Will this work?
I tried compiling with one ENDIF and it spit it out but with the 2nd ENDIF it compiled.

I don't want to continue down this road if this won't do what I need.

turnon1:
if r1sched = 0 and r1ctrl = 0 then
high relay1
pause 1500
low relay1
goto turnon3
else
if r1sched = 0 and r1ctrl = 1 then
high relay1
endif
endif
turnon2:
etc.....

Thanks for any replies..

mister_e
- 18th February 2005, 23:39
For this Kind of task i'll prefer using Select Case instead of bang my head everywhere.

I see you check only two bits variable..

if r1sched = 0 and r1ctrl = 1 then

if those are comming from some PORT pins, let's say PORTB.0 and PORTB.1 use the following



GetPORTB = PORTB & $3 ' mask other bits than B0 and B1

select case GetPORTB
Case 0 ' b0=0 b1=0
' Do your stuff here for that condition

Case 1 ' b0=1 b1=0
' Do your stuff here for that condition

Case 2 'b0=0 b1=1
' Do your stuff here for that condition

Case 3 ' b0=1 b1=1
' Do your stuff here for that condition

end select


that's easier like that.

Or you can also use BRANCH statement that will make your program jump (GOTO) to a specific label depending of the value of your var.



GetPORTB = PORTB & $3 ' mask other bits than B0 and B1
branch GetPORTB[condition0,condition1,condition2,condition3]



BUT in case you still want to use IF then bla blah



IF condition 1 then
' do your stuff here
endif

IF condition 2 then
' do your stuff here
endif

IF condition 3 then
' do your stuff here
endif

IF condition 3 then
' do your stuff here
endif

ronjodu
- 19th February 2005, 00:05
Sorry,I should have given more detail. What I'm doing is filling the variables thru a webpage, serially to the pic. They are bit var's.
There are other conditions also.
I really like the "Case" senario. I didn't know that was possible. (still very much a newbie)
Any help on setting that up with what I have would be apreciated. Don't give away too much, I don't mind working for it..

Here's a snippet with the var's.

mister_e
- 19th February 2005, 00:14
WHAT A LOAD OF VAR!!! Since i don't know where all those BIT var will comming from, i figure it will be more easy to use a different approach. let us know what goes in and out... for the serial output... not a problem but for the AMOUNT of BIT variable, sure we can suggest you a other way to do what you want. Example work with a WORD sized variable that can provide you a 65536 different case, BYTE 256 or else.

ronjodu
- 19th February 2005, 13:25
Sorry for the delay, here's what I'm doing. I'm using a 16f876 with 4Mhz and of course PBP. Webpage interface thru Siteplayer.
What I'm trying to do is this..

I have 8 pilot relays to control from 4 to 8 lighting contactors via a time schedule.
Some of the contactors are mechanically held and require a pulse on, pulse off. For these I'll assign two seperate pilot relays, one for on pulse and one for off pulse.
Other contactors are electrically held and only require one relay to be energized when the contactor is to be on and cleared when the contactor is to be off.

I have declared R1ctrl thru R8ctrl variables to let me know what type control the relay will be used for, if clear the relay will be pulsed, if set the relay will hold on. These variables are set and cleared thru a webpage user interface that already works well.

The relays will also follow a time schedule, daytime and nighttime.
Each relay has a schedule bit R1sched thru R8sched to let me know if the relay is on the day schedule or the night schedule, day is clear and night is set.These variables are also set and cleared thru a webpage user interface that already works well.

I've also declared 4 additional bit variables, dayontime and dayofftime and nightontime and nightofftime.If dayontime is set anything on the day schedule should turn on and if dayofftime is set anything on the day schedule should be turned off.
Same goes for the nightontime/nightofftime relays.
These bits are set and cleared thru a RTClock subroutine, again set up thru a webpage that already works well.
code:
',
if rtchr = onhour and rtcmin = onmin then 'setup dayontime
dayontime = 1
else
dayontime = 0
endif

if rtchr = offhour and rtcmin = offmin then 'setup dayofftime
dayofftime = 1
else
dayofftime = 0
endif

if rtchr = nonhour and rtcmin = nonmin then 'setup nightontime
nightontime = 1
else
nightontime = 0
endif

if rtchr = nonhour and rtcmin = nonmin then 'setup nightofftime
nightofftime = 1
else
nightofftime = 0
endif
' ,
I also declared status variables for each contactor which need to be set when the contactor is told to be on, and cleared when the contactor is told to be off. R1status, R2status...R3status... These variables will be sent to the webpage for user notification that the lights are told to be on or off. (Possible realtime feedback in the next version. ;)

Another bit variable, "override" I have declared, if set will not allow any clock control and if cleared allows clock control.

The entire system must be available for manual control at any time. This manual user interface comes via a webpage that I've already tested with a simple on/off routine.

I'm thinking I can come up with a subroutine for each relay to sort thru the necessary variables and give the desired output to the relay. My limited experience with PBP puts me in a box with A load of if-then's.

What I'm thinking is maybe someone with more knowledge of PBP can devise a different approach and get me out of my box. Something more compact maybe. I'm not looking for someone to write the code for me just Ideas on what direction I should try. I like the concept of the "CASE SELECT" but I don't see how it can be applied to this app.

I've included a knocked down version of the variables with only the ones that apply.

Thanks for any help.

mister_e
- 19th February 2005, 17:46
Since you have only 8 outputs, why not using a loop test and branch to different procedure...

something like this...


MyInput var Byte ' contactor input
State var bit[8] 'state of each input
TestLoop var byte

Start:
MyInput = PORTA
TestLoop = 0

DoContactorTest:
State[TestLoop] = Myinput.0[TestLoop]

If state[testloop] = 1 then finishorno
Branch TestLoop,[relay1,relay2,relay3,relay4,_
relay5,relay6,relay7,relay8]
FinishOrNo:
TestLoop = TestLoop + 1
If Testloop<=7 then DoContactortest
goto start

relay1:
' Do the according stuff

goto finishorno

relay2:
' Do the according stuff

goto finishorno

'
' same for the other

ronjodu
- 21st February 2005, 00:32
Thanks again Steve, I've been working on stuffing my bits into a byte so I can use your snippetts.
I'm still leaning toward the case select, probably cause I don't follow the other very well.

Question, in the case select, is the case # the actual binary value of the byte?
Here's how I stuffed my bits..

myinput.0 = override
myinput.1 = r1sched
myinput.2 = r1ctrl
myinput.3 = r1status
myinput.4 = dayontime
myinput.5 = dayofftime
myinput.6 = nightontime
myinput.7 = nightofftime
So if r1status was high and dayofftime was high my byte value would be 40..

select case myinput
Case 0 ' b0=0 b1=0 b2=0 b3=0 b4=0 b5=0 b6=0 b7=0
' Do your stuff here for that condition

Case 40 ' b0=0 b1=0 b2=0 b3=1 b4=0 b5=1 b6=0 b7=0
' Do your stuff here for that condition

end select

or am I messed up??

mister_e
- 21st February 2005, 00:36
That's it you understand!

ronjodu
- 21st February 2005, 00:46
That's awsome, be back in a day or so.. Lots to work on now.;)

ronjodu
- 21st February 2005, 20:34
After confusing myself a little I came up with this,



checkrelays:
select case myinput
case 1,3,16,66 'manual and auto pulse on day and night
high relay1 : pause pulsetime : low relay1
case 5,7,20,70 'manual and auto hold on day and night
high relay1
case 8,10,40,138 'manual and auto pulse off day and night
high relay2 : pause pulsetime : low relay2
case 12,14,44,142 'manual and auto hold off day and night
low relay1
end select
return

This seems to cover all my possible cases, however this is for one relay, I have 8 to test.


After filling MYINPUT the first time the only bits that need to be changed for the next relay are:
myinput.1, myinput.2, myinput.3, and then the relays to act on, IE relay2, relay3, etc....

Is there a better way to do this or just write a seperate case select for each relay?

Thanks for any input! :)

mister_e
- 22nd February 2005, 18:31
Are those other have the same condition ?!?

If so, some array can be usefull..


For Loop = 0 to 3
SelectCase Loop
Case 0
TempTestVar = MyInput
Case 1
TempTestVar = Myinput1
Case 2
TempTestVar = Myinput2
Case 3
TempTestVar = Myinput3
end select

Select case TempTestVar
' cases stuff BUT
' Relay1[Loop] = 1 or 0
' Relay2[Loop] = 1 or 0
end select
Next


Once it's done do a loop for refresh each relay section... that's it

ronjodu
- 22nd February 2005, 19:33
I've coded the "myinput" and Select Case by hand for all 8 relays and ran it on my relay board and other than one typo relay # it worked perfectly. I was able to select time schedules, on and off, and relay control and watch it all happen realtime, in manual and auto mode.. Really cool!!!
However, that portion of the program, all coded out individually, took 1.2K...
which I'm sure can be done with less.

I'm trying to follow your recent post about using an array. I'll surely have questions.

Thanks a bunch for the help so far, my next post I'll probably start a new thread about Arrays. Going to check the archives first.

Again thanks alot!!