Hello.
I have Arduino Duemilanove and lcd keypad shield DFRobot. I want to make an Arduino project so the user have on display message "Measurement" and when he pushes the key down on lcd display will be new message "Calibration". So the user can move between theese two. When he has on display desired option he presses SELECT button (Calibration for example). And when he is in Calibration menu, again he has options like "Put 0kg on the weight sensor and then press select" (something like that but shorter, i have only 32 characters on my lcd display) and when the user presses select then again he has a message for instance "put the 2kg weight on weight sensor and then press select button" and when he does so then to print a message "Callibration done!"
I have a problem with multiple key presses of the same button (select button in this case). My code for detecting a button is:
int get_key(unsigned int input)
{
int k;
for (k = 0; k < NUM_KEYS; k++) {
if (input < adc_key_val[k])
return k;
}
if (k >= NUM_KEYS) // No valid key pressed
k = -1;
return k;
}
So when I press the select button for the first time it stays selected till the end of the program and then Arduino can't detect another select button press for the further options. Do you know how to write a code for my project?
So when I press the select button for the first time it stays selected till the end of the program and then Arduino can't detect another select button press for the further options. Do you know how to write a code for my project?
Yes, I do. And, if I did write it, should I post a snippet of some other function?
key = get_key(adc_key_in);
if (key == 4) select = 1;
if (select == 1) {
key = get_key(adc_key_in);
Why are you calling get_key() again?
Have you noticed that the function you called just above this snippet was not called analog_read()? There is a reason. Underscores are a pain in the eye. camelCase is preferred.
if (key == 4) select = 1;
And, if key isn't 4?
Functions! You need some functions. loop() is far too long.
You appear to have a fundamental misunderstanding of what get_key() does. It is NOT a blocking function. That is, it does not wait for a key to be pressed. In fact, it doesn't read anything at all. It simply compares the input value to those stored in an array, and returns the appropriate index.
Well yeah, thanks.
But how to make a function that waits for a key to be pressed? I found this function (get_key) on internet.
I know that it compares input value to those in array, and I thought I could make something with that - and that was my problem.
The key is this function that waits for a key to be pressed.
I agree, loop is too big, but I did it like this just to test it, just a quicker way.
So do you have any proposal how to make this function that waits for a key to be pressed?
How do you define that a key has been pressed? What, EXACTLY, constitutes a key press? When you can answer that, it should be fairly obvious how to write a function that contains a while loop that terminates only when that condition occurs.
Hint: It has something to do with the value returned by analogRead() changing significantly.