Two 4051 muxers share common pins?

I have a project where I am using two 4051 ICs. One is used to read from (up to) 8 pots (let's call it A). The other 4051 (call it B) will be used for output to LEDs etc. Since I also want to use an LCD (especially for debugging) I am running out of digital pins. One 4051 requires 4 pins (3 for select input/output, 1 to read or write). I then thought I could save the 3 select pins for the B by using the ones already used by A, however the LEDs lit up erroneously. Reading the pot values are ok.
When assigning separate select pins for B it works!

Isn't this supposed to work? What do I need to change to make it work?
The 4051's are different types (CD4051BCN and CD4051BE), does it matter?

Example code based on the code by Tomek Ness on how I want it to work:

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
  for (count=0; count<8; count++) {
    row = bin[count];      
    r0 = row & 0x01;
    r1 = (row>>1) & 0x01;
    r2 = (row>>2) & 0x01;
    digitalWrite(4, r0);
    digitalWrite(5, r1);
    digitalWrite(6, r2);
    switch(count) {
      case 0:
        <<  read from the pot 0 >>
    switch(count) {
      case 0:
        <<  write to digital 0 >>

The code was just a sample, with the reading of the analog pin in the first switch and the writing to digital pin in the second switch. If I combine them i get same result.

Please see this picture (
) of what I have done. Since I am lazy I've only inserted one pot, but there are 7 in my real setup. The LEDs are however only two.

If one or the other LED is on (HIGH), both will glow. Both must be off (LOW) to not glow.
If I connect the orange wires directly to Arduino (pins 8,9,10 for example) and change the code accordingly they light as expected.

If you use a SR for output as Richard suggested it needs 3 lines for control, you can use the same 3 lines you are currently using for the 4051, just don't try to read from the mux while shifting data out to the SR.

unfortunately, it is not true that the shift register will remain inert while you diddle various combinations

Oops, didn't think of the other half of the equation :-[

Below is some previous 4051 discussion. With eight bit parallel multiplex latching chips, one can use common pin select lines and a common data select line. The clock lines need to be individual to each chip.

Thank you for the replies!

The solution with shift registers (SR) seems good, the problem is that I in the very end would like to use PWM with analogue displays (galvanometers); the LEDs were just the first step.
So even if I use SR, don't you think it is possible to make my own kind of PWM by keeping track of time in the main loop?
Have you seen anyone doing this before?

You said that the 4051 is not good for driving LEDs. When I did the 4051 tutorial (Arduino Playground - 4051) I used LEDs and I had similar problems to what I have now. I solved it that time by using delay(), which is not a good solution. And obviously it wasn't a solution at all for this more advanced case.

I have decided to get rid of the LCD (for this project!), so it means I will have 2 pins for serial, 6 PWM pins, and 6 standard pins. 3 of those should drive my demuxers (4051), 3 can be used for the SR and the PWMs for analog to control volt meters. I think this will be a good enough.

But thanks for advicing the 5916, I will consider it in future if necessary!