HCMatrixkeypad - Matrix keypad library

Useful guides, libraries, and example sketches to support our Arduino based products.
andrew
Site Admin
Posts: 1280
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCMatrixkeypad - Matrix keypad library

Post by andrew » Fri Aug 19, 2022 8:20 am

That would be a bit difficult to do because as this is a generic keypad library (not just for our keypads), it would somehow have to know the key mapping of the actual keypad you're using. The most memory efficient way to do this would be in your sketch as it then can be tailored to your keypad.

Here is an example (I don't have a keypad to hand right now so this is untested) for the 4x4 membrane keypad (HCPROJ0001) we sell for how to convert the row/col value returned by the read() function to the actual value printed on its keys:

Note, the key2val() function may look a bit long winded but as you're only mapping 10 keys it's actually more memory efficient to do it this way then have a lookup table and a loop function...

  1.  
  2. void setup()
  3. {
  4.     Serial.begin(9600);
  5. }
  6.  
  7. /* Main program */
  8. void loop()
  9. {
  10.   Keypad.Scan();
  11.  
  12.   if(Keypad.New_Key())
  13.   {
  14.     byte keyVal = key2val(Keypad.Read());
  15.  
  16.     if(keyVal != 255)
  17.       Serial.println(keyVal);
  18.   }
  19. }
  20.  
  21.  
  22. // Convert row/col index to key value
  23. byte key2val(byte key)
  24. {
  25.   byte val = 255;
  26.  
  27.   if(key == 11)
  28.     return 1;
  29.   if(key == 12)
  30.     return 2;
  31.   if(key == 13)
  32.     return 3;
  33.   if(key == 21)
  34.     return 4;
  35.   if(key == 22)
  36.     return 5;
  37.   if(key == 23)
  38.     return 6;
  39.   if(key == 31)
  40.     return 7;
  41.   if(key == 32)
  42.     return 8;
  43.   if(key == 33)
  44.     return 9;
  45.   if(key == 42)
  46.     return 0;
  47.   return val;
  48. }
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

RetroBoy
Posts: 64
Joined: Sat Feb 26, 2022 11:29 am
Location: U.K.

Re: HCMatrixkeypad - Matrix keypad library

Post by RetroBoy » Fri Aug 19, 2022 3:03 pm

Good Afternoon Andrew,

Thanks for the reply. In fact I had already done a similar thing but could not 'test' as I was awaiting the keypad to arrive. [I think I had mentioned this before, but an 'Age Issue' means I now tend to ask before thinking, so my apologies. ]

I worked using you '.cpp' code that 3 x 4 KP was ( did not need any more ):-

R 1 [ 11 : 12 : 13 ]
R2 [ 21 : 22 : 23 ]
R3 [ 31 : 32 : 33 ]
R4 [ 41 : 42 : 43 ]

Anyway, keypads arrived, code tested, working well. Simply Store the 1st key, wait for the 2nd ( or the '#' ). I'm sure you know how my MATHS is working here. Using '#' as ENTER ( ret = 43 ) - means I know if 0 - 9[1 digit] or 10 - 99[2 digit], and '*' ( ret = 41 ) as SAVE ( Relay settings, Power-Up reload last saved setting ).

Working well.

Thanks a lot, Cheers, S

RetroBoy
Posts: 64
Joined: Sat Feb 26, 2022 11:29 am
Location: U.K.

HCPROJ0002 - Row & Column Pinout

Post by RetroBoy » Tue Sep 13, 2022 6:36 pm

Hi,

I have the Keypad attached as per the Pinout you have described ( 2,3,4,5 - 12,11,10,9 ) working brilliantly on an UNO.
The problem I have is when I Upload to a NANO, it's a NO GO! Half the keys are not recognized, and it is so slow.
I have tried 3 different NANOs just to be sure I didn't have a dudun', but always the same results.

Does the NANO need a different PINOUT, or is there something else I am missing?

Any help on this appreciated.

Regards S.

RetroBoy
Posts: 64
Joined: Sat Feb 26, 2022 11:29 am
Location: U.K.

Re: HCMatrixkeypad - Matrix keypad library

Post by RetroBoy » Tue Sep 13, 2022 9:19 pm

Hi Andrew,

First ... Wiring problem with NANO solved. One of the Dupont Pins was loose in the connector ( I have said before about 'Age' Issues :oops: ).

However ... speed is still an issue. Don't know why it should be so slow running on an NANO when the UNO is there as soon as I enter any value(s).

Regards S.

andrew
Site Admin
Posts: 1280
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCMatrixkeypad - Matrix keypad library

Post by andrew » Wed Sep 14, 2022 7:38 am

Does the NANO need a different PINOUT, or is there something else I am missing?
Nope, both the Nano and the Uno use ATMega328p's running at 16MHz and the keypad library doesn't care what it's running on. I.e. it doesn't alter the way it works based on the board you're using. In fact, a Nano can just be treated as a cut-down Uno in a more breadboard friendly DIP format.


First ... Wiring problem with NANO solved. One of the Dupont Pins was loose in the connector ( I have said before about 'Age' Issues :oops: ).

However ... speed is still an issue. Don't know why it should be so slow running on an NANO when the UNO is there as soon as I enter any value(s).
Although unlikely, I'd first try running the blink sketch (File->examples->01.Basics->Blink) just to sanity check your Nano. Check to see that the LED blinks on for 1 second and off for 1 second and not for example on and off for 2 seconds or more.

By slow, do you mean you press *and* release a key and then there's a delay before you see the value, or do you mean you have to hold a key down for a while before it registers the key press?
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

RetroBoy
Posts: 64
Joined: Sat Feb 26, 2022 11:29 am
Location: U.K.

Re: HCMatrixkeypad - Matrix keypad library

Post by RetroBoy » Wed Sep 14, 2022 11:06 am

Good Morning Andrew,

Thanks for the reply.

The Speed Issue is a General Issue. Everything on the NANO works as it should, Keypad, Display, Relays, PCF8574s; everything Works --- SLOWER when compared to the UNO.

If I switch back to either a UNO or MEGA the Speed Issue disappears. I know the UNO and NANO are 16mHz, I have 4 NANOs and the Speed is the same using any of them. It's not so much the 'Wait for it to change' , more 'Come on, what are you waiting for?'.
I think if I hadn't been checking things with the UNO, I wouldn't have noticed the difference, and would think the 'Speed' was OK.

At the end of the day, it's only switching relays so I can live with it.

Regards S.

( NB: Watch out for a PM, if you get a chance to lookover the code, maybe you will spot something I missed. )

Post Reply

Return to “Arduino”