Go Down

Topic: Keypad Library... Returning a string after user pressed a certain character (Read 943 times) previous topic - next topic

FallenEvangellic

Jan 17, 2013, 06:40 am Last Edit: Jan 17, 2013, 09:01 am by FallenEvangellic Reason: 1
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: [Select]
/* @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.

LarryD

Do you understand this tutorial?
http://playground.arduino.cc//Main/KeypadTutorial
The way you have it in your schematic isn't the same as how you have it wired up!

CrossRoads

Pointing back to keypad tutorial doesn't help - there is nothing about putting data into arrays in there.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Arrch

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.

FallenEvangellic

#4
Jan 17, 2013, 08:40 am Last Edit: Jan 17, 2013, 08:43 am by FallenEvangellic Reason: 1
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: [Select]
/* @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.


PaulS

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

Code: [Select]
  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.

UKHeliBob

I understand that getKey() is non blocking.  What does it return if no key is pressed ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

PaulS

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.

HazardsMind

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.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

UKHeliBob

This will read keys until either 6 have been entered each followed by Return, or you enter an F followed by Return
Code: [Select]
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.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

FallenEvangellic

#10
Jan 17, 2013, 10:01 pm Last Edit: Jan 17, 2013, 10:03 pm by FallenEvangellic Reason: 1
Thanks for all the replies  :)
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: [Select]
/* @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.


UKHeliBob

You should be able to replace
Code: [Select]
  key = getChar();
with
Code: [Select]
key = customKeypad.getKey();
I only used the getChar() function because I do not have a keypad and needed to use Serial.read()
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

FallenEvangellic

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 :) 

Go Up