Hi,
I’m using the attached code to detect button presses. 16 buttons are connected to two daisy chained 74HC165. If I press one button at the time, each button gets detected and everything works fine. Also if I press multiple buttons that are connected to the first 74hc165 (the one that is connected to the arduino) I can detect several button presses at once.
But as soon as I press several buttons that are connected to the second 74HC165, the output is wrong. The output circles through different values & shows me presses of buttons that aren’t pressed.
Has anyone an idea why this happens?
Thanks!
//define where your pins are
int latchPin = 44;
int dataPin = 45;
int clockPin = 42;
//Define variables to hold the data
//for shift register.
//starting with a non-zero numbers can help
//troubleshoot
byte switchVar1 = 72; //01001000
byte switchVar2 = 159; //10011111
void setup() {
//start serial
Serial.begin(9600);
//define pin modes
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, INPUT);
}
void loop() {
digitalWrite(latchPin,0);
delayMicroseconds(5);
digitalWrite(latchPin,1);
switchVar1 = shiftIn(dataPin, clockPin);
switchVar2 = shiftIn(dataPin, clockPin);
Serial.println("--------");
Serial.println(switchVar1, BIN); //Buttons attached to first 74HC165
Serial.println(switchVar2, BIN); //Buttons attached to second 74HC165
Serial.println("--------");
for (int n=0; n<=7; n++)
{
if (switchVar1 & (0 << n) ){
//If xy 0 then do something
}
}
//delay so all these print statements can keep up.
delay(250);
}
//------------------------------------------------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(0.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;
}