Pages: [1]   Go Down
Author Topic: Digital Multiplexer and storing and comparing digital inputs  (Read 1169 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8600
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Should I store the readings into an array?
That's how I'd do it, something like this.

Code:
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
« Last Edit: February 21, 2011, 10:46:39 am by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8600
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, Rob. It helped a lot.
Logged

0
Offline Offline
Jr. Member
**
Karma: 1
Posts: 96
Why is it so?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
Logged

Pages: [1]   Go Up
Jump to: