Perhaps I'm not phrasing my searches quite right, but I've had limited success in finding guidance on the approach for this project and would appreciated any enlightenment the community can share on reading the state of multiple digital inputs into an array...
Essentially, the project I'm looking at involves a number of push-button inputs (lets say 5) of which 4 are used to set the array value and the 5th is used to conduct an action based on the value of the array.
I intend for the system to function as follows:
User presses and holds the 4 array buttons in the desired configuration (e.g. any pattern between none and all of them)
Still holding the selected array buttons, the user presses and holds the 5th button, causing the array buttons to be read and written to a variable (lets call it ARRAY)
The user may now release the array buttons (if desired) without interrupting the process
An action is performed, depending on the value of ARRAY
The action continues until the 5th button is released
Now I'm fairly confident in applying my novice knowledge to achieve this based using digitalread() to set the value of ARRAY if only a single button is used (not much of an array!), but how would I go about scaling it to work with multiple buttons?
Please also note that that the system action performed on the basis of the array is short (10s of seconds) and the user is likely to repeat the steps shortly after the previous iteration has completed. The intent is not to set up a long-term static configuration.
Many thanks in advance for any forthcoming assistance.
Thanks dougp, I'm looking to implement this on a Nano to fit the desired form factor. Would the same port approach be applicable to that rather than an UNO?
I'll make sure to familiarise myself with bitWrite() and explore options.
Edit: Looking at bitWrite() I could use this to read the value of each button and construct a 4-digit value for ARRAY ranging from 0000 - 1111. Is that interpretation correct?
As for logical operators, I am versed in the concept, but not necessarily execution in an Arduino (or any programming) application.
Create an Array with the pin numbers of the buttons you want to multiplex. In loop, read the master button with digitalRead. When it's LOW, because of course you used INPUT_PULLUP for your pinmode, use a for loop to digitalRead each of the pins in your array. Put the results in another array if you like and then do whatever you're planning with them.
flammableguy:
Thanks dougp, I'm looking to implement this on a Nano to fit the desired form factor. Would the same port approach be applicable to that rather than an UNO?
Looks like it. The low four bits of ports B and C are available.
flammableguy:
Edit: Looking at bitWrite() I could use this to read the value of each button and construct a 4-digit value for ARRAY ranging from 0000 - 1111. Is that interpretation correct?
Yep. Even if the input pins are separated, and possibly even on different ports, bitWrite() could drop the input values into your byte variable of choice. Still need to do the masking, or, initialize the variable to zero before putting in the bits.