Keypad returned key

Hi I'm using the keypad library and example to give and address to a rows and columns setup... Basically I have a bunch of locks and when I turn a key in a lock it gives me a contact. So far I am able to see on the serial monitor witch lock I activated because it shows me for example that the lock that was activated matches the button "1" of a normal keypad.

I would like to know if it's possible to make my code program (or library) return another preset name than the standard keypad key like "1"-"2"-"3"...

I tried to change the designated character in the setup of the program but even if I removed and replaced the character "1" by "apt1" the serial monitor still shows "1" when the contact is made...

Can someone help?

I would like to know if it's possible to make my code program (or library) return another preset name than the standard keypad key like "1"-"2"-"3"...

It is possible to make your program do anything you want. Within reason, anyway. The keypad library does NOT return "1". It returns '1'. Huge difference.

I don't get what you mean Paul.

I'm a nubee! Please help

Mini_ME_997:
I'm a nubee! Please help

The library returns a char which, by definition, can only be a single character, not a string of characters.

http://playground.arduino.cc/Code/Keypad

char keys[rows][cols] = {
 {'1','2','3'},
 {'4','5','6'},
 {'7','8','9'},
 {'#','0','*'}
};

I think all you need to do is change the character here, to letters or other numbers.

I think all you need to do is change the character here, to letters or other numbers.

You could change the values to numbers. It matters not whether you store '1' or 1 in the array.

But, then you'd need array, of strings, and another function to get the string at the position of the value returned by getKey() and return it, instead of the value returned by getKey().

But, why? It is easier to use single byte values for comparison, and then do what you want.

here is some food for thoughts

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'#', '0', '*'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

const char * apartment;
const char * apartmentNames[] = {"apt1", "Condo 4", "penthouse #", "vacant"};

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

void loop() {
  char key = keypad.getKey();
  if (key != NO_KEY) {
    switch (key) {
      case '1':
        apartment = apartmentNames[0];
        break;
      case '4':
        apartment = apartmentNames[1];
        break;
      case '#':
        apartment = apartmentNames[2];
        break;
      default:
        apartment = apartmentNames[3];
        break; // not necessary but feels nicer
    }
    Serial.println(apartment);
  }
}

What I want ultimately is to write to my LCD the address of the lock that been triggered.

I just don't know how can I retrieve a specific button?

Is it like if getkey(1) {do something}?

have you read my code above or the documentation and examples for the keypad library that CrossRoads kindly pointed you at?

char key = keypad.getKey();
  if (key != NO_KEY) {
      // here key is the character from the matrix that has been pressed, like '#' or '7'
  }

Oh nice! So I just say that key 1 == appartement 1 from then? Right?!

Mini_ME_997:
Oh nice! So I just say that key 1 == appartement 1 from then? Right?!

well I'm not sure what you are saying there :slight_smile:

but basically my code above changes the string buffer pointer apartment to something more readable based on the key you pressed

Forgot to mention that I use more than 9 appartement so I need to refer the single character that is allowed to the new designation. So I can't just multiply key by appartement.

No in that case you need to define a way to validate an entry , like 12# 124# for example --> the # will mark the end of the user typing on the keyboard.

You need to have code building up that buffer as the user types in and then have some sort of lookup table where you know that if I typed 12# I meant "apt 636"

another way is also to have a timeout an if I stop typing after say 2 seconds, then whatever I typed is my input

both can be combined

another way is to make it mandatory to type always 3 digits like 012 instead of 12, then you need to listen for 3 digits

Some would say you also need a start key so that if I got it wrong I can start typing again. for example in that case I could do *12# for apt12 and if I type 1312# then the code needs to be able to discard the input as I typed * again.

try to think about what would make it easy for the end user (not for the programmer) and that's what you need to program.
makes sense?

Yes but it's because I used a 6x6 rows and columns that I had to name my "button" letters after the 1-0 numbers. I don't use the "*" and "#". Thats why I was wondering if I could transform the input into a case or something that allow me to take an action from that event.

Thank you by the way you're very helpful I appreciate it!

OK so you have something like this

char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'#', '0', '*'}
};

but as a 6x6 instead or a 3x4, right?

