I connected a 74HC151 multiplexer to my arduino board to access a switch matrix, and I'm using a single pin for the digital input from the multiplexer output. How can i store and compare the digital readings of the input? Should I store the readings into an array? Please help. Thanks.
byte vals [8];
for (i = 0; i < 8; i++) {
setMuxAddress(i); // does whatever is required to set the three select inputs on the MUX
vals[i] = digitalRead(muxPin);
}
Next thing I need to know is how to compare each position in the array. Suppose I have an array of:
[0,1,0,1,0,1,1,1]
with the leftmost position being the first or lowest entry, and the rightmost position is the last or highest entry.
I need to select and compare the positions that has 0 binary value, so that I can transmit serially the highest position that has 0 binary value.
Using the array above as example, I need to select the positions that yielded 0 binary value, which are 000, 010 and 100. Then I need to compare them so that I can get the highest position, which is 100, and transmit it serially.
hmm, i feel somewhat in the dark about your intended goal
in multiple instance similar to yours, i have simply created an array of assigned values to slots. they are binary numbers in decimal representation as well as a state. it will effectively work similar to a debounce, pressing any combination will provide you with a unique sum
int values[8] = {1,2,4,8,16,32,64,128};
int state = 0;
int current = 0;
void loop()
{
// reset current
current = 0;
for(int n = 0; n < 8; n++){current += digitalRead(n) == LOW ? values[n] : 0;}
if(state != current)
{
/*
do something here to execute on value change
*/
// set state to current
state = current;
}
}