Storing multiple key presses in an array/variable

Hi all.
I’m working on this project where I wan to recharge a line in a GSM module.
I want to be able to enter the pin of the airtime using the keypad and store it in a variable so I can use “AT commands” to recharge this line via USSD.

Right now, I am checking to see if the keys are successfully stored in the variable.
I have been able to get the keys to display on an LCD in succession.
The action to store them in the variable is in the same function but when I try to print out the variable (with key “E”), I just get the key I just pressed which is key E.

I want to also be able to press a key to exit the function (either back to the main loop or wherever) when I have entered the pin completely.

These are my two challenges.
Below is my code.

#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
#include <Keypad.h>
SoftwareSerial mySerial(3, 2); //Rx and Tx respectively

const int rs = A0, en = A1, d4 = A2, d5 = A3, d6 = A4, d7 = A5;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
char rechargepin[20];
short pin = 0, j = 0;


const byte numRows = 4;         //number of rows on the keypad
const byte numCols = 4;         //number of columns on the keypad

//The keymap for the keypad

char keymap[numRows][numCols] =
{
  {'1', '2', '3', '4'},
  {'5', '6', '7', '8'},
  {'9', '0', 'A', 'B'},
  {'C', 'D', 'E', 'F'}
};

char keypressed;

byte rowPins[numRows] = {9, 8, 7, 6}; //Rows 0 to 3 //if you modify your pins you should modify this too
byte colPins[numCols] = {5, 4, 3, 2}; //Columns 0 to 3


Keypad myKeypad = Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);



void setup()
{

  lcd.begin (16, 2);
  Serial.begin(9600);
  Serial.setTimeout(100);

  mySerial.begin(9600); //
  mySerial.setTimeout(100);//

  Serial.println("Starting...");
  delay(1000);
  mySerial.println("AT");//
  delay(100);
  mySerial.println("AT+CMGF=1");//
  delay(100);

  mySerial.println("AT+CNMI=1,2,0,0,0");//
  delay(500);
}


void loop()
{
  keypressed = myKeypad.getKey();

  if (keypressed == 'B') {
    lcd.setCursor(0, 0);
    lcd.print("Enter pin below:");
    recharge();
  }

  if (keypressed == 'E') {
    lcd.clear();
    lcd.setCursor(0, 1);
    lcd.print(rechargepin);
  }

}


void recharge() {
  while (keypressed != 'A') {
    keypressed = myKeypad.getKey();
    if (keypressed != NO_KEY && keypressed != 'A' ) {     //If the char typed isn't A and neither "nothing"
      pin = 0;
      lcd.setCursor(j, 1);                                 //This to write the key pressed on the LCD, whenever a key is pressed it's position is controlled by j
      lcd.print(keypressed);
      rechargepin[pin] = keypressed;
      pin++;
      j++;
    }

  }
}

See How to use the multiple passwords for keypad or Arduino Keypad tutorial

I have no idea what you mean with "recharge" in your description. To me recharge means connect a rechaable battery to a power-supply and let "flow in" electricity into the battery.

or as another example a truck delivered goods to a store.
all goods were put into the store, the truck is empty and drives back to the headquarter putting new goods into the truck the truck gets "re.charged"

But how do you "recharge a line in a GSM-module???"

I want to be able to enter the pin of the airtime using the keypad and store it in a variable so I can use "AT commands" to recharge this line via USSD.

I know what a PIN is. But I have no idea what a "pin of the airtime" means.

I'm pretty good in talking/writing/reading english though it is not my native language. But I don't understand that.
So could you please describe in other words what you want to do.
If english is not your bative language than type yur explanation into google translate and let do google-translate do the translation

https://translate.google.com/?hl&ie=UTF-8&sl=auto&tl=en&op=translate

best regards Stefan

Hello Stefan.
Thank you for replying.
I also am not a native English speaker but let me further simplify.

By LINE, I mean a SIM card inside the module (or the phone number in it).
When you would want to top up your airtime (credit) on the line, you would buy a voucher that has a PIN (a list of numbers) on it and RECHARGE (by entering these numbers in the device) by USSD.

I want to be able to top-up the airtime in this phone number by entering the pin through a keypad, first storing it in say an array and then performing the USSD operation.

Hello @ IoT_Hobbyist

Thanks for linking me to the website. It seems very descriptive and workable.
But I want to be able to print the pin as it is being entered on the LCD.
Here’s what I tried out.

#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
#include <Keypad.h>
SoftwareSerial mySerial(3, 2); //Rx and Tx respectively

const int rs = A0, en = A1, d4 = A2, d5 = A3, d6 = A4, d7 = A5;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
char rechargepin[20];
short pin = 0, j = 0;


const byte numRows = 4;         //number of rows on the keypad
const byte numCols = 4;         //number of columns on the keypad

//The keymap for the keypad

char keymap[numRows][numCols] =
{
  {'1', '2', '3', '4'},
  {'5', '6', '7', '8'},
  {'9', '0', 'A', 'B'},
  {'C', 'D', 'E', 'F'}
};

char keypressed;

byte rowPins[numRows] = {9, 8, 7, 6}; //Rows 0 to 3 //if you modify your pins you should modify this too
byte colPins[numCols] = {5, 4, 3, 2}; //Columns 0 to 3


Keypad myKeypad = Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

String inputString;
long inputInt;

void setup()
{

  inputString.reserve(10); // maximum number of digit for a number is 10, change if needed
  lcd.begin (16, 2);

}


void loop()
{
  keypressed = myKeypad.getKey();

  if (keypressed == 'B') {//
    lcd.setCursor(0, 0);
    lcd.print("Enter pin below:");
    j=0;

    if (keypressed >= '0' && keypressed <= '9') {     // only act on numeric keys
      inputString += keypressed;               // append new character to input string
      lcd.setCursor(j, 1);
      lcd.print(inputString);
      j++;
    }
    else if (keypressed == 'A') {
      if (inputString.length() > 0) {
        inputInt = inputString.toInt(); // YOU GOT AN INTEGER NUMBER
        inputString = "";               // clear input
        // DO YOUR WORK HERE


      }
    } 
    else if (keypressed == 'C') {
      inputString = "";                 // clear input
    }
  }

}