So you have 36 different symbols (characters with single quotes nothing in double quotes, right??).

--> can you show what they are?

it works the same way, just build an input buffer of what the user enters. and then translate that into your apt #

#include <Keypad.h>
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int led_G = 18; //Led droit
const int led_D = 19; //Led gauche

const byte ROWS = 6; //four rows
const byte COLS = 6; //three columns
char keys[ROWS][COLS] = {
{'1','2','3','4','5','6'},
{'7','8','9','0','A','B'},
{'C','D','E','F','G','H'},
{'I','J','K','L','M','N'},
{'O','P','Q','R','S','T'},
{'U','V','W','X','Y','Z'},
};

byte rowPins[ROWS] = {37, 36, 35, 34, 33, 32}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {45, 44, 43, 42, 41, 40}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
byte ledPin = 13;

boolean blink = false;
boolean ledPin_state;

void setup(){
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // Sets the digital pin as output.
pinMode(led_G, OUTPUT); // Sets the digital pin as output.
pinMode(led_D, OUTPUT); // Sets the digital pin as output.
digitalWrite(ledPin, HIGH); // Turn the LED on.
ledPin_state = digitalRead(ledPin); // Store initial LED state. HIGH when LED is on.
keypad.addEventListener(keypadEvent); // Add an event listener for this keypad
}

void loop(){
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(millis()/1000);

char key = keypad.getKey();

if (key) {
Serial.println(key);
led_flash();

}
if (blink){
digitalWrite(ledPin,!digitalRead(ledPin)); // Change the ledPin from Hi2Lo or Lo2Hi.
delay(100);
}
}

void led_flash(){
digitalWrite(led_G, HIGH); // turn the LED on (HIGH is the voltage level)
delay(75); // wait for a second
digitalWrite(led_G, LOW); // turn the LED off by making the voltage LOW
delay(75); // wait for a second
digitalWrite(led_D, HIGH); // turn the LED on (HIGH is the voltage level)
delay(75); // wait for a second
digitalWrite(led_D, LOW); // turn the LED off by making the voltage LOW
delay(75); // wait for a second
digitalWrite(led_G, HIGH); // turn the LED on (HIGH is the voltage level)
delay(75); // wait for a second
digitalWrite(led_G, LOW); // turn the LED off by making the voltage LOW
delay(75); // wait for a second
digitalWrite(led_D, HIGH); // turn the LED on (HIGH is the voltage level)
delay(75); // wait for a second
digitalWrite(led_D, LOW); // turn the LED off by making the voltage LOW
delay(75); // wait for a second
digitalWrite(led_G, HIGH); // turn the LED on (HIGH is the voltage level)
delay(75); // wait for a second
digitalWrite(led_G, LOW); // turn the LED off by making the voltage LOW
delay(75); // wait for a second
digitalWrite(led_D, HIGH); // turn the LED on (HIGH is the voltage level)
delay(75); // wait for a second
digitalWrite(led_D, LOW); // turn the LED off by making the voltage LOW
delay(75); // wait for a second
digitalWrite(led_G, HIGH); // turn the LED on (HIGH is the voltage level)
delay(75); // wait for a second
digitalWrite(led_G, LOW); // turn the LED off by making the voltage LOW
delay(75); // wait for a second
digitalWrite(led_D, HIGH); // turn the LED on (HIGH is the voltage level)
delay(75); // wait for a second
digitalWrite(led_D, LOW); // turn the LED off by making the voltage LOW
delay(75); // wait for a second
digitalWrite(led_G, HIGH); // turn the LED on (HIGH is the voltage level)
delay(75); // wait for a second
digitalWrite(led_G, LOW); // turn the LED off by making the voltage LOW
delay(75); // wait for a second
digitalWrite(led_D, HIGH); // turn the LED on (HIGH is the voltage level)
delay(75); // wait for a second
digitalWrite(led_D, LOW); // turn the LED off by making the voltage LOW
delay(75); // wait for a second
digitalWrite(led_G, HIGH); // turn the LED on (HIGH is the voltage level)
delay(75); // wait for a second
digitalWrite(led_G, LOW); // turn the LED off by making the voltage LOW
delay(75); // wait for a second
digitalWrite(led_D, HIGH); // turn the LED on (HIGH is the voltage level)
delay(75); // wait for a second
digitalWrite(led_D, LOW); // turn the LED off by making the voltage LOW
delay(75); // wait for a second
}

