Pages: [1]   Go Down
Author Topic: Keypad Library... Returning a string after user pressed a certain character  (Read 799 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,
I just started with the Keypad library and I think I got the basics. I was able to build my own keypad 3X2
using push button and assigning characters "A-F". What I'm trying to do now is store these characters from
getKey() in an array and return them as a string when user press "F". I have searched the forum for 2 days and haven't really find anything that will help.
Any help will be appreciated.
Thanks for your time.
Code:
/* @file CustomKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates changing the keypad size and key values.
|| #
*/
#include <Keypad.h>

const byte ROWS = 2; //four rows
const byte COLS = 3; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'A','B','C'},
  {'D','E','F'}
 
};
byte rowPins[ROWS] = {9,8 }; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7, 6, 5}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup(){
  Serial.begin(9600);
}
 
void loop(){
  char customKey = customKeypad.getKey();
 
 
  if (customKey){
    Serial.println(customKey);
     }
     
 
}

That's what I have right now.
« Last Edit: January 17, 2013, 03:01:09 am by FallenEvangellic » Logged

Offline Offline
Faraday Member
**
Karma: 67
Posts: 2704
The last thing you did is where you should start looking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do you understand this tutorial?
http://playground.arduino.cc//Main/KeypadTutorial
Logged

The way you have it in your schematic isn't the same as how you have it wired up!

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27144
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Pointing back to keypad tutorial doesn't help - there is nothing about putting data into arrays in there.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3384
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need a char array to store the characters, and an index variable to keep track of where in the array you are. Every time a key is pressed, you put it in the array, increment the index variable, and null terminate the array.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey guys,
and thank you Arrch
Well so far i took matter in my hands and started writing.
I was able to store characters in a buffer and have it return a string when the buffer is full and clearing the buffer. I will now just need to get the butter return when a certain key is pressed.
Suggestions are always welcome.
Thanks again
the code so far
Code:
/* @file CustomKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates changing the keypad size and key values.
|| #
*/
#include <Keypad.h>
char buffer[6];
int i;
const byte ROWS = 2; //four rows
const byte COLS = 3; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'A','B','C'},
  {'D','E','F'}
 
};
byte rowPins[ROWS] = {9,8 }; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7, 6, 5}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup(){
  Serial.begin(9600);
}
 
void loop(){
  while( i < sizeof(buffer))
  {
   
  buffer[i] = customKeypad.getKey();
  if (buffer[i] == '\0')
    break;
    i++;
   if ( i == sizeof(buffer))
    {
      Serial.println(buffer);
      buffer[i]=0;
      Serial.println("-------------");
      break;
    }
   
  }
}
 

Image of serial Monitor is attached.



* Screen Shot 2013-01-17 at 1.36.07 AM.png (49.02 KB, 623x655 - viewed 15 times.)
« Last Edit: January 17, 2013, 02:43:59 am by FallenEvangellic » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  buffer[i] = customKeypad.getKey();
What did you store here if no key was pressed? Why do you want to store that?

Code:
  if (buffer[i] == '\0')
This is a rather poor test. The getKey() function returns a #defined value, NO_KEY, when no key is pressed. Assuming that NO_KEY is equivalent to NULL is a poor assumption.
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4267
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I understand that getKey() is non blocking.  What does it return if no key is pressed ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What does it return if no key is pressed ?
Quote
The getKey() function returns a #defined value, NO_KEY, when no key is pressed.
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 101
Posts: 3651
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Look at this post, http://arduino.cc/forum/index.php/topic,137352.30.html it does work and it compiles. Tweak it for your needs.

There are two codes, the one by dcr_inc will only display the numbers once " * " is pressed, and the one I corrected will display the numbers every time a button is pressed.

Keep in mind that they both use the LCD, so just fix it so that it goes to the serial monitor.
« Last Edit: January 17, 2013, 08:18:35 am by HazardsMind » Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4267
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This will read keys until either 6 have been entered each followed by Return, or you enter an F followed by Return
Code:
char buffer[7];
char newChar;
int i;

#define NO_KEY '!'

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

void loop()
{
  i = 0;
  buffer[0] = '\0';  //terminator in first position just in case
  while (i < sizeof(buffer) - 1)  //leave space for terminator
  {
    newChar = getChar();
    if (newChar == 'F')
    {
      break;
    }
    else if (newChar != NO_KEY)
    {
      buffer[i] = newChar;
      i++;
      buffer[i] =  '\0'; //terminate string in case we finish next time
    }
  }
  Serial.println(buffer);
}

char getChar()
{
  if (Serial.available() == 0 )
  {
    newChar = NO_KEY;
  }
  else
  {
    newChar = Serial.read();
  }
  return newChar;
}

I don't have a keypad so it reads input from the serial port.  Set the monitor to no termination if you try it.

NOTE - the routine as written is not bullet proof and is quite happy to accept F as the first character as well as a string longer than 6 characters from the serial input with no Return after each but it illustrates the concept of terminating input on receipt of a particular character.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for all the replies  smiley
I have spend some time getting info from each one of the messages and have a working code that does what it is meant to do. I took into consideration NO_Key, which return nothing when the user press ' F ' with no inputs in the buffer.
Working code
Code:
/* @file CustomKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates changing the keypad size and key values.
|| #
*/
#include <Keypad.h>
char key;
char buffer[7];
int i;
const byte ROWS = 2; //four rows
const byte COLS = 3; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'A','B','C'},
  {'D','E','F'}
 
};
byte rowPins[ROWS] = {9,8 }; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7, 6, 5}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup(){
  Serial.begin(9600);
}
 
void loop(){
  i = 0;
  buffer[0] = '\0';
  while( i < sizeof(buffer) - 1)
  {
   
  key = getChar();
  if (key == 'F')
     {
    break;
     }
     
   else if ( key != NO_KEY)
    {
      buffer[i] = key;
      i++;
      buffer[i]= '\0';
      }
   
  }
  Serial.println(buffer);
}
 
   
   char getChar()
{
  char key = customKeypad.getKey();
  if (key != NO_KEY)
  {
     return key;
  }
  else {
  return NO_KEY;
   }
  }

Screenshot of Serial Monitor


Image of setup


Thanks again to all the replies.

« Last Edit: January 17, 2013, 04:03:40 pm by FallenEvangellic » Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4267
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You should be able to replace
Code:
  key = getChar();
with
Code:
key = customKeypad.getKey();
I only used the getChar() function because I do not have a keypad and needed to use Serial.read()
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Agreed.
And I kept your function because it is a nicer way to have a separate function to get input and return values instead of having it in the main.
Thanks again for your help smiley 
Logged

Pages: [1]   Go Up
Jump to: