So I tried to give the SPI a shot, but I just didn't understand (or didn't want to figure out) the translation of pins from the exampled 74hc165 to the CD4021BE. I decided to go with what I could understand. I got it working, with some strange side effects. I wont outline them here, because its rudimentary crap of replacing cheap chips and adding pull downs on the inputs.
Even though I didn't make a solid attempt at the SPI, I still had to translate others examples of the 74HC165 to figure out which pins did what and how to use the the serial data.
The only problem I cant seem to solve is when I press input 7, both 7 and 8 light up. The serial display shows that they are both pressed, but there is no voltage leaving 8's pin and nothing into 8's input on the CD4021BE. I tried multiple things to solve it; editing delays, changing from LSB to MSB, adding resistance, disconnecting 8's lead from the IC completely... still the same result. I changed the IC 4 times, surely I didnt get 5 identically failed ICs.
And sometimes, the input (CD4021BE) seems unstable and gets all out of whack and begins spitting out erroneous serial states. Like pin one would show in the serial display that it was being pressed about 4 times a second, but there was no input to it. And pin 8 coming out of the 74HC595 was blinking the led at the speed of the latch pin. I tried removing the serial monitor to see if that helped, and it did not. Finally changing to another 595 has appeared to solve the issue.
Heres the code
// 74HC595 CD4021BE
// Pin 1 - relay 1 Pin 1 - input 7
// Pin 2 - relay 2 Pin 2 - NC Q6 (pulse behind q7)
// Pin 3 - relay 3 Pin 3 - Q8 data pin
// Pin 4 - relay 4 Pin 4 - input 3
// Pin 5 - relay 5 Pin 5 - input 2
// Pin 6 - relay 6 Pin 6 - input 1
// Pin 7 - relay 7 Pin 7 - input 0
// Pin 8 - Negative (Ground) Pin 8 - Negative (Ground)
// Pin 9 - (Not Used) Serial out Pin 9 - pl latch pin
// Pin 10 - MR Master Reset (+) Pin 10 - cp clock pulse
// Pin 11 - SHCP CLOCK Pin 11 - ds? serial data input (not used)
// Pin 12 - STCP LATCH Pin 12 - Q7 pulsebehind q8
// Pin 13 - OE Output Enable (-) Pin 13 - input 4
// Pin 14 - DS DATA Pin 14 - input 5
// Pin 15 - relay 0 Pin 15 - input 6
// Pin 16 - Vcc Pin 16 - VDD Voltage IN (+)
int latchPin = 10; //Pin connected to latch pin (ST_CP) of 74HC595 PIN 12
int clockPin = 11; //Pin connected to clock pin (SH_CP) of 74HC595 PIN 11
int dataPin = 12; ///Pin connected to Data in (DS) of 74HC595 PIN 14
int loadlatch = 7; // Connects to Latch pin 9 of CD4021BE
int dataIn = 5; // Connects to the Data pin 3 (Q8) of CD4021BE
int clockIn = 6; // Connects to the Clock pin 10 of CD4021BE
void setup()
{ pinMode(latchPin, OUTPUT); //Pin connected to latch pin (ST_CP) of 74HC595 PIN 12
pinMode(dataPin, OUTPUT); //Pin connected to clock pin (SH_CP) of 74HC595 PIN 11
pinMode(clockPin, OUTPUT); ///Pin connected to Data in (DS) of 74HC595 PIN 14
pinMode(loadlatch, OUTPUT); // Connects to Latch pin 9 of CD4021BE
pinMode(clockIn, OUTPUT); // Connects to the Clock pin 10 of CD4021BE
pinMode(dataIn, INPUT); // Connects to the Data pin 3 (Q8) of CD4021BE
}
void loop()
{
digitalWrite(loadlatch,HIGH);
delayMicroseconds(10);
digitalWrite(loadlatch,LOW);
delayMicroseconds(10);
digitalWrite(clockIn,HIGH);
byte incoming = shiftIn(dataIn, clockIn, MSBFIRST);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, incoming );
//1-8
digitalWrite(latchPin, HIGH);
}
Basically where I am now is trying to figure out how to turn the button presses into "latched" or push on/push off style of switch. I have gone through the tutorials for the latched buttons, but I dont know how to integrate that into the inputs of the CD4021BE.
And the sensitivity on the touch panel must be adjusted. I can place my finger 2mm away from the panel and it will activate a button press. I need to figure out how to adjust the sensitivity on the TTP226. The data sheet is poorly translated from a foreign language and its difficult to understand what they mean.
If anyone has any input that could help, I would love to see it!