Hi.
i'm having a bit of a hard time understanding the code for the 4051 Multiplexer given on the Arduino Playground.
here it is:
/*
* code example for useing a 4051 * analog multiplexer / demultiplexer
* by david c. and tomek n.* for k3 / malm? h?gskola
*
* edited by Ross R.
*/
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++) {
// select the bit
r0 = bitRead(count,0); // use this with arduino 0013 (and newer versions)
r1 = bitRead(count,1); // use this with arduino 0013 (and newer versions)
r2 = bitRead(count,2); // use this with arduino 0013 (and newer versions)
//r0 = count & 0x01; // old version of setting the bits
//r1 = (count>>1) & 0x01; // old version of setting the bits
//r2 = (count>>2) & 0x01; // old version of setting the bits
digitalWrite(2, r0);
digitalWrite(3, r1);
digitalWrite(4, r2);
//Either read or write the multiplexed pin here
}
}
So, here is what i think i understand:
- connecting it to the arduino and the pins payout i think is ok.
- i understand the basic functionality of reading 8 Analog Pins using only 4 pins on the Arduino Board.
- i understand that in order to select which pin of the Multiplexer we want to Read/Write we have to set a different value to r0, r1 and r2. right?
- r0 HIGH + r1 LOW + r2 LOW would select one InputPin ; r0 HIGH + r1 LOW + r2 HIGH another InputPin ; and so forth. Right?
But now here is my question.
If i have, lets say, a button connected to InputPin0 (y0/pin 13 of the 4051. that would be r0 LOW, r1 LOW, r2 LOW ? ).
How can i say something like "if button is pressed, please turn on my beautiful LED".
can i still use something like a normal
int buttonRead = AnalogRead(what would go here?);
if (buttonRead == HIGH) {
digitalWrite(ledPin,HIGH);
}
i would really appreciate any help with understanding this. i feel i am missing how it works and i am hoping you can point me in the right direction.
Thank you!
=)