4051 Analog Mux not working with photocells

It might be a good idea if you try to meassure the actual voltage at the central leg of each of the voltage dividers and change the amout of light that reaches the LDR's

If the variation is actually very small your software might be ok, and there might be a problem with the LDR's or the realtion between the LDr's resistance and the fixed resistance in the voltage dividers.

Can anyone explain whats going on in this code segment right here?
r0 = row & 0x01;
r1 = (row>>1) & 0x01;
r2 = (row>>2) & 0x01;

The "AND ONE" operation ( & 0x01 ) is isolating the least significant bit from all the other bits in the variable. So the variable r0 becomes the least significant bit of the variable row.
By shifting the variable row one place to the left (moving all the bits to the left) the variable r1 becomes the same as the second least significant bit of the variable row.
Guess what r2 is.
:slight_smile:
So this takes a number and splits it up into the bits you need to send out to the different pins / channel select input of the multiplexer.

I would try connecting one of the LDRs to say Analogue in 4 and see if you are seeing a direct reading.

If you are then it's down to the wiring not being what you think it is or a faulty multiplexer.

Well I may be mistaken but this

int  bin [] = { 000, 1, 10, 11, 100, 101, 110, 111}

is decimal not binary so when you do this

r0 = row & 0x01;
  r1 = (row>>1) & 0x01;
  r2 = (row>>2) & 0x01;

you don't get what you want.
You'd better put this

int  bin [] = { 0, 1, 2, 3, 4, 5, 6, 7}

But in fact you can directly use your loop index

r0 = count & 0x01;
  r1 = (count >>1) & 0x01;
  r2 = (count >>2) & 0x01;

Yes you are quite right, but, at the moment he is just looking at input channel zero so that would be the same in decimal or binary. Or in fact any other number base.