storing input from keypad to array

Hi! I would like to store the input from keypad to a char array, but I my code wasn't able to do it properly.. Please help me figured out how to do it. Thanks!

char newNum[12]="";
int x=0;

void loop()
{
key = keypad.getKey();
switch (key)
case 3:
      {
          
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Enter number:   "); 
        
        char key = NO_KEY; 
        while (key!='#' && key!='*')
        {
          key = keypad.getKey(); 
     
          if (key!='#' && key!='*')
          {
            newNum[x] = key;
            newNum[x+1]='\0';    
            x++;
          }
        }           
        break;
      }
}
switch (key)
case 3:
      {
          
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Enter number:   "); 
        
        char key = NO_KEY;

Having two variables with the same name (key) with different scope is a recipe for disaster.

            newNum[x] = key;
            newNum[x+1]='\0';    
            x++;

This is correct, if newNum is declared correctly, x is declared correctly, and is initialized at the appropriate time.

Of course, not seeing all of your code makes it difficult to help you, as does not knowing anything more than "it doesn't work". Some description of the expected results and the actual results (how you KNOW it doesn't work) would also be necessary.

here's my sketch..

#include <LiquidCrystal.h>
#include <Keypad.h>
#define ledPin 13

char newNum[12];            //stores the number from keypad input
int x=0;                    //index for newNum array

int setMode = 0;      //determines whether write/read state
int switchMode = 0;   //determines different panels
int i=0;              //Setting the position of lcd cursor
int ledState = 0;

//Keypad
char key;
const byte ROWS = 4; 
const byte COLS = 3; 
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3',},
{'4','5','6',},
{'7','8','9',},
{'*','0','#',}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = {24,34,32,28}; 
byte colPins[COLS] = {26,22,30};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

//LCD
LiquidCrystal lcd(11,10,9,8,7,6,5,4,3,2);

void setup()
{
  lcd.begin(16,2);
  pinMode(ledPin,OUTPUT);
  digitalWrite(ledPin, LOW); 
  lcd.setCursor(0,0);
  lcd.print("Press key 3");
  lcd.setCursor(0,1);
  lcd.clear();
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad
  keypad.setHoldTime(1000); 
  keypad.setDebounceTime(50);
  delay(2000);    
}
void loop()
{
  key = keypad.getKey();
  
  //switch mode:
  if (digitalRead(ledPin)==LOW)
  {
    x=0;
    i=0;
    switch (key)
    {
      //Main Panel
      case '1':
        lcd.clear();      
        switchMode = 1;
        break;      
      //View contacts
      case '2':
        lcd.clear();
        switchMode = 2;
        break;
      //Add new contact
      case '3':
        lcd.clear();
        x=0;
        for (int i=0; i<=11; i++)
        {
          newNum[i]='\0';
        }
        switchMode = 3;
        break;
      //View recipient's number
      case '4':
        lcd.clear();
        switchMode = 4;
        break;
    }
    switch (switchMode)
    {
      //Main Panel
      case 0:
        lcd.setCursor(0,0);
        lcd.print("Temperature: ");
        lcd.setCursor(0,1);
        lcd.print("pH Level: ");
        break;
      //Main Panel
      case 1:        
        lcd.setCursor(0,0);
        lcd.print("Temperature: ");
        lcd.setCursor(0,1);
        lcd.print("pH Level: ");     
        break;
      //View contacts
      case 2:     
        lcd.setCursor(0,0);
        lcd.print("View contacts:  ");
        lcd.setCursor(0,1);
        lcd.print("*-enter/cancel  ");
        break;
      //Add new contact
      case 3:      
        lcd.setCursor(0,0);
        lcd.print("Add new number: ");
        lcd.setCursor(0,1);
        lcd.print("*-enter/cancel  ");
        break;
      //View recipient's number
      case 4:      
        lcd.setCursor(0,0);
        lcd.print("View recipients:");
        lcd.setCursor(0,1);
        lcd.print("*-enter/cancel  ");
        break;
    }    
  }
  //enter certain mode  
  if (digitalRead(ledPin)==HIGH)
  {
    switch (switchMode)
    {     
      //mode3: add new contact
      case 3:
      {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Enter number:   "); 
        
        char key7 = NO_KEY;                           //this part right here up to the bottom is not working 
        while (key7!='#' && key7!='*')                //since it doesn't read input from the keypad because
        {                                                      //if it is, it should be displayed on the LCD.
          key7 = keypad.getKey(); 
          
          if (key7!='#' && key7!='*')
          {
            newNum[x] = key7;
            newNum[x+1]='\0';    
            x++;
       
            if (i>=11)
              i=0;  
          }   
          else if (key7=='*')
          { 
            lcd.clear();      
            switchMode=0;
            state();
          }
          else if (key7=='#')
          {
            x=0;
            i=0;
            setMode = 2;
            lcd.clear();
            lcd.setCursor(0,0);
            lcd.print("new num: ");
            lcd.setCursor(0,1);
            lcd.print(newNum);
          }            
        }
        break;
      }             
    }  
  }
 //Input State
  if (key=='*' && switchMode!=1 && switchMode!=0)
    state();
}
//take care of some special events
void keypadEvent(KeypadEvent eKey)
{ 
  if (digitalRead(ledPin)==HIGH && switchMode==3)
  { 
    switch (keypad.getState()){
    case PRESSED:
      if (eKey!='#' && eKey!='*')
      {
        lcd.setCursor(i,1);
        lcd.print(eKey);
        i++;
      }
    }
  }
}
void state()
{
  x=0;
  i=0;
  ledState = !ledState;
  digitalWrite(ledPin,ledState);
}
  if (digitalRead(ledPin)==LOW)

Why are you reading from an LED connected to an OUTPUT pin? You know what value you last wrote the the pin. Use that, instead.

Some description of the expected results and the actual results (how you KNOW it doesn't work) would also be necessary.

You forgot something.

Why are you reading from an LED connected to an OUTPUT pin? You know what value you last wrote the the pin. Use that, instead.

that's the ledState, am I right?

Some description of the expected results and the actual results (how you KNOW it doesn't work) would also be necessary.

As I uploaded my sketch, after pressing key '3', it would show "Enter number: " but when I try to hit number keys they won't show up on the LCD, even the '#' which suppose to display the value of the array won't work. So I guess the 'while loop' isn't working at all.

As I uploaded my sketch, after pressing key '3', it would show "Enter number: " but when I try to hit number keys they won't show up on the LCD, even the '#' which suppose to display the value of the array won't work. So I guess the 'while loop' isn't working at all.

I know that you've been introduced to the Serial.begin(), Serial.print(), and Serial.println() functions. Why do you persist in not using them?

I know that you've been introduced to the Serial.begin(), Serial.print(), and Serial.println() functions. Why do you persist in not using them?

Because I'm working on a project right now which uses lcd, that's why I don't use serial functions. Btw I can't still figure out how to get my code working. Any help??

You could add some serial debug prints, then remove them once your code is working to your satisfaction.

I am able to do it using 'if', and now just want to try using 'while' but won't work.

//mode3: add new contact
      case 3:
      {

        if (key!=NO_KEY && key!='#' && key!='*')
        {
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("Enter number:   "); 
          
          newNum[x] = key;
          newNum[x+1]='\0';    
          x++;
          if (i>=11)
            i=0;      
        }
        else if (key=='*')
        { 
          lcd.clear();      
          switchMode=0;
          state();
        }
        else if (key=='#')
        {
          x=0;
          i=0;
          setMode = 2;
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("new num: ");
          lcd.setCursor(0,1);
          lcd.print(newNum);
        }
          break;
}

but won't work.

You need to have a stern talk with the Arduino, then. Tell it to shape up, or ship out.

Or add some Serial.print() statements to figure out where what it is doing differs from what you expect it to do.

Only you have your particular hardware and your particular need. Therefore, you are in the best position to debug it.

ok so i think i'm on my own now.. thanks PaulS :smiley:

ok so i think i'm on my own now..

Only until you have some more data to share.