hey folks. i've been messing around with the arduino for awhile, but for some reason, i just can't get my 4051 multiplexer working.
i followed the tutorial on the playground, and i've got the digital outputs sending the right values, etc., and i believe i've got it hooked up correctly, but i'm having trouble actually reading the values. i'm pretty sure it's got to be in my coding, but i don't know where.
i have two pots connected right now, on pins y0 and y1 of the 4051. as the arduino cycles through it's loop, i have an if statement that should select for a row value of 0 (all digital pins output 0, which should select pin y0 on the 4051); if row = 0, then it should take a reading from the analog input. I'm sure there are better/more efficient ways of doing this, but I'm just trying to get something that works! it takes a reading, but instead of reading from just the first pot connected to pin y0 on the 4051, it will take readings from either one and add them together.
if both pots are at 0%, it reads 0; if i turn one to 100%, it will read around 500 (out of a possible 1024); subsequently turning the other pot to 100% will then give a reading of 1024. But it works either way, depending on which one you turn first.
What am I doing wrong??? Any ideas? I know it's got to be something stupid in the code, but I can't figure it out.
here is my code:
int pot1 = 0;
int led = 13; //just a led
int r0 = 0; //value select pin at the 4051 (s0)
int r1 = 0; //value select pin at the 4051 (s1)
int r2 = 0; //value select pin at the 4051 (s2)
int row = 0; // storeing the bin code
int count = 0; // just a count
int bin [] = {000, 1, 10, 11, 100, 101, 110, 111};//bin = binär, some times it is so easy
void setup(){
pinMode(2, OUTPUT); // s0
pinMode(3, OUTPUT); // s1
pinMode(4, OUTPUT); // s2
digitalWrite(led, HIGH);
beginSerial(9600);
}
void loop () {
for (count=0; count<=7; count++) {
row = bin[count];
r0 = row & 0x01;
r1 = (row>>1) & 0x01;
r2 = (row>>2) & 0x01;
digitalWrite(2, r0);
digitalWrite(3, r1);
digitalWrite(4, r2);
if (row == 0) {
pot1 = analogRead(0);
Serial.print("row= "); Serial.print(row);Serial.print(" ");
Serial.print("pot1 ="); Serial.print(pot1);
Serial.println("");
delay (1000);
}
}
}
Thanks!!