interfacing with multiple 4021b IC's

greetings,

i’m doing a project wich involves getting user input from a typewriter, and to do so, i’ve followed the tutorial for the shiftin wich is in the main website. i’ve managed to get the data for 5 4021b (i’ll be needing 6, but i haven’t got time to buy the 6th, so i am working with 5) and it all works great, i can get the info i need, yet i get a 1 to 1,5 seconds delay. any method to reduce the delay? or any other way to get what i want without such a noticeable delay?

this is the code i’ve got

int latchPin = 8;
int dataPin = 9;
int clockPin = 7;

byte switchVar1 = 0;
byte switchVar2 = 0;
byte switchVar3 = 0;
byte switchVar4 = 0;
byte switchVar5 = 0;

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

//define pin modes
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);

switchVar3 = shiftIn(dataPin, clockPin);

switchVar4 = shiftIn(dataPin, clockPin);

switchVar5 = shiftIn(dataPin, clockPin);

Serial.println(switchVar1, BIN);
Serial.println(switchVar2, BIN);
Serial.println(switchVar3, BIN);
Serial.println(switchVar4, BIN);
Serial.println(switchVar5, BIN);

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;

}

many thanks

Bruno Gustavo Costa

I don't know if this is ýour problem, but you are using 0.2 as a patameter to delayMicroseconds.

This is probably not a good idea.

Read here : http://www.arduino.cc/en/Reference/DelayMicroseconds

thank you for the quick reply. will try changing that as soon as possible

worked perfectly

cool