"While" function not looping

I have function which saves the values I type into a keypad as an integer, thanks PaulS, and then I have another function confirming the value that was typed, because the value is important and error must be mitigated. The problem I have is that the confirm function only works the first time, and gets skipped the second time around the loop. The location of the problem I think: The "while" function in my "Confirm()" function only executes once during the first run through the loop, thereafter it is skipped completely, although the condition I set is to press a keypad button (#) . I have tried and failed to fix it, please help.

void setup(){
  Serial.begin(9600);
  lcd.begin(16,2); 
  lcd.noAutoscroll();
  lcd.print("Arrival Amount:");
  lcd.setCursor(0,1);
  lcd.print("Enter->");
  
  keypad.setDebounceTime(20);
} 



void loop()

{

Serial.println();
Serial.println("Requesting new value");
Saved1 = SavedValue();

Serial.println("New Value: ");
Serial.print(Saved1);


Confirm();
             
Serial.println("Confirmation Successfull");
Saved1 += Saved2 ;
Saved2 = Saved1 ;
Serial.println("Accumulated Value: ");
Serial.print(Saved2);
Saved1 = 0;
Reset();
}



void Reset()
{
        lcd.clear();
        lcd.print("Arrival Amount:");
        lcd.setCursor(0,1);
        lcd.print("Enter->");
        lcd.setCursor(7,1);
}

void Confirm()
{
       
       lcd.clear();
       lcd.print("Confirm Input");
       lcd.setCursor(0,1);
       lcd.print("Amount = ");
       lcd.print(Saved1);
       Serial.println();
       Serial.println("Awaiting Confirmation");
       while(key != '#')
      {
       key = keypad.getKey();
      } 
}      

int SavedValue()
{
  int value = 0;
  char key = keypad.getKey();
  while(key != '#')
        {
          switch(key)
            {
              case NO_KEY:
              break;
        
        case '0': case '1': case '2': case '3': case '4':
        case '5': case '6': case '7': case '8': case '9': 
        
        lcd.print(key);
        value = value * 10 + (key - '0'); 
        screen ++ ;
        if(screen == 10)
        {
        lcd.setCursor(7,1);
        lcd.write("         ");
        lcd.setCursor(7,1);
        screen = 0 ;
        }
        break;
        
      case '*':
        value = 0;
        Reset();
        break; 
        }  
      key = keypad.getKey();
    }
 return value;
 }

Thank you in advance

 while(key != '#')
      {
       key = keypad.getKey();
      }

Well, what happens if key is already == '#'?

Within your confirm function I find while(key != '#') Checking through the rest of this function I find that the variable key is not defined within the confirm function. A quick run through the rest of the code and I find it's not a global variable either. If your code compiles at all you must be using an include file that defines this variable. If you don't give us the complete listing of your code, we're left in the dark about what is going on.

A possible solution is:

key = 0;
while(key != '#')
      {
       key = keypad.getKey();
      }

Now you are sure that key is not currently == '#'. And then you wait until it is.

[quote author=Nick Gammon date=1413959046 link=msg=1930664] A possible solution is:

key = 0;
while(key != '#')
      {
       key = keypad.getKey();
      }

Now you are sure that key is not currently == '#'. And then you wait until it is. [/quote] Maybe even char key = 0; That way there will actually be a variable called key available. If, however, there is a global variable by the name of key, then I'd suggest re-naming the local variables used within the other functions of this sketch.

Thank you Nick, I am starting to grasp coding thanks to you forum masters. Excuse me KenF for not including my global variables, I indeed have a global variable "char key = keypad.getKey()". For some reason I thought the variable "key" will somehow reset, or not remember its last input. :D Quick and subtle fix,

Chris2:
Thank you Nick, I am starting to grasp coding thanks to you forum masters. Excuse me KenF for not including my global variables, I indeed have a global variable “char key = keypad.getKey()”. For some reason I thought the variable “key” will somehow reset, or not remember its last input. :smiley:
Quick and subtle fix,

I thought as much. It was that conflict between some functions having a local variable (key) and others NOT having it. It’s rarely a good idea to have local variables with the same name as a global one. It can lead to real problems later when debugging.