Help with coding a contact closure(s).
Paul Roy
Posts: 5
Thanks for taking a look.
I'm new to using basic stamps so my knowledge of the pbasic is way before the curve. Here is what I'm trying to do.
I am monitoring for certiane contact closures on one of my systems. At design time i was told that the contact closure was momentary, turns out it's not. So it's a held closure. Here is my problem I·have to·use a BS2 stamp, the BS2 stamp does not support polling so I have to go about it another way. so·I was thinking of using variables as code "switches" with if then statements. but I couldn't get the variables to work. Below is the code I am using before adding the Variables.
[/code][/size]
I am monitoring for certiane contact closures on one of my systems. At design time i was told that the contact closure was momentary, turns out it's not. So it's a held closure. Here is my problem I·have to·use a BS2 stamp, the BS2 stamp does not support polling so I have to go about it another way. so·I was thinking of using variables as code "switches" with if then statements. but I couldn't get the variables to work. Below is the code I am using before adding the Variables.
[size=2][code] '{$STAMP BS2} '{$PBASIC 2.0} Start: LOW 0 HIGH 1 HIGH 3 HIGH 5 HIGH 7 HIGH 9 HIGH 11 HIGH 13 DO IF IN1 = 0 THEN cr1 'cr1 in controll IF IN3 = 0 THEN cr2 'cr2 in controll IF IN5 = 0 THEN cr3 'Booth in controll IF IN7 = 0 THEN cr5 'Cr1 booth pot on IF IN9 = 0 THEN cr6 'Cr1 Booth pot off IF IN11 = 0 THEN cr7 'cr2 booth pot on IF IN13 = 0 THEN cr8 'Cr2 booth pot off PAUSE 50 GOTO Start cr1: SEROUT 16, 16468, [noparse][[/noparse]"1"] '16468 = 9600 Baud HIGH 0 PAUSE 200 GOTO Start cr2: SEROUT 16, 16468, [noparse][[/noparse]"2"] HIGH 0 PAUSE 200 GOTO Start cr3: SEROUT 16, 16468, [noparse][[/noparse]"3"] HIGH 0 PAUSE 200 GOTO Start cr4: SEROUT 16, 16468, [noparse][[/noparse]"4"] HIGH 0 PAUSE 200 GOTO Start cr5: SEROUT 16, 16468, [noparse][[/noparse]"5"] HIGH 0 PAUSE 200 GOTO Start cr6: SEROUT 16, 16468, [noparse][[/noparse]"1"] HIGH 0 PAUSE 200 GOTO Start cr7: SEROUT 16, 16468, [noparse][[/noparse]"6"] HIGH 0 PAUSE 200 GOTO Start cr8: SEROUT 16, 16468, [noparse][[/noparse]"2"] HIGH 0 PAUSE 200 GOTO Start
[/code][/size]
I was·trying to·do this.
But I couldn't get it to work.
Any thoughts?
Thanks for your time.
ZeroEffect
Cr1State VAR Bit Start: LOW 0 HIGH 1 Cr1State = 1 '1 is open 0 is closed GOTO CheckState CheckState: DO IF IN1 = 0 THEN cr1 'cr1 in controll PAUSE 50 GOTO CheckState cr1: IF Cr1State = 0 Then SendCr1 GOTO CheckState SendCr1: Cr1State = 1 'Reset by another sub SEROUT 16, 16468, [noparse][[/noparse]"1"] '16468 = 9600 Baud HIGH 0 ' Flash led PAUSE 200 GOTO CheckState
But I couldn't get it to work.
Any thoughts?
Thanks for your time.
ZeroEffect
Comments
Sorry if I wasted your time
·
1. If you say "HIGH 1", then you have set pin 1 to an OUTPUT, and set its level to +5 volts.
If you then say "MyVal = IN1", then you are only reading the last state *you* set pin 1 to, you are not actually reading the input value on the pin. Instead you want to say "INPUT 1" -- now "IN1" will read the state on the pin.
2. You probably want to have an initialization section (like your "START:" label) but then also have a 'MAIN:' section. Most of your "GOTO START" should be "GOTO MAIN", and then put the MAIN: label where you have "DO" now. Once you've initialized your pins to input or output, you don't want to keep doing that every loop.
3. It looks like the "DO" has no corresponding "LOOP". Of course, that's a {PBASIC 2.5} construct -- but you still should not use "DO" as a label.
4. You should have "MyBaud CON 16468" in the start of your program, then use:
"SEROUT 16, MyBaud, [noparse][[/noparse]"1"] " ' -- This way, you can change the baud rate in *one* place, the 'CON' statement.
5. Any time I see lots of subroutines, which are identical except for a single parameter, I begin to think there *must* be a way to make a single routine with one changeable parameter out of them.
Could you give me an example
Here is my code as of now. It does what I need it to do but if there's a better/Right way to do this I would like to learn it.
Again Thank you for your help.
Paul
>>High 1
>>MyVal = IN1
>> Input 1 '???? This is where you lost me.
>>Input 1 = IN1 'Like this?
' Nope. More like this...
MyVal VAR BIT
MAIN:
' HIGH 1 ' This statement makes Pin 1 an OUTPUT, and sets it to +5. NOT something you want.
'INSTEAD, do:
INPUT 1 ' This statement makes Pin 1 an INPUT.
MyVal = IN1 ' This statement reads the state of pin 1, earlier set to an INPUT.
' And, go on...
Now it's making sense. The state would change by the voltage comming into the input right. Hang on.....
Ok I'm Back, quick eh? anyways I check the contact closures·and there is no votage on them, so with that the way I have now would be correct. I get the state change by the "dry" contact·taking the· +5 volts to ground.
· So That would make us both right, I guess
Thanks for your input I'm slowly getting it.
Paul
An input pin can't really read a 'dry closure' -- there must be a 10K ohm resistor in there somewhere to tie to +5 or GND to give the stamp a voltage to read -- BS2 inputs read voltage, not continuity.
Is is correct that you mentioned the inputs you are watching (IN1, IN3, IN5 etc) are not momentary closures, but remain closed until "reset" somehow?
The reason I ask is.....in the code snippet below, if IN1 = 0 and the switch connected to that input is not reset, the code will never check the state of the other inputs.
CheckState:
DO
LOW 0
IF IN1 = 0 THEN cr1 'cr1 in controll
IF IN3 = 0 THEN cr2 'cr2 in controll
IF IN5 = 0 THEN cr3 'Booth in controll
IF IN7 = 0 THEN cr5 'Cr1 booth pot on
IF IN9 = 0 THEN cr6 'Cr1 Booth pot off
IF IN11 = 0 THEN cr7 'cr2 booth pot on
IF IN13 = 0 THEN cr8 'Cr2 booth pot off
PAUSE 50
Loop
GOTO CheckState