Arduino-4X4 matrix keypad with I/O shift register

2.3k Views Asked by At

I need help. I have done some research and my little understanding of keypad scanning is that the ShiftIn value of Input Column should return zero (0) when a keypad button is pressed. Mine is only returning 255 (or 11111111) in BIN. All I need is to track the zero value when a key is pressed and then scan the keys matrix to display the pressed key. I will appreciate any help. I have added my code and schematic.

enter image description here]1

const int kbdRows = 4;
const int kbdCols = 4;
int LatchIn = 2; //165 pin1
int ClockPin = 3; // 595 pin11 & 165 pin2
int DataIn = 4; //165 pin9
int LatchOut = 5; // 595 pin12
int DataOut = 6; //595 pin14
int led = 7;
int PinState = 0;

char keys[kbdRows][kbdCols] = {
  { '1','2','3','4' },
  { '5','6','7','8' },
  { '9','0','A','B' },
  { 'C','D','E','F' }
};

byte KeyIsDown() {
  int row;
  int col;
  int rowBits;
  int colBits;
  rowBits = 0X10;
  for (row = 0; row < kbdRows; row++) {
    digitalWrite(ClockPin, LOW);
    digitalWrite(LatchOut, LOW);
    shiftOut(DataOut, ClockPin, LSBFIRST, rowBits);
    digitalWrite(LatchOut, HIGH);
    delay(5);
    digitalWrite(ClockPin, HIGH);
    digitalWrite(LatchIn, LOW);
    delay(5);
    digitalWrite(LatchIn, HIGH);
    colBits = shiftIn(DataIn, ClockPin, LSBFIRST);
    for (col = 0; col < kbdCols; col++) {
      if (colBits==0) {
        // not sure what condition to put here
        byte keypressed = keys[kbdRows][kbdCols]; here
        // I know this is the right stuff to return here
      }
      return colBits;
      colBits = colBits >> 1;
    }
    rowBits = rowBits << 1;
  }
}

void setup() {
  pinMode(ClockPin, OUTPUT);
   pinMode(DataOut, OUTPUT);
   pinMode(DataIn, INPUT_PULLUP);
   pinMode(LatchOut, OUTPUT);
   pinMode(LatchIn, OUTPUT);
  digitalWrite(LatchOut, HIGH);
  digitalWrite(LatchIn, HIGH);
  Serial.begin(9600);
  digitalWrite(led, HIGH);
}

void loop() {
  byte retColBit = KeyIsDown();
  Serial.print("ColBit: ");
  Serial.println(retColBit,BIN);
  delay(500);
  PinState = digitalRead(DataOut);
  Serial.print("DataOut: ");
  Serial.println(PinState,BIN);
  delay(500);
}
0

There are 0 best solutions below