programming 4x4 button pad, 4th column is not registering hits

I’m using the Teensy 3.0 which uses the arduino software. I have the hardware connected correctly. It has 8 wires connected to a different pin. When I run the serial monitor and hit different buttons I receive a signal from all but the column on the right. If I switch the column wires of column 4 with say 3, the buttons in column 4 work, but they send a signal to the 3rd column and don’t send a signal from the 3rd column. I’ve had a friend look it over and he can’t find a fault in anything so this is a last ditch effort. The hardware all seems to be fine. I have diodes to prevents ghosting and masking at each button. Here is my code:

#define ROWS 4
#define COLS 4

int rowPins[ROWS] = { 23, 22, 21, 20 };
int colPins[COLS] = { 0, 1, 2, 3 };
int keypad_state[ROWS][COLS];

void keypad_init ()
{
  // set rows to HIGH Z
  for (int i = 0; i < ROWS; i++) {
    pinMode (rowPins[i], INPUT);
    digitalWrite (rowPins[i], LOW);
  }

  // set cols to input with pullups
  for (int j = 0; j < COLS; j++) {
    pinMode (colPins[j], INPUT);
    digitalWrite (colPins[j], HIGH);
  }
  
  for (int i = 0; i < ROWS; i++) {
    for (int j = 0; j < COLS; j++) {
      keypad_state[i][j] = 0;
    }
  }
}

void keypad_scan ()
{
  for (int i = 0; i < ROWS; i++) {
    // set row to LOW
    pinMode (rowPins[i], OUTPUT);

    for (int j = 0; j < COLS; j++) {
      int val = digitalRead (colPins[j]);
      keypad_state[i][j] = (val == LOW);
    }

    // set row to High Z
    pinMode (rowPins[i], INPUT);
  }
}

void setup ()
{
  Serial.begin(115200);
  keypad_init ();
}

void loop ()
{
  keypad_scan ();
  
  for (int i = 0; i < ROWS; i++) {
    for (int j = 0; j < COLS; j++) {
      Serial.print (keypad_state[i][j]);
      Serial.print (' ');
    }
    Serial.print ('\n');
  }
  Serial.print ('\n');
  
 delay (100);
}

P.s.

The code works if I made a 3x3 matrix out of my button pad or a 2x2 by changing ROWS and COLS and then changing my I/O pins to correspond. For some reason the jump to a 4x4 causes the 4th column to not work.

Just out of curiosity, why are you not using the Keypad library?

1) I don't understand this: "If I switch the column wires of column 4 with say 3, the buttons in column 4 work, but they send a signal to the 3rd column and don't send a signal from the 3rd column" Your keypad might be broken? Can you test with a tester ?

2) For me your code is correct. I don't know the Teensy 3. I see here the pinout: http://dorkbotpdx.org/blog/paul/teensy_30

Can you change the pins? 0,1,2 are not PWM, 3 can be PWM. I don't think here is the problem, but you can try to change pins used.

Thanks for the keypad recommendation! I didn't know that was there but it's working perfectly for what I want to do.

I've spent a few more hours on my code though and I still can't figure anything out. Maybe I'll come back to it if I feel more ambitious at a later time. If I switched the pins it didn't have any effect and the same problem would occur. I have tested it as well and everything checked out fine.

Thanks for the help guys!

I didn't know that was there but it's working perfectly for what I want to do.

Excellent. I made that suggestion for two reasons, though. One was to get you past the immediate hurdle (my code doesn't work). The other was because libraries for the Arduino are not like libraries for the PC, where all you get is a header file and a dll/lib file. The Arduino "library" is nothing more than a collection of header and source files.

Since you have a working application, that means that you have access to ALL the source code that makes up that application. You can compare how the Keypad.cpp files determines which one key was pressed with how you were trying to determine if one key was pressed.

I'd me looking, I think, at off-by-one issues. If you don't scan the last column or the last row (off by one), then you'd get exactly the behavior you describe.