// Taking care of some special events.
void keypadEvent(KeypadEvent key){
switch (keypad.getState()){
case PRESSED:
if (key == '#') {
digitalWrite(ledPin,!digitalRead(ledPin));
ledPin_state = digitalRead(ledPin); // Remember LED state, lit or unlit.
}
break;

case RELEASED:
if (key == '*') {
digitalWrite(ledPin,ledPin_state); // Restore LED state from before it started blinking.
blink = false;
}
break;

case HOLD:
if (key == '*') {
blink = true; // Blink the LED when holding the * key.
}
break;
}

}

void led_flash(){
       digitalWrite(led_G, HIGH);   // turn the LED on (HIGH is the voltage level)
       delay(75);               // wait for a second
       digitalWrite(led_G, LOW);    // turn the LED off by making the voltage LOW
       delay(75);               // wait for a second
       digitalWrite(led_D, HIGH);   // turn the LED on (HIGH is the voltage level)
       delay(75);               // wait for a second
       digitalWrite(led_D, LOW);    // turn the LED off by making the voltage LOW
       delay(75);               // wait for a second
       digitalWrite(led_G, HIGH);   // turn the LED on (HIGH is the voltage level)
       delay(75);               // wait for a second
       digitalWrite(led_G, LOW);    // turn the LED off by making the voltage LOW
       delay(75);               // wait for a second
       digitalWrite(led_D, HIGH);   // turn the LED on (HIGH is the voltage level)
       delay(75);               // wait for a second
       digitalWrite(led_D, LOW);    // turn the LED off by making the voltage LOW
       delay(75);               // wait for a second
       digitalWrite(led_G, HIGH);   // turn the LED on (HIGH is the voltage level)
       delay(75);               // wait for a second
       digitalWrite(led_G, LOW);    // turn the LED off by making the voltage LOW
       delay(75);               // wait for a second
       digitalWrite(led_D, HIGH);   // turn the LED on (HIGH is the voltage level)
       delay(75);               // wait for a second
       digitalWrite(led_D, LOW);    // turn the LED off by making the voltage LOW
       delay(75);               // wait for a second
       digitalWrite(led_G, HIGH);   // turn the LED on (HIGH is the voltage level)
       delay(75);               // wait for a second
       digitalWrite(led_G, LOW);    // turn the LED off by making the voltage LOW
       delay(75);               // wait for a second
       digitalWrite(led_D, HIGH);   // turn the LED on (HIGH is the voltage level)
       delay(75);               // wait for a second
       digitalWrite(led_D, LOW);    // turn the LED off by making the voltage LOW
       delay(75);               // wait for a second
       digitalWrite(led_G, HIGH);   // turn the LED on (HIGH is the voltage level)
       delay(75);               // wait for a second
       digitalWrite(led_G, LOW);    // turn the LED off by making the voltage LOW
       delay(75);               // wait for a second
       digitalWrite(led_D, HIGH);   // turn the LED on (HIGH is the voltage level)
       delay(75);               // wait for a second
       digitalWrite(led_D, LOW);    // turn the LED off by making the voltage LOW
       delay(75);               // wait for a second
       digitalWrite(led_G, HIGH);   // turn the LED on (HIGH is the voltage level)
       delay(75);               // wait for a second
       digitalWrite(led_G, LOW);    // turn the LED off by making the voltage LOW
       delay(75);               // wait for a second
       digitalWrite(led_D, HIGH);   // turn the LED on (HIGH is the voltage level)
       delay(75);               // wait for a second
       digitalWrite(led_D, LOW);    // turn the LED off by making the voltage LOW
       delay(75);               // wait for a second
       }

OUCH

If only there was a way to make code loop for a specified number of times and a way to make comments automatically match the code.

Told you I wasn't too familiar with C code lol!

Told you I wasn't too familiar with C code lol!

So you figured you'd prove your ignorance by putting in a lot of useless comments that are wrong. Hmmm...