Programming Keypad to Arduino

Hi there! I'm new to Arduino and have run into some problems programming the keypad to my Arduino Uno. :~
The Arduino can't seem to read the signal send from the keypad and I've made several modifications but it can't seem to work. And I really don't understand which part of the program went wrong. :cold_sweat:

The program will only display up to "Enter your gender:" but does not respond when I press 1 or 2 on the keypad.
I've also written another program to test my keypad, and the keypad is not faulty.

Any help would be greatly appreciated! :slight_smile:

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

char gender, height1, height2, height3, height, weight1, weight2, weight3, weight, key;

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns

char keys[ROWS][COLS] = 
{
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {13, 12, 11, 10}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup() 
{                
// Turn the Serial Protocol ON
  Serial.begin(9600);
  Serial.print("Press any key to start! (: \n");
}

void loop()
{
  char key = keypad.getKey();
  if (key != NO_KEY)
  {
    key=0;
    Serial.print("Enter your gender:\n");
    delay(5000);
    
   char gender = keypad.getKey();
    Serial.println(gender);
   if (gender = 1)
    {Serial.print("You are a male\n");}
//    delay(1000);}
    
    else if (gender = 2)
    {Serial.print("You are a female\n"); }
//    delay(1000);}

    else Serial.print("Error. Please enter either 1 or 2. \n");

First of all, this:

   if (gender = 1)

is going to be a problem since it is an assignment into gender, not a test condition. Same for the other if statement. Also, does keypad.getKey() really return an int or does it return a char? My guess is it returns a char, so your tests should be like:

   if (gender == '1')

which tests for the digit character '1', not the int 1. If you need the interger value, which I think is a better approach if getKey() returns a char, try:

gender -= '0';

Then, if getKey() returns a '1', the ASCII code for '1' is 49. Therefore:

gender -= '0';
gender = gender - '0';
gender = 49 - 48;
gender = 1;

which makes it an int, although you have it defined as a char.

Unless you can wait exactly 5 seconds and enter a value, your code is not going get anything. It will check the keypad, see that nothing is pressed and return Error. Please enter either 1 or 2..

What you want to do is prompt your messages then wait for a response. You can make a function and a while loop to see if anything is pressed on the keypad. If you need to enter multiple numbers, then you need to specify a return key like * or #. So say you want to enter a weight, then you will press 160* or 160#. These characters should tell your function to stop collecting key presses and return the value.

"Enter Gender"
getResponse();
"Enter age"
getResponse();
"Enter weight"
getResponse();

// This is just a sample function, but you may want to check to see that the button is NOT held down
// this could cause issues if it is not checked.
int getResponse()
{
char key;
int response = 0;

do
{
key = keypad.Getkey();

if(key)
{
response = (response * 10) + (key - '0');
}
}
while (key != '*');
return response;
}

Thanks for the help! I managed to get the program to work! :smiley: