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.
/* @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);
}
}
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.
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
/* @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;
}
}
}
What did you store here if no key was pressed? Why do you want to store that?
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.
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.
This will read keys until either 6 have been entered each followed by Return, or you enter an F followed by Return
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.
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
/* @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;
}
}
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