Shop OBEX P1 Docs P2 Docs Learn Events
converting switch states to decimal number — Parallax Forums

converting switch states to decimal number

ArchiverArchiver Posts: 46,084
edited 2003-03-08 21:23 in General Discussion
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

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2003-03-08 20:59
    Yes, you can do it in one line of code:

    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]
  • ArchiverArchiver Posts: 46,084
    edited 2003-03-08 21:23
    In a message dated 3/8/2003 1:01:50 PM Pacific Standard Time, jonwms@a...
    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]
Sign In or Register to comment.