Hi there I am brand new to the Arduino ecosystem and have very little knowledge of electronics so far (working on it). I am building a suduko style puzzle for an escape room using a 4x4 grid of 4 position rotary switches. To solve the puzzle no switch can be set to the same value as any in the row or column or in the 4 2x2 grids. I could hard wire a single solution but would like to make the puzzle general purpose and have the solution determined by a micro controller.
My question is, what is the most efficient way of interfacing the switches to the Arduino to determine the position of each switch in the grid? It seems as though some form of encoder would be necessary. I am thinking if I can make the reading of each switch addressable and the value of each switch binary encoded to use 2 bits.
It's quite common to read a 4x4 keypad by driving one row at a time and scanning columns, needing 8 lines. You might generalise that by arranging resistors on your switches such that each position gives a different resistance, which you could sense by applying a suitable bias current and looking at the voltage developed with the analogue inputs. Some steering diodes might be needed, I haven't thought this through completely.
If you already plan to use a Mega, then you have plenty of pins. But for most other types of arduino, some extra chip might be needed. If we know what you plan to use, we can advise what extra chip to use.
I also thought along the lines of a matrix.
I also realised that you can ignore one of the terminals on each rotary switch: If you can detect that the switch is not in one of positions 1, 2 or 3, it must be in that 4th position.
Yes looks like I could combine pins on the switches to have 2 lines per switch (2 bits) and then use either 4 x 8-to-1 or 2 x 16-to-1 multiplexers like the HEF4067BP.
There are many ways to approach it. Your idea could work, but this is an interesting optimization problem: how to achieve a solution with minimum extra components/pins. I'm pretty sure your idea can be beaten! I'm giving it some thought...
You could still hardwire the switches, but use spade connectors to allow the escape room proprietor rewire them. This looks like an easy problem looking for a hard solution.
Using rotary encoders in a matrix is not a very good idea, because it will be easy to miss pulses from them because of the time it takes to scan the matrix. Normally a single encoder would need to generate oat least one and preferable two interrupts so they could be read fast enough so as to not miss pulses and get a false number from them.
This is not so bad if the encoders have a knob on them with no pointer on it, and you have some feedback elsewhere that displays the number you have reached with each encoder.
But really I would use a pot to replace each encoder, like others have suggested.
It sounds like if you're reading a 4 position mechanical rotor switch, in each of the 4x4 matrix, then youve got 444 total pins to read. So 64 pins.
You could use 4, 16 channel multiplexers with a simple loop to read all 4x4x4 positions continually.
Off the top of my head, this would need 16 pins to read.
(A 5th multiplexor could reduce the pin count further, but not needed with a Mega)
When the user rotates the switch into a position youll know which position is "high"
So your arrary would be
[SwitchPosition, column, row] with either a high or low reading
The person in charge would select the appropriate winning array combo. You would need to be careful with the logic of switch position cannot equal itself if the column or row equals itself