I'm making a HID device with 128 buttons on a Uno R3, around 50% of these buttons need to work at the same time (with regard to human perception)
my current best (simplest) guess at design uses 4 * 32:1 multiplexers (due to price reduction for buying multiples [as I need several for analogue also] and because I fully understand how they work)
Given that this set up will eat 5 digital outs (shared) for muxing and 1 pin per input, would a port expander offer any advantages?
If they need to work at the same time, then capturing the state of all at once is needed. Capture them all at once with 74hc165, transfer in with spi.transfer.
If you attach a diode to each switch, you can organise them into a 7 x 8 matrix and read them with 8 arduino pins. No other components needed.
PaulRB:
If you attach a diode to each switch, you can organise them into a 7 x 8 matrix and read them with 8 arduino pins. No other components needed.
If it's 7 rows by 8 columns and you read the columns, would setting the row you want to OUTPUT HIGH while setting the other rows to INPUT LOW then could you get away without the diodes?
Also maybe I'm a dunce here but how does a 7 x 8 multiplex cover 128 buttons? Charlieplexing?
GoForSmoke:
If it's 7 rows by 8 columns and you read the columns, would setting the row you want to OUTPUT HIGH while setting the other rows to INPUT LOW then could you get away without the diodes?
Yes, but only if you can guarantee (or don't care) if more than one button is pressed at once.
GoForSmoke:
Also maybe I'm a dunce here but how does a 7 x 8 multiplex cover 128 buttons? Charlieplexing?
Same principal, yes. Example attached with 6 switches using 3 outputs.
If you use a port expander they have a so called interrupt output. This can be set up so that it changes when any of the 16 inputs change. Therefore your code only has to monitor this one pin to see if you need to access that port expander. This makes your code much more speedy as you don't need to keep scanning all the inputs.
PaulRB,
After looking for a while I see how that should work. It looks simpler than I remember Charlieplexing looking.
Is the right word for the number of switches "combinatorial"? It's been so long since math class....
I remember a thing called triangle numbers that describes how many connections you'd get,
X = ( N * ( N + 1 )) / 2
X = ( 3 * ( 3 + 1 )) / 2 = 6 in the case of the example, 8 lines would do 36 switches (as opposed to 4 x 4 = 16 ).
Matrices are a big mess of wires/diodes if you want each switch readable with no ghosting.
I2C I/O expanders with 16 inputs are cheap and need no external components. eg. PCF8575, MCP23017, ... and many more.
They have three address pins so you can have 8 chips connected at once, that's 128 completely independent switches and neat wiring. Only two Arduino pins needed.
GoForSmoke:
Is the right word for the number of switches "combinatorial"?
No, its quite simple. For Charlieplexing switches (with diodes), or LEDs, using N outputs, imagine a square matrix with N rows and N columns, making NN positions. But there is a diagonal line of positions that can't be used (where same output would be both row and column), so that's NN-N.
But looking back at my earlier post, I made a howling mistake (which you spotted). It would have to be 12 x 11 matrix, needing 12 Arduino outputs for up to 132 switches. 8 outputs would only allow for 56 switches.
Fungus may be right about the practicalities of charlieplexing that many switches, I don't know, I've never made one that big, but the theory is sound.
Paul
I still think 16 shift registers, read in at 8 Mhz speed taking about 18 microseconds, is the way to go.
Would be the most responsive.Gives you lots of time to change whatever, or send a midi message, between samples.
Am on a smart phone for a couple of days, otherwise I'd post some simple code to start.
Thanks to all, no need to post up any code as there are many tutorials I have found, I have a look at both shift registers and the I2C option, charlieplexing looks like it would be a bit messy sorting out the wiring.
I2C is nice for a Uno R3 as the SDA and SCL pins are separate pins, but it might cause problems with the HID code. Only 3 pins used with shift registers, looks like it will be the way to go.
Question how far can I go with the number of shift registers? Would using 20 offer any problems?
20 is not a problem. I have a box running 27 tcip6b595 right now.
