Go Down

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

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.
Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

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.

Go Up