Button box problem more than 20 controls

Hello

i making a button box for a friend, he need more than 20 input that the leonardo have, i have been looking at a matrix but am i right to say that does not work with rotary encoder.

He need this
4 rotary encoder (12 input)
4 Push buttons (4 input)
9 toggle switch they need to hold down the joystick button until switch back to off ( 9 input)
2 pull switch basic works like toggle switch, just pull out instead of flip ( 2 inputs)
1 key (2 inputs)

so total 29 inputs needed, i was told i could do it with a mega if i got one with atmega16u2, i can not get it to works guess i to new to arduino world, could someone please help me ?

I wonder if you were to run a three outputs from a encoder into stepped resistors and combine into one pin.

So now the encode output would be measured as analog value on that pin.

Never tried it, but would save you 8 pins alone right there if it works.

Rotary encoders normally have two outputs, that's only eight total.

You could use shift registers for the switches and use only a small number of I/O pins.

You list all inputs. Surely some sort of outputs are needed, no?

He need this
4 rotary encoder (12 input)
4 Push buttons (4 input)
9 toggle switch they need to hold down the joystick button until switch back to off ( 9 input)
2 pull switch basic works like toggle switch, just pull out instead of flip ( 2 inputs)
1 key (2 inputs)

so total 29 inputs needed,

So just use real inputs for the 8 inputs you need for rotary encoders and then use port expander chips for the others, like the MCP23016, you can use up to 8 at a time and you get 16 I/O pins for each. Also their is a single output that signals a change as been made to selected inputs, so this makes monitoring and dealing with pushes easy.

i need joystick button press output to all the switches..

I thought the MEGA has 54 digital pins ? that seems to be more than needed.

can you post a link to the encoder ? most encoders we use only use 2 pins. maybe your encoders are different ?

4 push buttons. if you know, for certain, that no two will ever be pushed at the same time, you can wire them with resistors in series and connect to an Analog pin.

the toggles could be wired in series with resistor to use 2 onto one analog pin
the key could use 1 analog pin
the two pull switches could use one analog pin

heino79:
i need joystick button press output to all the switches..

What does that mean?

The chip I told you about has programmable I/O pins.

dave-in-nj:
I thought the MEGA has 54 digital pins ? that seems to be more than needed.

can you post a link to the encoder ? most encoders we use only use 2 pins. maybe your encoders are different ?

4 push buttons. if you know, for certain, that no two will ever be pushed at the same time, you can wire them with resistors in series and connect to an Analog pin.

the toggles could be wired in series with resistor to use 2 onto one analog pin
the key could use 1 analog pin
the two pull switches could use one analog pin

the rotary encoder i have have 3 switch(left, right and push) legs and 2 ground.

Yes the mega have enhoug input, but how do i get it to act like a game controler / joystick so i can make it send this Joystick.pressButton(2);

The rotaries have buttons too.

Can’t all of your buttons look the same electrically, either ON or OFF? And the key too, treat as button.
Then you can wire them all as a matrix to get the ON/OFF. In your software, you make the toggles toggle, etc, for what changes get made when the button changes, how it seems to act… but electrically they can be the same.

20 button matrix needs 4+5 IO pins, 20 switches of any sort and with 20 diodes you can press multiple buttons at once.

Most people I’ve seen do rotaries use an interrupt for each. AVR allows state change interrupt on any digital pin.
It’s potentiometers that need analog. Each analog read takes at least 105 microseconds, very slow compared to digital operations.

I have a matrix example sketch if you want. It debounces pretty quickly.

Grumpy_Mike:
like the MCP23016

Better use the MCP23017 instead (is the MCP23016 even still available?).

Or for inputs only the PCF8575 (cheaper than the MCP but outputs can only sink current).

the rotary encoder i have have 3 switch(left, right and push) legs and 2 ground.

Are you sure of that? Most hand driven encoders have a quadrature phase output not a left and right. As these are hand turned it is quite easy to keep track of them by polling rather than interrupts. Also you can only move two at once due to the limitation on the number of hands you have.

Depending on the exact application you might even be able to miss the odd pulse without noticing. For example the old mouse design ( with a ball ) often missed pulses but because you have feedback in the form of a visual pointer you just keep on going until you arrive at the right point.

Better use the MCP23017

Yes quite right, sorry.

Grumpy_Mike:
Most hand driven encoders have a quadrature phase output not a left and right.

Gray Code? You can know that you missed some and still get the right position.

GoForSmoke:
Gray Code? You can know that you missed some and still get the right position.

They don’t tend to have that because of all the extra outputs you would need. Unless you consider a quadrature output to be a Gray code.

Gray code is 4 bits but they do not sequence as binary counting. Gray named it Reflected Binary but engineers that used it named it Gray code perhaps because it's brilliant. It has to do with detecting state change of switches that don't all flip at the same instant, the Gray Code only changes 1 bit going from one state to the next, if you see 2 bits changed you know where you are... just don't miss 4 steps!

Gray invented this back in the electro-mechanical days, IIRC the late 1930's.

Some day I might get in my head the workings to where I don't forget but I think I'm too far along for that, I need to find a reference and have a good day (a map and a near miracle anymore) just to mess with it.

Anyway the button on the rotary is just an added button. Matrix that and the rest and code which ones are toggles to act in code as toggles.

You can use a resistor network to decide which button was pushed. If you had SPDT switches, an R-2R network would be the way to go.

Something like this, just instead of the opamp buffer connect the Arduino.

Alternatively, for SPST, you can also use this (the voltages will be a bit different) or you can make the resistor network however you want and just use that pic for inspiration.

I have done this before and I think it should allow you to use more than 10 buttons per one input.

Gray code is 4 bits

Well it can be any number of bits you want.
I have a 128 position rotary switch with 7 bits out that use a Gray code.

I used to have my students make them up for homework questions that started at a unique bit pattern for each student so they could not copy each other’s work.

There's only 4 bits in the tutorial what I worked thru then. Don't remember seeing more than 4 bits before either but never used it, a curiosity for me.

I guess the user needs to click slow and see what comes out.