Help with CD4021B Serial Output

Hi, I’m trying to use 12 daisy chained CD4021B shift registers to control 97 buttons. I have 96 connected up to the buttons and one connected directly to the arduino. Right now, I’m having issues with the serial output. Someone told me it might be because of the low clock speed of the shift register, but I don’t know how to change that.

My code:

//define where your pins are
int latchPin = 8;
int dataPin = 9;
int clockPin = 7;

//Define variables to hold the data 
//for each shift register.
//starting with non-zero numbers can help
//troubleshoot
byte switchVar01 = 72;
byte switchVar02 = 72;
byte switchVar03 = 72;
byte switchVar04 = 72;
byte switchVar05 = 72;
byte switchVar06 = 72;
byte switchVar07 = 72;
byte switchVar08 = 72;
byte switchVar09 = 72;
byte switchVar10 = 72;
byte switchVar11 = 72;
byte switchVar12 = 72;

void setup() {
  //start serial
  Serial.begin(9600);

  //define pin modes
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT); 
  pinMode(dataPin, INPUT);

}

void loop() {

  //Pulse the latch pin:
  //set it to 1 to collect parallel data
  digitalWrite(latchPin,1);
  //set it to 1 to collect parallel data, wait
  delayMicroseconds(20);
  //set it to 0 to transmit data serially  
  digitalWrite(latchPin,0);

  //while the shift register is in serial mode
  //collect each shift register into a byte
  //the register attached to the chip comes in first 
  switchVar01 = shiftIn(dataPin, clockPin);
  switchVar02 = shiftIn(dataPin, clockPin);
  switchVar03 = shiftIn(dataPin, clockPin);
  switchVar04 = shiftIn(dataPin, clockPin);
  switchVar05 = shiftIn(dataPin, clockPin);
  switchVar06 = shiftIn(dataPin, clockPin);
  switchVar07 = shiftIn(dataPin, clockPin);
  switchVar08 = shiftIn(dataPin, clockPin);
  switchVar09 = shiftIn(dataPin, clockPin);
  switchVar10 = shiftIn(dataPin, clockPin);
  switchVar11 = shiftIn(dataPin, clockPin);
  switchVar12 = shiftIn(dataPin, clockPin);

  //Print out the results.
  //leading 0's at the top of the byte 
  //(7, 6, 5, etc) will be dropped before 
  //the first pin that has a high input
  //reading
  Serial.println(switchVar01, BIN);
  Serial.println(switchVar02, BIN);
  Serial.println(switchVar03, BIN);
  Serial.println(switchVar04, BIN);
  Serial.println(switchVar05, BIN);
  Serial.println(switchVar06, BIN);
  Serial.println(switchVar07, BIN);
  Serial.println(switchVar08, BIN);
  Serial.println(switchVar09, BIN);
  Serial.println(switchVar10, BIN);
  Serial.println(switchVar11, BIN);
  Serial.println(switchVar12, BIN);

Serial.println("-------------------");
delay(500);
}

//------------------------------------------------end main loop

////// ----------------------------------------shiftIn function
///// just needs the location of the data pin and the clock pin
///// it returns a byte with each bit in the byte corresponding
///// to a pin on the shift register. leftBit 7 = Pin 7 / Bit 0= Pin 0

byte shiftIn(int myDataPin, int myClockPin) { 
  int i;
  int temp = 0;
  int pinState;
  byte myDataIn = 0;

  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, INPUT);

//we will be holding the clock pin high 8 times (0,..,7) at the
//end of each time through the for loop

//at the begining of each loop when we set the clock low, it will
//be doing the necessary low to high drop to cause the shift
//register's DataPin to change state based on the value
//of the next bit in its serial information flow.
//The register transmits the information about the pins from pin 7 to pin 0
//so that is why our function counts down
  for (i=7; i>=0; i--)
  {
    digitalWrite(myClockPin, 0);
    delayMicroseconds(2);
    temp = digitalRead(myDataPin);
    if (temp) {
      pinState = 1;
      //set the bit to 0 no matter what
      myDataIn = myDataIn | (1 << i);
    }
    else {
      //turn it off -- only necessary for debuging
     //print statement since myDataIn starts as 0
      pinState = 0;
    }

    //Debuging print statements
    //Serial.print(pinState);
    //Serial.print("     ");
    //Serial.println (dataIn, BIN);

    digitalWrite(myClockPin, 1);

  }
  //debuging print statements whitespace
  //Serial.println();
  //Serial.println(myDataIn, BIN);
  return myDataIn;
}

I am currently using an Arduino UNO and following this tutorial: http://arduino.cc/en/Tutorial/ShiftIn.

The outputs are only correct for the first print on the serial monitor, and only if I press buttons in a certain range (I think the first 50ish). Also, I dropped all of the leading 0s.

The other outputs are: when no buttons are pressed

0
0
0
0
0
0
0
0
11111111
11111111
11111111
11111111

when the byte is 0***0000, where any, but not necessarily all, of the *s are 1s, it varies between

1110000
1110000
1110000
1110000
1110000
1110000
0
0
0
0
0
0

and

1110000
1110000
1110000
1110000
1110000
1110000
0
0
0
1110000
11100001
11000011

and

1110000
1110000
1110000
1110000
1110000
1110000
0
0
1110000
11110001
11110011
11110111

The rest of the outputs are kinda weird, and i don’t know when they happen specifically

1110000
1110000
1110000
1110000
1110000
1110000
11111111
11111111
11111111
11111111
11111111
11111111
0
0
0
0
0
0
0
10000
11111111
11111111
11111111
11111111

When I press too many of the early range buttons, I get all 1s, when I press too many of the late range buttons, i get all 0s. Combination gets me 0s.

Thank you for taking the time to look at my issue, and this is my first post here, so please let me know if I do anything wrong in terms of etiquette.

Post your code.
And before you do that, find out what "code tags" are by reading the "How to use this Forum" sticky.
OK?

Whoops, thank you. Also, is it incorrect to use code brackets for the serial outputs? If so, what should I use instead?

You could use the "teletype" button instead and it will mono-space your selected text.
On the tutorial link you provided, they neglected to show any decoupling capacitors (big mistake).

dlloyd:
On the tutorial link you provided, they neglected to show any decoupling capacitors (big mistake).

can you tell me please or somebody else where i have to place the capacitors

Connect a 0.1uF cap from each chip's VCC pin to gnd, as close to the Vcc pin as you can.

i add the missing capacitors on this picture
have i placed these capacitors right?