Keypad code won't recognize 3rd column

Hi Guys, I'm, still a bit of a noob when it comes to writing code, but i'm trying to learn!

For a project i need to attach a keypad. In the end it will need to change a boolean when i press a certain button, but for the experimenting i'm doing right now, i would be happy if i can serial print some teksts. With the great examples here, its no problem to get that to work.

The sketch below works fine, but for some reason the 3rd column does not work! I'm using the analog pins because i will need all digital pins for other purposes.

I already checked the keypad with a multimeter, and i can not find any issues with column 3. I also tried to connect column 3 to A7, but the problem remains the same.

I have no clue whats wrong. Some help would be very appreciated.

#include <Keypad.h>

const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'A','B','C'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { A0, A1, A2, A3 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { A4, A5, A6 }; 

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

#define ledpin 13

void setup()
{
  pinMode(ledpin,OUTPUT);
  digitalWrite(ledpin, HIGH);
  Serial.begin(9600);
}

void loop()
{
  char key = kpd.getKey();
  if(key)  // Check for a valid key.
  {
    switch (key)
    {
      case '1':
        Serial.println("test");
        break;
      case '2':
        Serial.println("test 2");
        break;
      default:
        Serial.println(key);
    }
  }
}
byte colPins[COLS] = { A4, A5, A6 };

Which board are you using?
A Mega, I hope.

Nope, i'm using a nano. The "problem" is that i already build a PCB that the nano clicks into. May idea to add a keypad i new to my design.

Additionally i'm not sure if the mysensors libary that i'm using will run good good at a mega. As far as i know its only used on nano's and mini pro's.

I'm guessing you are asking about the mega because it has more digital pins, but from what i understand on these forums you can just used analog pins instead.

However, for some reason the 3rd column doesnt work.

On a Nano, A6 and A7 are analogue-only inputs.

ahh.. that explains everything. Thank you very much! I'l guess i'll have to free another digital input then.

While where on the subject of using analog pins as digital pins in combination of with the keypad library i have a additional question.

My project is a mysensors node which consists of quite advanced library that provides a whole network system. The nodes communicate with a nfr24l01 radio. The node is always listening to the network so this is quite a labor intensive task.

The keypad code isnt huge, but i could imagine that the analog pins require much more processing time than the digital pins. Is this true, and is there a risk of slowing down the arduino to much with the use of analog pins?

And if so, do i need to set the pins at startup?

superkris:
but i could imagine that the analog pins require much more processing time than the digital pins

No they don't. Analog is just an extra function but you just use them as digital pins here. So expect for the possibility to do analog, there is nothing analog about it :wink:

Only A6 and A7 (on Uno/Nano/Pro mini like) Arduino's are analog only hence it doesn't work because the library tries to access them as digital.

Great! 1 other question.

are RX(0) and TX(1) a suitable replacement? I understand these can be a bit tricky to use. I really need all pins available, and i'm thinking i'd rather use a analog pin to switch a relay module than one of those tricky RX/TX pins.

Since the pins are open when the keyboard is not used, i should not interfere with the serial communication.

I will only use serial (trough the onboard USB convertor) for debugging during development. Once everything is working, i will not use it anymore. All communication will be done by nfr24l01 radio

If you don't want serial, you can use them. But then you can't debug all the stuff around those pins....