Converting keypad entry/string to integer

hi, i have a function that converts entries from a keypad to string, but i want it to convert that string into an integer, and return its value.

#include <Keypad.h>

const int ROW_NUM=4;
const int COLUMN_NUM=4;

char keys[ROW_NUM][COLUMN_NUM]={
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte pin_rows[ROW_NUM] = {2,3,4,5};
byte pin_column[COLUMN_NUM] = {6,7,8,9};

Keypad keypad = Keypad (makeKeymap(keys), pin_rows, pin_column,ROW_NUM, COLUMN_NUM); 

String input_password;
int numm;

void setup() {
  Serial.begin(9600);
  input_password.reserve(32);
}

void loop() {
  char key = keypad.getKey();

  if(key){
    if (key=='C'){
    Serial.println("Ingrese su ID: ");
    CCgetId();
    Serial.println(CCgetId());
    Serial.println(numm);
    }
  }
}

int CCgetId(){
  String input_password;
  input_password = "";
  char key = keypad.getKey();
  
  while(key != '#'){
    key = keypad.waitForKey();
    if(key!='#'){
    input_password+=key;
    }
  }
  numm = input_password.toInt();
  return numm;
}

see, if i was to put

Serial.println(numm);

right after defining numm inside the function, it´d write it, but the function itself won´t actually return anything to the loop.

no, the function returns, but you do not use returning value.
For fix it, you should call function as

imt password = CCgetId();
1 Like

Maybe try
numm=CCgetId();

For integers you can accumulate the number directly rather than gathering characters and then converting.

int CCgetId()
{
  int input_password = 0;

  while (true) // Repeat forever
  {
    char key = keypad.getKey();

    // If it's a digit, add it to the 'pasword'
    if (key >= '0' && key <= '9')
    {
      input_password *= 10;
      input_password += key - '0';
    }

    // If it's the 'enter' key, return the value
    if (key == '#')
      return input_password;

  } // End: while (true)
}
2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.