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.

thanks for your answers!

  • It doesn't sound like you've asked a question. Can you show us what you have tried? – jose can u c Jan 3 '17 at 16:54
  • “Store a number via keypad” is too vague to be an adequate problem description or program specification. Please edit your post and add question details, keypad model and which pins you attach it to, and some code that shows keypad library #include and calls. (To mark code as code, highlight it and press ctrl-k) – James Waldby - jwpat7 Jan 3 '17 at 16:57
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.

| improve this answer | |
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()
| improve this answer | |
  • The return at third line of loop() is unnecessary and is problematic -- it locks out do_other_stuff_like_blinking_leds_or_what() during the whole time a button is held down. – James Waldby - jwpat7 Jan 3 '17 at 20:49
  • @jwpat7 No, just one, as it sets in_press to false and so the next time the in_press && ... is false, thereturn is skipped and so no more locking occures. On the other hand those returns may be avoided, but the structure would be more complicated. And if the process is reading human input (really slow) and manages room temperature (slow as hell), then one or two more cycles thru loop() does not mean real problem. I did choose more simple and straithforward approche (and slightly less efficient), as it looked to me, that the autor is not much skilled, so clarity is better than speed. – gilhad Jan 3 '17 at 21:21
  • Ok, you are correct that do_other_stuff...() gets skipped only once. – James Waldby - jwpat7 Jan 4 '17 at 0:00
  • well there are other returns too, so theoretically you can press/release keys so fast, that the other_stuff is avoided at all - but you cannot do it by hand, you would need some other comp to emulate it. But it is not important here I think. – gilhad Jan 4 '17 at 0:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.