Mega speed reading 128 push buttons

I am seeking advice for a project in which I need to read the equivalent of 128 push buttons. The buttons, (or contacts) are within the Hornby R044 lever and it has within it two passing contact switches. I have 64 of these already, which is why I am pursueing the idea of sensing them all via an Arduino. The lever passes the two contacts when it it is travelling between one side and the other. Once it is home, or latched on the other side, there is no contact made.

I cannot comprehend how fast a board such as the Mega can accurately read these, so am exploring the following options.

In all options only one lever is moved at a time.

Option 1, prefered, is that the arduino senses the first contact, starts a timer, senses the second contact and remembers the time taken between contacts. It then changes the state of a variable between 0 and 1 for that particular lever. The time is used later to move a servo at an appropriate speed.

I have tried this using analog pins and differing resistors in a resistor ladder. I found that it was too easy for the arduino to miss a lever being moved while it was reading other analog pins.

Option 2, The levers control latching relays. One for each lever. The arduino then reads the state of the relay. This is probably more my level of coding but uses more physical space and is less elegant. I would preferably use cascaded shift registers to read the state of the relays. Failing that, use analog pins again with resistor ladders.
I also lose the ability to read how fast the lever was moved between contacts.

In option 1, is it likely that an arduino can sense all the contacts suffiently quick enough through PISO shift registers or simply reading analog pins that it can sense and time the movement of any of the 64 levers?

I appreciate any input.

Have you considered wiring the switches as a 128 key matrix keypad ? That would allow you to directly detect any of the 128 keys individually using an 8 x 16 matrix using 24 digital keys

NOTE : I have no idea whether tis is feasible !

@UKHeliBob Thank you for the useful idea, I had wondered about this which would effectively make a keyboard, I will look further into it. I had stopped after reading most keyboards have less keys than the number I would need.

What do you refer to regarding "24 digital keys" ?

You would wire the 128 key matrix in a 16 x 8 pattern, thus using 24 digital inputs

Actually, as a 16x8 keypad, either 8 or 16 pins would actually be used as outputs.
Of course, that might better resolve as 12x12, or 144 possible buttons.

Then again, there are 64 in play now, which resolves as an 8ix8o keypad. Is there a need for more?

That needs further exploration. I'm doing 25 buttons per AI now, with a small routine set up as a class so I can theoretically expand to 25x8 on a classic nano clone. I haven't yet explored the occasional miss, but have put them down to the garbage buttons I tend to buy.

The way your mechanism works, timing of the contact pulse is quite variable, depending upon how fast the user crashes the lever across the contact zone. Are you sure that's not why you're missing events?

You are right. I misused the term "digital input" in post #4

What I should have said was

You would wire the 128 key matrix in a 16 x 8 pattern, thus using 24 digital inputs pins

Lose that line of inquiry.

What speed are we talking about? Usually microprocessors are way faster than real life?

I haven't looked at the thing you are talking about, if you don't think google will turn it up, please post any info you have or links to such.

a7

Why? 25 buttons per analog pin is quite feasible, with (at least) three caveats - non-trivial setup, speed of close-open, and single-event limited. First is down to the user, third was identified as non-issue by OP, which leaves the second item as a possible non-starter.

Eight MCP23017 would give you 128 inputs.
Each one has two interrupt output so you don't need to continuously poll the switches.

2 pins for the I2C and 8 pins for the interrupts.

Fair question.

I'm not so fond of analog stuff and have had mixed experiences; just a little gunshot gunshy I guess.

a7

The switches in question are manually operated, so at first sight speed of reading them is unlikely to be a problem

However, it seems that they are not on/off switches.

From Hornby R044 Switch | Model Railway Forum

Hornby R044 is a "Passing Contact Switch". Contact is momentarily made whilst the lever is moved from one side to the other. There is no contact whilst the lever is at either end.

In that case the use of interrupts to detect the momentary switch contact could be an advantage.

The contact closure in this instance is of limited duration, and will vary from super-short to very long, depending upon the speed with which the user operates the lever. That does suggest that an interrupt-driven approach would have a higher likelihood of success, provided the interrupt source also provides the identification of which pin triggered the interrupt, because the contact action could be fleeting.
The OP should look at the various forms one can purchase an MCP23017, and make a choice. The popular A'zon board is compact, but leaves the user with the task of going from each header pin to the push buttons. There are options in the model railway world that might work better, such as MRCS' IOX32, which provide you with both screw terminal options for input, and the ability to insert interface components should you wish; of course, at a cost, unless you buy the bare board and source the components yourself.
Two of those would address the 64 inputs described, and buying more is an option to get to 128- the I2C bus will support 4 of these. The OP will have to decide whether convenience at a price is palatable, or whether the A'zon option more suits their purpose.

The MCP23017 will also latch the pin state on change, so it will work like @Loosecann0n latching relay idea.

Thank you all so far for the input and correct analysis of what I was trying to achieve. I appologise for not replying sooner, xmas other chaos got in the way.

I will follow the MCP23017 route, it seems the most likely to function how I am hoping.

I noticed there is a forum for requesting paid help with projects, I am considering requesting paid help with this. This project is for a toy for my kids (and myself) but I worry they will have grown up by the time I have succesfully completed it.

If you want this topic to be moved to the Jobs and Paid Consultancy category of the forum please flag it for the attention of a moderator as "Something Else" and ask for it to be moved

If you have enough pins, you might consider using MCP23S17, the SPI version, which is even faster to read than the I2C version.

This is again interesting. I am not certain though, the board I am using is a Mega with an Arduino wifi sheild of some years on top. I believe it uses SPI too, could this be an issue with the overall speed or max numbers of devices connected to SPI?

I don't think speed will be an issue with reading those lever switches, especially since you said you will be moving one at a time. I personally would lean towards the I2C version since I think the wirining may be simpler

It might be good to get data from 1 lever that you move fast to see how long each contact is made and the time between.

Direct port reads can be made at 8 pins each read in less than a microsecond including masking bits to identify pins.
Bounce in your case can be handled (to not get extra positives) different ways. Consider that 2 millis bounce is normal for contact switches.
On a Mega2560 I would go with 1 port (8 pins) set as INPUT_PULLUP and 16 pins that change from INPUT LOW (zero-state) to OUTPUT LOW to sense 1 row at a time.

To read all 128 switches would be awfully fast, quicker than a single 105 microseconds analog read and note that when you switch analog pins you have to wait at least 55 micros 'settle time' to get a good read.