Go Down

Topic: Digital Multiplexer and storing and comparing digital inputs (Read 1 time) previous topic - next topic

Hey everyone,

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.

Graynomad

#1
Feb 21, 2011, 04:44 pm Last Edit: Feb 21, 2011, 04:46 pm by Graynomad Reason: 1
Quote
Should I store the readings into an array?

That's how I'd do it, something like this.

Code: [Select]
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);
}


______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Thanks, Rob.

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.

Thanks.

Graynomad

Just count down from the top until you hit a 0. Something like this...
Code: [Select]
for (int i = 8; i ; i--) {
    if (vals[i-1] == 0) {
         Serial.print (i);
         break;
    }
}   


______
Rob
Rob Gray aka the GRAYnomad www.robgray.com


32teeth

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

Code: [Select]

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;
}
}


my two cents

Go Up