Shop OBEX P1 Docs P2 Docs Learn Events
Reading dip-switch values — Parallax Forums

Reading dip-switch values

ArchiverArchiver Posts: 46,084
edited 2003-03-07 00:36 in General Discussion
I need my Stamp 2SX to detect the states of a 4-switch dip and set
some variables based on what it finds. I have one side of each switch
hooked to the Stamps's ground pin, and the other side of each switch
hooked to a seperate i/o pin.

Questions:
1) Is this the proper hardware configuration?

2) Do I need a resistor of any kind in the system to the protect the
Stamp?

3) Would I use a BUTTON statement, or an 'if in2=1' clause or
something else in the code to read the states of the switches?

4) The project is to be set up so that the switches' positions are
read once at power-on and then not again until the unit is powered off
and on. Do I need to take any measures in the code to keep the 'on'
switches from wasting current after they're read and the variables are
assigned based on their value? (hope that makes sense)

Any advice or sample code would be greatly appreciated!
Thanks in advance,
--Alex

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2003-03-06 23:01
    Alex,

    I would use the [noparse][[/noparse]if in (pin #) = 1 then .....) command. No, you do
    not have to series connect a resistor between the i/o pin and +5volt
    (or GND). But don't forget to tie a 10k ohm resistor to ground and
    the other side to your i/o pin, or the state of the pin will float
    hi/low at random.

    Regards,
    Brian Dalziel
    Milwaukee, WI, USA

    --- In basicstamps@yahoogroups.com, "atl_guy1138" <atl_guy1138@y...>
    wrote:
    > I need my Stamp 2SX to detect the states of a 4-switch dip and set
    > some variables based on what it finds. I have one side of each
    switch
    > hooked to the Stamps's ground pin, and the other side of each switch
    > hooked to a seperate i/o pin.
    >
    > Questions:
    > 1) Is this the proper hardware configuration?
    >
    > 2) Do I need a resistor of any kind in the system to the protect the
    > Stamp?
    >
    > 3) Would I use a BUTTON statement, or an 'if in2=1' clause or
    > something else in the code to read the states of the switches?
    >
    > 4) The project is to be set up so that the switches' positions are
    > read once at power-on and then not again until the unit is powered
    off
    > and on. Do I need to take any measures in the code to keep the 'on'
    > switches from wasting current after they're read and the variables
    are
    > assigned based on their value? (hope that makes sense)
    >
    > Any advice or sample code would be greatly appreciated!
    > Thanks in advance,
    > --Alex
  • ArchiverArchiver Posts: 46,084
    edited 2003-03-06 23:01
    Alex,

    I would use the [noparse][[/noparse]if in (pin #) = 1 then .....) command. No, you do
    not have to series connect a resistor between the i/o pin and +5volt
    (or GND). But don't forget to tie a 10k ohm resistor to ground and
    the other side to your i/o pin, or the state of the pin will float
    hi/low at random.

    Regards,
    Brian Dalziel
    Milwaukee, WI, USA

    --- In basicstamps@yahoogroups.com, "atl_guy1138" <atl_guy1138@y...>
    wrote:
    > I need my Stamp 2SX to detect the states of a 4-switch dip and set
    > some variables based on what it finds. I have one side of each
    switch
    > hooked to the Stamps's ground pin, and the other side of each switch
    > hooked to a seperate i/o pin.
    >
    > Questions:
    > 1) Is this the proper hardware configuration?
    >
    > 2) Do I need a resistor of any kind in the system to the protect the
    > Stamp?
    >
    > 3) Would I use a BUTTON statement, or an 'if in2=1' clause or
    > something else in the code to read the states of the switches?
    >
    > 4) The project is to be set up so that the switches' positions are
    > read once at power-on and then not again until the unit is powered
    off
    > and on. Do I need to take any measures in the code to keep the 'on'
    > switches from wasting current after they're read and the variables
    are
    > assigned based on their value? (hope that makes sense)
    >
    > Any advice or sample code would be greatly appreciated!
    > Thanks in advance,
    > --Alex
  • ArchiverArchiver Posts: 46,084
    edited 2003-03-06 23:05
    See the earlier message 'Button Help' for debouncing.

    Setting a Stamp's pins to input automatically makes
    them high-impedance -- I don't think you can read
    switches with the circuit you detailed.

    --- In basicstamps@yahoogroups.com, "atl_guy1138" <atl_guy1138@y...>
    wrote:
    > I need my Stamp 2SX to detect the states of a 4-switch dip and set
    > some variables based on what it finds. I have one side of each
    switch
    > hooked to the Stamps's ground pin, and the other side of each
    switch
    > hooked to a seperate i/o pin.
    >
    > Questions:
    > 1) Is this the proper hardware configuration?
    >
    > 2) Do I need a resistor of any kind in the system to the protect
    the
    > Stamp?
    >
    > 3) Would I use a BUTTON statement, or an 'if in2=1' clause or
    > something else in the code to read the states of the switches?
    >
    > 4) The project is to be set up so that the switches' positions are
    > read once at power-on and then not again until the unit is powered
    off
    > and on. Do I need to take any measures in the code to keep the 'on'
    > switches from wasting current after they're read and the variables
    are
    > assigned based on their value? (hope that makes sense)
    >
    > Any advice or sample code would be greatly appreciated!
    > Thanks in advance,
    > --Alex
  • ArchiverArchiver Posts: 46,084
    edited 2003-03-06 23:35
    While not a requirement, it's a good idea to put a series resistor in line
    with the Stamp pin (220 ohms will do). That way, if you press the button and
    the pin has been made an output, the series resistor will protect the Stamp.

    In the case you describe, you also need pull-ups to keep the pin at a known
    (high) state when the switch is open. Letting the pins float is not
    reliable. I'll try to draw a diagram in text:


    +---[noparse][[/noparse]10K]
    Vdd
    |
    |
    Stamp ---[noparse][[/noparse]220]----*
    |
    |
    +---[noparse][[/noparse]N.O. Switch]
    Vss

    You can minimize the current through the "on" switches by bumping the 10K to
    50K.

    -- Jon Williams
    -- Parallax




    In a message dated 3/6/2003 4:35:12 PM Central Standard Time,
    atl_guy1138@y... writes:

    > I need my Stamp 2SX to detect the states of a 4-switch dip and set
    > some variables based on what it finds. I have one side of each switch
    > hooked to the Stamps's ground pin, and the other side of each switch
    > hooked to a seperate i/o pin.
    >
    > Questions:
    > 1) Is this the proper hardware configuration?
    >
    > 2) Do I need a resistor of any kind in the system to the protect the
    > Stamp?
    >
    > 3) Would I use a BUTTON statement, or an 'if in2=1' clause or
    > something else in the code to read the states of the switches?
    >
    > 4) The project is to be set up so that the switches' positions are
    > read once at power-on and then not again until the unit is powered off
    > and on. Do I need to take any measures in the code to keep the 'on'
    > switches from wasting current after they're read and the variables are
    > assigned based on their value? (hope that makes sense)
    >
    > Any advice or sample code would be greatly appreciated!
    > Thanks in advance,
    > --Alex



    [noparse][[/noparse]Non-text portions of this message have been removed]
  • ArchiverArchiver Posts: 46,084
    edited 2003-03-07 00:36
    I would put a 330 ohm resistor in series between the pin and the switch, and
    use a 10k resistor between +5 volts and the switch. The 330 ohm resistor
    protects the Stamp pin should you accidentally make it an output and drive
    it high, then press the switch. The 10k resistor pulls the pin high (rather
    than leaving the input floating) when the switch is not pressed.

    Original Message

    > I need my Stamp 2SX to detect the states of a 4-switch dip and set
    > some variables based on what it finds. I have one side of each switch
    > hooked to the Stamps's ground pin, and the other side of each switch
    > hooked to a seperate i/o pin.
    >
    > Questions:
    > 1) Is this the proper hardware configuration?
    >
    > 2) Do I need a resistor of any kind in the system to the protect the
    > Stamp?
    >
    > 3) Would I use a BUTTON statement, or an 'if in2=1' clause or
    > something else in the code to read the states of the switches?
    >
    > 4) The project is to be set up so that the switches' positions are
    > read once at power-on and then not again until the unit is powered off
    > and on. Do I need to take any measures in the code to keep the 'on'
    > switches from wasting current after they're read and the variables are
    > assigned based on their value? (hope that makes sense)
Sign In or Register to comment.