Dip Switch Decode // Compare Value to Array

Hello Everyone,

I'm working on a project and due to the popularity of Arduino in regards to what I am searching for, Google has given me nothing but a mountain of useless information unrelated to what I need.

I'm sure the answer is simple, I'm probably just trying to overcomplicate it. Here is the gist of what I need to do:

  • Read the inputs from a 4-bit DIP switch
  • Decode inputs and convert these four inputs into a binary string? (0001, 0011, 1001 etc...)
  • Compare this binary input to an array of values and light an LED if it matches: example. if input equals any value in the array {0011, 1001, 1010, 0111} then turn on a green LED for 10 seconds, if not turn red LED.

I'm having trouble on how to convert the inputs from the dip switches, and how to compare this value to the values in the array.

Should I be using the bitread/bitwrite functions? I've read up on them but cannot find a single example of how to use them in this case. I don't want to do anything funky like a resistance ladder to read the dips. I just want to use four inputs. Should I need to convert these values to decimal to compare to the array?
I can also add a pushbutton to initiate a read of the dip switches so it's not constantly running.

I'm having trouble on how to convert the inputs from the dip switches, and how to compare this value to the values in the array

Please illustrate your "trouble".
If your C bit handling is not up to scratch, then bitWrite is the way to go.

So how would you do that with a 4-dip switch? Do you need combine it with an IF: High-Low state?

One way would be to convert the values from the 4 DIP switches into a 4-bit integer (i.e. 0 to 15). Then use that value to index into a 16-element byte array. The value in each array element would tell you whether or not to light the LED.

One input pin per switch.
Each switch wired between input pin and ground, with the built-in pull-up resistor enabled.
A close switch will read LOW, an open switch will read HIGH.

Roger that, I already have the circuit part down... I'm just asking about the programming aspect. Converting those inputs to binary string and comparing to an array. That's the part that I am a bit clueless about.

Switch zero, weight 20
Switch one,weight 21
Switch two, weight 22
Switch three, weight 23

So are you suggesting converting to decimal, summing, and then comparing to the values in the array?

There is no reason to convert numbers to decimal - the processor stores all numbers as binary. Don't confuse things with strings

One truly great thing about the Arduino is that it makes things dead easy to try.

If you get into difficulties, post your code and your ideas and what you observe, and you will get help.

AWOL:
Switch zero, weight 20
Switch one,weight 21
Switch two, weight 22
Switch three, weight 23

And to follow up a bit:

Make a byte and give it a name.

Zero that byte.
Read switch 0 and if HIGH, add 1 to the byte.
read switch 1 and if HIGH, add 2 to the byte.
read switch 2 and if HIGH,add 4 to the byte.
read switch 3 and if HIGH, add 8 to the byte.

Now you have the bits set in the byte to match the DIP switch.

Paul

tlminings:
Roger that, I already have the circuit part down... I'm just asking about the programming aspect. Converting those inputs to binary string and comparing to an array. That's the part that I am a bit clueless about.

tlminings:
Roger that, I already have the circuit part down... I'm just asking about the programming aspect. Converting those inputs to binary string and comparing to an array. That's the part that I am a bit clueless about.

You don't *compare * it to the array, you use it to *index into * the array and do something with what is found at the given array address.

If you implement code based on @AWOL's posts (and others) don't forget you'll need to invert the bits before using the value collected.

The gist of it is that the value generated by the bit pattern, only 16 possible, becomes the index value used to access the array. So, if the four bits give you a value of 7 then you get the value contained in arrayAddress[7]. In practice the number in the brackets is replaced by the variable name you give to the bit pattern so it can be changed at runtime.

You probably don't know about "lookup tables" but that is what is being talked about. Google lookup tables.