Reading dip-switch values
Archiver
Posts: 46,084
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
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
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
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
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
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]
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)