converting switch states to decimal number
Archiver
Posts: 46,084
I need my Stamp 2SX to convert the states of its two dipswitches to a
number from 0-3 using binary math, i.e.:
switch 1 off, switch 2 off => 0
switch 1 on, switch 2 off => 1
switch 1 off, switch 2 on => 2
switch 1 on, switch 2 on => 3
Is there a way to use the values of their input pins (in1 and in2) to
make this conversion using a single statement instead of a bunch of
if...then's?
Thanks
--alex
number from 0-3 using binary math, i.e.:
switch 1 off, switch 2 off => 0
switch 1 on, switch 2 off => 1
switch 1 off, switch 2 on => 2
switch 1 on, switch 2 on => 3
Is there a way to use the values of their input pins (in1 and in2) to
make this conversion using a single statement instead of a bunch of
if...then's?
Thanks
--alex
Comments
switches = InA & %0110 >> 1
Do I win a prize?
-- Jon Williams
-- Parallax
In a message dated 3/8/2003 1:48:15 PM Central Standard Time,
atl_guy1138@y... writes:
> need my Stamp 2SX to convert the states of its two dipswitches to a
> number from 0-3 using binary math, i.e.:
>
> switch 1 off, switch 2 off => 0
> switch 1 on, switch 2 off => 1
> switch 1 off, switch 2 on => 2
> switch 1 on, switch 2 on => 3
>
> Is there a way to use the values of their input pins (in1 and in2) to
> make this conversion using a single statement instead of a bunch of
> if...then's?
>
> Thanks
> --alex
>
[noparse][[/noparse]Non-text portions of this message have been removed]
writes:
> Yes, you can do it in one line of code:
>
> switches = InA &%0110 >>1
>
> Do I win a prize?
>
Yes, you win a prize......you have been awarded a job at Parallax.....
[noparse][[/noparse]Non-text portions of this message have been removed]