Shop OBEX P1 Docs P2 Docs Learn Events
Help with coding a contact closure(s). — Parallax Forums

Help with coding a contact closure(s).

Paul RoyPaul Roy Posts: 5
edited 2004-10-30 06:13 in BASIC Stamp
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.

[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.
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? confused.gif
Thanks for your time.

ZeroEffect

Comments

  • Paul RoyPaul Roy Posts: 5
    edited 2004-10-28 16:43
    Ok I found my error blush.gif . ya know if your going to set a variable to a value make sure it the correct value.rolleyes.gif

    Sorry if I wasted your time

    ·
  • allanlane5allanlane5 Posts: 3,815
    edited 2004-10-28 17:25
    Wow, where to start?

    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.
  • Paul RoyPaul Roy Posts: 5
    edited 2004-10-28 18:13
    Thanks Allanlane5 for your help. I'm still new at this so I'm not really following the code structure on·your first point.
    Allanlane5 said...
    1. If you say "HIGH 1", then you have set pin 1 to an OUTPUT, and set its level to +5 volts. (I understand)
    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.

    Could you give me an example
    Main: 
    High 1 
    MyVal = IN1 
    Input 1 '???? This is where you lost me.
     
    Input 1 = IN1 'Like this?
    

    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
    ' {$STAMP BS2} 
    ' {$PBASIC 2.0}
     
    Cr1State VAR Bit 
    Cr2State VAR Bit 
    Cr3State VAR Bit 
    BaudRate CON 16468 '9600
     
    Main: 
    LOW 0 
    HIGH 1 
    HIGH 3 
    HIGH 5 
    HIGH 7 
    HIGH 9 
    HIGH 11 
    HIGH 13 
    Cr1State = 0 '1 is open 0 is closed 
    Cr2State = 0 '1 is open 0 is closed 
    Cr3State = 0 '1 is open 0 is closed 
    GOTO CheckState
     
    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
     
    cr1: 
    IF Cr1State = 0 THEN SendCr1 
    GOTO CheckState 
    
     
    SendCr1: 
    Cr1State = 1 'Reset by another sub 
    SEROUT 16, BaudRate, [noparse][[/noparse]"1"] '16468 = 9600 Baud 
    HIGH 0 ' Flash led 
    PAUSE 200 
    Cr2State = 0 
    Cr3State = 0 
    GOTO CheckState 
    
     
    cr2: 
    IF Cr2State = 0 THEN SendCr2 
    GOTO CheckState 
    
     
    SendCr2: 
    Cr2State = 1 'Reset by another sub 
    SEROUT 16, BaudRate, [noparse][[/noparse]"2"] '16468 = 9600 Baud 
    HIGH 0 ' Flash led 
    PAUSE 200 
    Cr1State = 0 
    Cr3State = 0 
    GOTO CheckState 
    
     
    cr3: 
    IF Cr3State = 0 THEN SendCr3 
    GOTO CheckState 
    
     
    SendCr3: 
    Cr3State = 1 'Reset by another sub 
    SEROUT 16, BaudRate, [noparse][[/noparse]"3"] '16468 = 9600 Baud 
    HIGH 0 ' Flash led 
    PAUSE 200 
    Cr1State = 0 
    Cr2State = 0 
    GOTO CheckState 
    
     
    cr5: 
    SEROUT 16, BaudRate, [noparse][[/noparse]"5"] 
    HIGH 0 
    PAUSE 200 
    GOTO CheckState 
    
     
    cr6: 
    SEROUT 16, BaudRate, [noparse][[/noparse]"1"] 
    HIGH 0 
    PAUSE 200 
    GOTO CheckState 
    
     
    cr7: 
    SEROUT 16, BaudRate, [noparse][[/noparse]"6"] 
    HIGH 0 
    PAUSE 200 
    GOTO CheckState 
    
     
    cr8: 
    SEROUT 16, BaudRate, [noparse][[/noparse]"2"] 
    HIGH 0 
    PAUSE 200 
    GOTO CheckState
    
  • allanlane5allanlane5 Posts: 3,815
    edited 2004-10-28 19:45
    >>Main:
    >>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...
  • Paul RoyPaul Roy Posts: 5
    edited 2004-10-28 21:18
    Thanks Again Allanlane5 tongue.gif·.

    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.

    idea.gif· So That would make us both right, I guess

    Thanks for your input I'm slowly getting it.

    Paul
  • allanlane5allanlane5 Posts: 3,815
    edited 2004-10-29 13:34
    You should probably download the Basic Stamp Programming Manual (PDF download from www.parallax.com). On the 'Button' command, this shows you what circuit the BS2 need to be able to read a high (1, +5) or a low (0, or GND).

    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.
  • KenMKenM Posts: 657
    edited 2004-10-30 06:13
    I would like to pipe in here.

    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
Sign In or Register to comment.