Serial display problem

Hello~

I’ve encountered some problems having the serial monitor display the first value that I enter on the keypad. I’m trying to get the program to display the value that the user will enter into the keypad the moment it has been entered. However, only the second and third digit are being displayed. The first one keeps showing up as a blank.

I hope someone can help me! Thanks! :slight_smile:

#include <stdio.h> 
#include <Keypad.h>
#include <SimpleTimer.h>

char startrace, stoprace, gender;
int height1, height2, height3, height, weight1, weight2, weight3, weight, age1, age2, age, key, key1, key2;
double BMR, Calories;
unsigned long starttime, endtime;
long timetaken;

const byte ROWS = 4; 
const byte COLS = 3; 

char keys[ROWS][COLS] = 
{ {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'} };
byte rowPins[ROWS] = {13, 12, 11, 10}; 
byte colPins[COLS] = {9, 8, 7};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup() 
{ // Turn the Serial Protocol ON
  Serial.begin(9600);
  keypad.setHoldTime(200);
  keypad.setDebounceTime(200);
  Serial.print("Press any key to start! (: \n");
  key == '0'; }

void loop()
{
  char key = keypad.getKey();
  if (key != NO_KEY)
  
  {Serial.print("\nEnter your gender: ");    
  char key = keypad.waitForKey();           
  Serial.println(key);                      
   {                                        
   if (key == '1')                          
   { gender = key;                          
     Serial.print("You are a male\n");}     
   if (key == '2')                           
   { gender = key;                          
     Serial.print("You are a female\n"); }  
   }                                        
   
   Serial.print("\nEnter your height:\n");    
   key = (keypad.waitForKey()-'0');
   Serial.print(key);
   key1 = (keypad.waitForKey()-'0');          
   Serial.print(key1);
   key2 = (keypad.waitForKey()-'0');          
   Serial.print(key2);
   
      height1 = (key)*100;                    
      height2 = key1*10;                        
      height3 = key2;                         
      height = height1+height2+height3;       
   Serial.print ("\n");   
   Serial.print(height);
   Serial.print("cm.");

   Serial.print("\n\nEnter your weight:\n");      
   key = (keypad.waitForKey()- '0');            
   Serial.print(key);
   key1 = (keypad.waitForKey()- '0');           
   Serial.print(key1);
   
     weight1 = key*10;                          
     weight2 = key1;                            
     weight = weight1 + weight2;                
   Serial.print ("\n");   
   Serial.print(weight);                        
   Serial.print("kg.");
   
   Serial.print("\n\nEnter age:\n");         
   key = (keypad.waitForKey()- '0');         
   Serial.print(key);
   key1 = (keypad.waitForKey()- '0');        
   Serial.print(key1);

     age1 = key*10;                          
     age2 = key;                             
     age = age1 + age2;                      

   Serial.print ("\n");   
   Serial.print(age);                        
    
   Serial.print("\n\nReady to run?\n");         
   delay(1000);                                 
                                                
   Serial.print("Press 1 to start!");           
   startrace = keypad.waitForKey();             
   if (startrace == '1')                        
   { starttime = millis();                      
     Serial.print("\nStart time: ");            
     Serial.print(starttime);}                  
                                                
   Serial.print("\n\nPress 2 to stop");         
   stoprace = keypad.waitForKey();              
   while (stoprace != '2')                      
     {stoprace = key;}                          
                                                
   endtime = millis();                          
   Serial.print("\nStop time: ");               
   Serial.print(endtime);                       
   Serial.print("\n");                          
     timetaken= (endtime - starttime)/1000;     
   Serial.print("\nThe time taken is: ");       
   Serial.print(timetaken);                     
   Serial.print("s.");                          
    
   if (gender=='1')                                           
   { Serial.print("\n\nYou are a male\n");                    
     BMR = ((13.75*weight) + (5*height) - (6.76*age) + 66);   
     Serial.print("BMR value is:");                           
     Serial.print(BMR); }                                     
                                                              
   else if (gender=='2')                                      
   { Serial.print("\nYou are a female\n");                    
     BMR = ((9.56*weight) + (5*height) - (4.68*age) + 655);   
     Serial.print("BMR value:  ");                            
     Serial.print(BMR); }                                     
                                                              
     Calories = (BMR/86400)*8*timetaken;                      
   Serial.print("\nCalories burned:  ");                      
   Serial.print(Calories);                                    
   Serial.print("cal.");                                      
 }
}

Hi AstraeaS I think you have declared two variables called key. The global one is int, and the local one in loop is char. You use the char variable for the first digit of age, and the global int variable key1 for the second digit. The difference between char and int may affect what gets displayed by print. Try using an int variable for the first digit.

Regards Ray

   if (key == '1')                          
   { gender = key;                          
     Serial.print("You are a male\n");}     
   if (key == '2')                           
   { gender = key;                          
     Serial.print("You are a female\n"); }  
   }

Why not just store the value read from the keyboard in gender?

      height1 = (key)*100;                    
      height2 = key1*10;                        
      height3 = key2;

Same here. Just store the values in height1, height2, and height3.
Consistent use of parentheses makes sense, too.

  Serial.println(key);

Anonymous print()s suck. Print a prefix so you know WHAT is being printed!

  {Serial.print("\nEnter your gender: ");

NOTHING goes on the line after the {. Ever!

  Serial.println(key);                      
   {                                        
   if (key == '1')

Useless curly braces suck, too.