Go Down

### Topic: Function for multiplexing (Read 1 time)previous topic - next topic

#### alkopop79

##### Mar 04, 2011, 02:47 pm
Hi,

I've been playing with multiplexers recently and was wondering if there's a way to create a function to read the inputs and print the result. I wrote a sketch that does not work for some reason:

void setup()
{
//4051 digital control pins
pinMode(7, OUTPUT); // s0
pinMode(8, OUTPUT); // s1
pinMode(9, OUTPUT); // s2
Serial.begin(9600);
}
void loop(){
}

digitalWrite=(7,bita); // the three digital pins to control the multiplexer
digitalWrite=(8,bitb);
digitalWrite=(9,bitc);
return p;
Serial.println(p);
}

This one results an error message: assignment of function 'void digitalWrite(uint8_t,uint8_t)'

Any ideas? I will use 32 analog inputs, it would be awesome if I could read all of them with a function. I also wonder how could I make the counting for the select pins easier? So far I have hardcoded the values manually. How could I do it with bit shifting? This ( http://www.arduino.cc/playground/Learning/4051 ) particular example does something similar:

int r0 = 0;      //value of select pin at the 4051 (s0)

int r1 = 0;      //value of select pin at the 4051 (s1)

int r2 = 0;      //value of select pin at the 4051 (s2)

int count = 0;   //which y pin we are selecting

void setup(){

pinMode(2, OUTPUT);    // s0

pinMode(3, OUTPUT);    // s1

pinMode(4, OUTPUT);    // s2

}

void loop () {

for (count=0; count<=7; count++) {

r0 = count & 0x01;

r1 = (count>>1) & 0x01;

r2 = (count>>2) & 0x01;

digitalWrite(2, r0);

digitalWrite(3, r1);

digitalWrite(4, r2);

//Either read or write the multiplexed pin here

}

I need 16 inputs to read instead of 8 like in the example. Any ideas?

#### Benji

#1
##### Mar 04, 2011, 04:30 pm
Quote

there is a typo in there

#2
int bitc:D

#### Benji

#3
##### Mar 04, 2011, 08:15 pm
int bita, not in bita

#### yumnam

#4
##### Mar 08, 2011, 12:17 pm
Another thing is whether this statement digitalWrite=(8,bitb); is syntatically correct? Won't it be digitalWrite(8,bitb); ?

Yumnam Kirani Singh
Tronglaobi Awang Leikai

#5
You have:-