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.
-
It doesn't sound like you've asked a question. Can you show us what you have tried?jose can u c– jose can u c2017年01月03日 16:54:35 +00:00Commented Jan 3, 2017 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– James Waldby - jwpat72017年01月03日 16:57:50 +00:00Commented Jan 3, 2017 at 16:57
2 Answers 2
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:
How to de-bounce a switch has been asked many times including here.
How to use pull up resistors with a switch has been asked many times including here.
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.
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()
-
The
return
at third line ofloop()
is unnecessary and is problematic -- it locks outdo_other_stuff_like_blinking_leds_or_what()
during the whole time a button is held down.James Waldby - jwpat7– James Waldby - jwpat72017年01月03日 20:49:45 +00:00Commented Jan 3, 2017 at 20:49 -
@jwpat7 No, just one, as it sets
in_press
tofalse
and so the next time thein_press && ...
isfalse
, 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– gilhad2017年01月03日 21:21:13 +00:00Commented Jan 3, 2017 at 21:21 -
Ok, you are correct that
do_other_stuff...()
gets skipped only once.James Waldby - jwpat7– James Waldby - jwpat72017年01月04日 00:00:32 +00:00Commented Jan 4, 2017 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– gilhad2017年01月04日 00:51:47 +00:00Commented Jan 4, 2017 at 0:51