Accumulate numbers from keypad ONLY when a key is pressed

hi, i´m doing this big project, and i´m currently struggling with this part of the code.

what it should do:
when you press "C", user enters a number from a 4x4 keypad, which can be single or multiple digits.

what happens is that when you press a key, it gets added to the input_password consecutively, and when you press another, then those 2 numbers get added on consecutively (like, i press 2 and the input_password is 222222etc until i press 7, and then it´s 22222222272727272727, and so on)

this is the code:

#include <Keypad.h>

const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 4; //four columns

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}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {6,7,8,9}; //connect to the column pinouts of the keypad

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

String input_password;

void setup(){
  Serial.begin(9600);
  input_password.reserve(32); // maximum input characters is 33, change if needed
}

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

  if (key){
    if(key == 'C') {
      input_password = ""; // clear input password
      Serial.println("Ingrese su ID: ");
      CCgetId();
      Serial.println(input_password);
  }
  }
}

int CCgetId(){
      char key = keypad.getKey();
      String input_password;
      do{
      key = keypad.getKey();
      input_password += key; // append new character to input password string
      Serial.print(input_password);
      } while (key != '#');
}

String input_password = “”;

i´m sorry, i dont understand

the problem is on the CCgetId function

void CCgetId(){
      char key = '\0';
      do{
         key = keypad.getKey();
          if(key != NO_KEY && key != '#'){
            input_password += key;
            Serial.print("*");
          }
      } while (key != '#');
      Serial.println();
}
1 Like

Always initialize local variables.

1 Like

Edited.

input_password make this a character array.

When # is seen, do a string compare to see if PW is correct.

strcmp( )

int CCgetId(){
      char key = '\0';
      do{
         key = keypad.getKey();
          if(key != NO_KEY && key != '#'){
            input_password += key;
            Serial.print("*");
          }
      } while (key != '#');
      Serial.println();
}

It promises to return an int, but doesn't.

1 Like

Yeah, I just copied his function and I didn't notice the return type until I posted it. I don't why he wants to return an int. But regardless, I modified the function again.

Compare to what? I think he function was meant to set a password not check if it's the right one.

At some point what is entered would need to be compared to a value or what’s the point.

You should probably filter out the many times that 'key' is equal to 'NO_KEY'.

int CCgetId()
{
  char key;
  String input_password;

  do 
  {
      key = keypad.getKey();
      if (key != NO_KEY)
      {
        input_password += key; // append new character to input password string
        Serial.println(input_password);
      }
  } while (key != '#');
}
1 Like

@mseddougui
Your code should allow to enter next digit only after releasing the keyboard:

1 Like

thanks to everyone, i ended up solving it on my own, this is what i did:

#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;

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();
    }
  }
}

void CCgetId(){
  String input_password;
  input_password = "";
  char key = keypad.getKey();
  
  do{
    key = keypad.waitForKey();
    input_password+=key;
  }while (key != '#');
  Serial.println(input_password);

}
1 Like

By looking at the OP's original code, there was no indication that the OP wanted to compare the user-inputted password to any other value. There are many different points that could have been intended by the OP.

Defining a password and saving it to a database is one of them.

I appreciate the fact that you gave the OP a way to compare strings to each other, but all I was saying is that there was no value to compare the input_password with.

Cheers

1 Like

Good catch! Never used a keypad before but it's definitely something to watch out for.

Thank you.

1 Like

Point taken, we see that, just offering information for the OP to consider.

2 Likes

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