char *pattern[] = {"000", "001", "010", "011", "100", "101", "110", "111", NULL};
char *keys[] = {
  "4", "5", "6", "7", "0", "1", "2", "3",
  "C", "D", "E", "F", "8", "9", "A", "B",
  "GO", "ST", "RS", "SST", "AD", "DA", "PC", "+"};

int last_key = -1;

void setup() {
  Serial.begin(115200);
  Serial.println("Press buttons to see the key value");

  pinMode(12, OUTPUT); // Counter bit 0
  pinMode(13, OUTPUT); // Counter bit 1
  pinMode(14, OUTPUT); // Counter bit 2

  pinMode(25, INPUT_PULLUP); // Row bit 0
  pinMode(26, INPUT_PULLUP); // Row bit 1
  pinMode(27, INPUT_PULLUP); // Row bit 2
}

int getKey(void) {
  int cnt = 0;
  int idx = -1;
  while (pattern[cnt] != NULL) {
    digitalWrite(12, pattern[cnt][2] == '1'?HIGH:LOW);
    digitalWrite(13, pattern[cnt][1] == '1'?HIGH:LOW);
    digitalWrite(14, pattern[cnt][0] == '1'?HIGH:LOW);

    int r0 = digitalRead(25);
    int r1 = digitalRead(26);
    int r2 = digitalRead(27);

    idx = r0 == 0?0:idx;
    idx = r1 == 0?1:idx;
    idx = r2 == 0?2:idx;

    if (idx >= 0) return idx * 8 + cnt;

    cnt++;
   }

  return -1;
}

void loop() {
  int key = getKey();
  if (last_key != key) {
    if (key != -1) {
      Serial.println(keys[key]);
    }
    last_key = key;
  }

  delay(75);
}
74HC138/74HC238Breakout