Monitoring the state of multiple buttons

Hi All,

I'm building a controller for a large model train set which has approx 30 sets of electronic points. I need to be able to identify when one of 30 points buttons has been pressed and then jump to the appropriate subroutine to change the associated set of points and LED indicator. I know this can be done using nested "if.....else" statements but this seems very code wasteful.

Any ideas on more efficient code.

Hope you can help.

Kind regards - Frank

It depends how you have wired the buttons, but assuming one button per pin, put the pin numbers in an array and, in a for loop, scan the array and read the pins.

As the buttons control points the action(s) to be taken when one is pressed is premably very similar, ie change the points and set signals. So, use an array of pin numbers and iterate through it to determine whether one of them has become pressed. This will give you the index number of the button. Use this index to access other arrays (or a struct) to determine the points and signals to be changed.

The Arduino is very good at asking "Are we there yet?" thousands of times per second. It never gets bored.

The "most efficient" code is the code which takes the least amount of time to write and meets the specification. Nobody, least of all the Arduino, cares if you have 30 if() statements if they do the right thing.