Arduino code for weight sensor - user menu

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?

Whatever you like - I just want to see an idea how to make my program work as I described...

anicicn:
Whatever you like - I just want to see an idea how to make my program work as I described...

That was a hint, for you to post ALL of your code. You failed that test. Let's see how you manage this one.

Post ALL of your code!

/*
  Kolo:
 * LCD RS je digitalni pin 8
 * LCD Enable je digitalni pin 9
 * LCD D4 pin je digitalni pin 4
 * LCD D5 pin je digitalni pin 5
 * LCD D6 pin je digitalni pin 6
 * LCD D7 pin je digitalni pin 7
 * LCD BL pin je digitalni pin 10 backlight control
 * KEY pin je analogni pin 0
 */

#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);

//char msgs[2][16] = {
//  "Kalibracija",
//  "Ispis tezine",               
////  "",
////  "Left Key OK  ",
////  "Select Key OK" 
//};

int adc_key_val[5] = {50, 200, 400, 600, 800 }; //right, up, down, left, select 
int NUM_KEYS = 5; 
int adc_key_in; 
int key = -1;
int oldkey = -1;
int select = -1;
int up = -1;
int down = -1;
double xmin; 
double xmax; 

void setup()
{
  lcd.clear(); 
  lcd.begin(16, 2);
  lcd.setCursor(0,0); 
  lcd.print("Welcome! ");
  delay(3000);
  lcd.clear();
  lcd.print ("measurement");
  lcd.setCursor(0,1);
  lcd.print("of weight");
  delay(3000);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("press");
  lcd.setCursor(0,1);
  lcd.print("select btn");
  }

void loop()
{
  adc_key_in = analogRead(0);      
  key = get_key(adc_key_in);       
  if (key == 4) select = 1;          
 
  if (select == 1) {
    key = get_key(adc_key_in); 
    if (key == 1) up = 1;
    else if (key == 2) down = 1;
  
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("options:");
    lcd.setCursor(0,1);
    lcd.print("measurement");
    
    if (down == 1){
         int select_down = -1;
         int dugme = -1;
         lcd.clear();
         lcd.setCursor(0,0);
         lcd.print("Opcije:");
         lcd.setCursor(0,1);
         lcd.print("calibration 1 kg");
         dugme = get_key(analogRead(0));
         if (dugme == 4) {select_down = 1; selCounter = 2;}
         else if (dugme == 1) down = -1;
         if (select_down == 1 && selCounter == 2){
           int select_d = -1;
           int dgm = -1;
           lcd.clear();
           lcd.setCursor(0,0);
           lcd.print("put 0 kg and then");
           lcd.setCursor(0,1);
           lcd.print("btn select");
           dgm = get_key(analogRead(0));
           if (dgm == 4) {select_d = 1, selCounter = 3;}
           if (select_d == 1 && selCounter == 3){
             xmin = analogRead(2);
             lcd.clear();
             lcd.setCursor(0,0);
             lcd.print("put 5 kg and then");
             lcd.setCursor(0,1);
             lcd.print("btn select");
           }
         }
         
           //if (key1 == 4) calibrate(1); 
         }
    
    
    if (up == 1){
         lcd.clear();
         lcd.setCursor(0,0);
         lcd.print("options:");
         lcd.setCursor(0,1);
         lcd.print("measurement");
         key = get_key(adc_key_in); 
         if (key == 2) up = -1;
    }
    
  }


  delay(100);
}

// Convert ADC value to key number
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;
}

But this code won't work as I described...

  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.