0

I'm stuck with a little problem. I need to create a program that can store a number via keypad, (this number will be chosen by the user) and then do something with it. To be more specific I will create a temperature control in which the user will be able to input a value of temperature they need, and then with this value control a heater, but that's another story. The main point here is that the program needs to be able to store a value via keypad.

I use the keypad library and some of its commands like getKey() and waitForKey() and so on.

Gil Sven
  • 167
  • 8
INME
  • 3
  • 1
  • 2

2 Answers2

2

http://playground.arduino.cc/Code/Keypad#Example here is the setup for you, you should see, what you are pressing.

then try something like this:

int temp=0;
bool in_press=false;
void loop() {
  char key = keypad.getKey();
  if (in_press &&  key == NO_KEY){ in_press=false; return;} // key released
  if ( ! in_press ){ 
    if (key != NO_KEY){ // if key is just pressed - proceed it
      in_press=true;
      if (key == '*') {temp=0; return;} //cancel input
      if (key == "#') { do_something_with(temp); temp=0;return;} //accept input
      // key is '0'..'9' - add it to result
      temp= temp*10 + (key-'0'); // maybe check it temp is not too high ...
      return;
    } 
  } 
  // this pass of loop no new key was pressed, do anything else to do
  // if we want, we can test in_press to see, if key is holded or not
  // or ignore it at all
  do_other_stuff_like_blinking_leds_or_what();
} // end of loop()
gilhad
  • 1,466
  • 2
  • 11
  • 20
1

There are many issue when interfacing a processor to a physical switch. So many that this one question can and has been broken down into several separate questions here on this web site:

  1. How to de-bounce a switch has been asked many times including here.

  2. How to use pull up resistors with a switch has been asked many times including here.

  3. How to scan a switch matrix has been asked many times including here.

Add to that, you need to decide how to parse your data.

  • Will you always expect 2 digits?
  • If so, What will you do if the user enters 3 digits?
  • Will you support a back-space feature?
  • Hows about a clear-entry feature?
  • What about a enter-key feature?

Fortunately many of these questions are addressed in this tutorial.

st2000
  • 7,513
  • 2
  • 13
  • 19