I have succeeded in writing a sketch to drive 3 CD4021's inputting and 2 74LS595's outputting.
The sketch works fine, I am watching the third input on the monitor.
The sketch is below, I discovered a way of lining the outputs up by adding a line
digitalWrite (clockPinin,1);
If I comment the line out the first register input reads 1 bit out, the second register shows the first bit as the last bit of the third register and the third register reads 1 bit out because of it? With it in the signals are all in line?
Can anybody tell me why this is and is this the only way of overcoming the error?
/*
3 shift registers in, 2 out.
Drive 2 banks of 8 LED's with 2 banks of 8 switches (with 1 extra input bank added for future expansion).
The circuit:
Connected to 3 cd 4021's with the inputs via switches to 5v and with 10k resistors to 0v.
Connected to 2 74LS595's with their outputs connected via resistors and LED's to 0v.
Created 09.11.13
By matelot
Modified ------------
By -----------
*/
//connected to 2 cd4021's with the pin 11 on the first connected to pin 3 on the second.
const int latchPinin = 5; //4021 pin 9
const int dataPinin = 6; //4021 pin 3
const int clockPinin = 7; //4021 pin 10
//connected to 2 47LS595's with the pin 9 on the first connected to pin 12 on the second.
const int dataPinout = 8; //595 pin 14
const int latchPinout = 9; //595 pin 11
const int clockPinout = 10; //595 pin 12
int myinput;
int myinput2;
int myinput3;
void setup()
{
Serial.begin(9600);
pinMode(dataPinout, OUTPUT);
pinMode(latchPinout, OUTPUT);
pinMode(clockPinout, OUTPUT);
pinMode(latchPinin, OUTPUT);
pinMode(clockPinin, OUTPUT);
pinMode(dataPinin, INPUT);
}
void loop()
{
delayMicroseconds (20);
digitalWrite (clockPinin,1);//what does this line do?
digitalWrite (latchPinin,1);//set it to 1 to collect parallel data
delayMicroseconds (20);//pause to let digitalWrite finish
digitalWrite (latchPinin,0); //set it to 0 to transmit data serially
delayMicroseconds (20);
myinput = shiftIn (dataPinin, clockPinin, LSBFIRST);//read first register
delayMicroseconds (20);
myinput2 = shiftIn (dataPinin, clockPinin, LSBFIRST);//read second register
delayMicroseconds (20);
myinput3 = shiftIn (dataPinin, clockPinin, LSBFIRST);//read second register
delayMicroseconds (20);
Serial.print (myinput,BIN);
Serial.print (" ");
Serial.print (myinput2,BIN);
Serial.print (" ");
Serial.println(myinput3,BIN);
digitalWrite (latchPinout, LOW);
shiftOut (dataPinout, clockPinout, LSBFIRST, myinput2);//Send data
shiftOut (dataPinout, clockPinout, LSBFIRST, myinput); //Send data
digitalWrite (latchPinout, HIGH);
}