Saving Keypad inputs to array

Hello,

I am new to arduino, I have a 4 x 4 keypad working well. Now, I need to use the keypad to input 2 digit numbers, say 5 and 2, I want to store these two digits into an array called 'keypressed' which has a size of 2.

Basically, I want to store the 1st input to keypressed[0] and the 2nd input to keypressed[1] and then print out as a two digit number '52' when '#' is being pressed.

I am having difficulty storing the 2nd digit with a for loop, any help? Thanks in advance!

Code is provided:

#include <Keypad.h>
int i=0;
long keypressed[3];
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
{'1','4','7','*'},
{'2','5','8','0'},
{'3','6','9','#'},
{'A','B','C','D'}
};
byte rowPins[ROWS] = {5, 4, 3, 2};
byte colPins[COLS] = {9, 8, 7, 6};

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

void setup(){
Serial.begin(9600);
}

void loop(){
for (i=0;i<3;i++){
if (keypressed != NO_KEY){

keypressed = keypad.getKey();

_ if (keypressed == '#'){_
_ Serial.println(keypressed[0]10+keypressed[1]);_
_
}_
_
} _
_
}*_

}

Please edit your post and add code tags

Type [code] before your code
Type [/code] after your code

That will prevent the italics and possibly other oddities.

If you intend to use keypressed as a string (a NULL terminated array of chars), so you can convert the value to an int, you need a 3 element array.

 for (i=0;i<3;i++){
    if (keypressed != NO_KEY){
   
     keypressed = keypad.getKey();

It hardly makes sense to see if the value is not NO_KEY before you read the value.

Here is some of the code I used to achieve pretty much the same thing

while(keyPress != '#' ){                // test to see if the # key is pressed, if not add more characters                   
          
        keyPress = mykeypad.getKey();     // get new key press
        
        if(keyPress)
        { 
          if(keyPress == '*')              // check if * has been pressed
          {
            keyPress = '.';                // if so replace with .
          }
        
        if(keyPress != 'D' && keyPress != 'A' && keyPress != 'B' && keyPress != 'C' && keyPress != '#')   // check that only a number key has been pressed
        {   
        textNumber += (char)keyPress;      // concatanate new character onto textNumber string
        
        lcd.setCursor(10,2);
        lcd.print(textNumber);
        lcd.print("");
        }
        }
      };

    switch(keyStore) {                        // This switch checks the original key pressed which is stored in keyStore to assign the string textNumber to the right variable
      
      case 'A':
      onDuration = textNumber.toFloat();     // reset the value of onDuration using the string textNumber
      break;
      
      case 'B':
      offDuration = textNumber.toFloat();     // reset the value of offDuration using the string textNumber 
      break;
      
      case 'C':
      numCycles= textNumber.toInt();         // reset the value of offDuration using the string textNumber
      break;
    }

basically each key pressed adds to the string textNumber and the while loop tests to see if a # key is pressed after which the string is converted to the relevant number - using the case switch based on the original key pressed a, B or C. keyStore is used to store the original key press. In this code I use the * key as a decimal point so have a bit of code to replace the keypress if required.

hi...am using keypad to store some alarms in an array of Strings the use LCD and RTC to display time and next Alarm...here is a section of a code am using to input alarms:

void setAlarms() {

while(y<totalAlarms){

while (k<4) { //FOUR characters 0,1,2,3 for HH and MM
char key=keypad_key.getKey();

if (key) //wait for the keyPad to be pressed

{

String myAlarms[]={};
int y=0;

myAlarms[y]+=key; //alarm start from alarm1 HourMinute

lcd.print(key);

k++;
} //end of if(key)loop

key=0;
}

lcd.setCursor(5,1);

lcd.print(myAlarms[y]);

delay(500);

lcd.clear();

k=0;

y++;

}
}

          String myAlarms[]={};

Since you could not be bothered to define how many elements in the array, the compiler counted the initializers and sized the array. There being 0 initializers, you have a completely useless array that can hold ZERO objects.

You appear to have a global variable named y AND a local variable named y. One letter global variable names are a really bad idea. One letter local variable names are not much better, though they are often used when the scope is a short for loop.

The myAlarms array, even if it was properly sized, does NOT contain alarms, so the name is useless.

The delay() call is completely useless.

Aside from these issues, what IS the problem?