problem reading multiple inputs with the CD4021B

Hi !
I would need to read at least 16 pushbuttons and send their states to Max/MSP for a project. I used the CD4021B to do so and followed the tutorial here : http://www.arduino.cc/en/Tutorial/ShiftIn
I had some problem on the road but got it working with help of Sebastian Tomczak from The Little Bird Electronics Network forum (thanks again Sebastian :).
However I m still having a major problem… I can now read every buttons but I have a lot of latency between the time I press a button and the time I read it in Max… I don t know if the code I use can be optimized or if it s a problem with the IC, the arduino or the computer… did anyone get the same thing or is using the CD4021B with success ?
heres the code I am using… (graciously provided by Sebastian)

Arduino :
int latchPin = 2;

int dataPin = 4;
int clockPin = 3;

byte switchVar1 = 0;
byte switchVar2 = 0;

void setup() {
Serial.begin(57600);

pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, INPUT);

}

void loop() {
digitalWrite(latchPin,1);

delayMicroseconds(20);
digitalWrite(latchPin,0);
switchVar1 = shiftIn(dataPin, clockPin);

switchVar2 = shiftIn(dataPin, clockPin);
Serial.print(switchVar1);

Serial.print(switchVar2);
delay(10);
}

byte shiftIn(int myDataPin, int myClockPin) {
int i;

int temp = 0;
int pinState;
byte myDataIn = 0;

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

for (i=7; i>=0; i–)

{
digitalWrite(myClockPin, 0);
delayMicroseconds(0.2);

temp = digitalRead(myDataPin);
if (temp) {

pinState = 1;
myDataIn = myDataIn | (1 << i);

}
else {
pinState = 0;
}

digitalWrite(myClockPin, 1);
}
return myDataIn;

}

The max/MSP patch can be found here :
http://www.milkcrate.com.au/_other/downloads/4021%20x%202.zip

thanks for your help!
Phil

How much latency ?

You have a Delay(10) in the main loop that is 10 mS delay + the processing time in the loop and the shiftin function + the time to send serial data. I think that the human ear (brain) will notice a delay when i reaches about 20 mS. This doesen’t leave MAX much time to deal with the data before a “hearable” delay is present.

I don’t know much about MAX or how i processes serial data.

You could try to send the data to the serial monitor and see if you have latency before it shows up there. If there is no latency there, then it’s probably MAX that is slow. This ofcourse requires that the latency is so long that you can actually “see it”

MikMo
http://www.mikmo.dk

Anyway somebody suggested that you could use 4067 CMOS multiplexer as well for digital pushbuttons. Of course you'll need to link the output pin on the 4067 to one DIGITAL input pin on the arduino...
Hope this helps.

hey MikMo
yeah I have tried to play around with the delays in the code but it doesn t change anything ( or anything noticeable anyway..).. I ll do more tests with max but it seems to come from the arduino or the IC at this point...
hey pierlooqup
thansk for the suggestion.. I also have some 4051 that should be ok but I d really like to make it work with the setup I have already started.. anyway maybe if it all turns bad, I might change my choice of multiplexer..
does anyone ever played with the CD4021 ?

You could try to do some timing meassurements by inserting strategically placed Millis() commands in your code.

This coud give you a rough idea of where the time is spent.

i'm having the same problem as you. i have 5 4021b reading 8 pushbuttons each and in the serial monitor i get a delay that's about 1 second to 1,5 seconds. have you figured a solution?