Having trouble with IF statement.

String kbdata="";
            byte menu=2;
char key1 = keypad.getKey();
if ((key1) && (menu==2)){
  lcd.print(key1);
Serial.println(key1);
kbdata.concat(key1);
if (key1 =='#')
{
    Serial.println(kbdata);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Sending Msg");
    delay(200);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Msg Sent");
    delay(200);
    
    
    menu=0;
    main_menu();
}
}
}

What i am trying to do:
I want to get the data from keypad and printing it to the LCD and serial. Also i want my code to store every key i am pressing, until i press # from my keypad. when i press # it simply prints all the keys that i have pressed from my keyboard at Serial, and print LCD with some messages.
The mentioned code is working fine enough but the only problem is that it is not storing every key in the String and simply overwriting the data in it. When i am pressing # key it is showing only the last key i have pressed. may be it is due to IF statement. i need help with this.

Hi surajveer37

Please post your complete program.

If the code you posted is what you have in loop(), it is resetting kbdata to an empty string each time round the loop.

Try this instead

static String kbdata="";

Regards

Ray

#include <Keypad.h>
#include <LiquidCrystalFast.h>
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {8, 7, 4, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {12, 11, 10}; //connect to the column pinouts of the keypad
LiquidCrystalFast lcd(13, 9, 22, 3, 5, 6);
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


byte menu=0;


void main_menu()
{
  lcd.setCursor(0,0);
   lcd.print("1.Inbox 2.New");
   lcd.setCursor(0,1);
   lcd.print("3.Outbox 4.Draft");
}

void setup(){
  Serial.begin(9600);
  Serial1.begin(9600);
   pinMode(13, OUTPUT);
   lcd.begin(16,2);
   main_menu();  

/*
menu=0    main menu
menu=1    inbox
menu=2    new
menu=3    outbox
menu=4    drafts




*/
menu=0;
      }

  
void loop(){
 char key = keypad.getKey();
  
  if ((key) && (menu==0)){
    char a = key;
        lcd.clear();
        lcd.cursor();
        if (a=='2')
        {
            lcd.print("Create Message         ");
            menu=2;
            delay(300);
            lcd.clear();
            
        }
        
        
   
        if(a=='1')
        {
           // Inbox
           lcd.clear();
           lcd.print("inbox");
           menu=1;
        }
        if (a=='3')
        {
          //Outbox
          lcd.clear();
          lcd.print("Outbox");
          menu=3;
        }
        if (a=='4')
        {
          //Draft
          lcd.clear();
          lcd.print("Draft");
          menu=4;
        }
        
        }
        if((key) && (menu !=0))
        {
          char a=key;
          if(a=='*')
          {
            
            menu=0;
            lcd.clear();
            main_menu();
          }
        }
  
    String kbdata="";
    
char key1 = keypad.getKey();
if ((key1) && (menu==2)){
  lcd.print(key1);
Serial.println(key1);
kbdata.concat(key1);
if (key1 =='#')
{
    Serial.println(kbdata);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Sending Msg");
    delay(200);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Msg Sent");
    delay(200);
    
    
    menu=0;
    main_menu();
}
}
}

This is my complete code:
I have first made a Menu
When we will press 2, it takes us to Create Message and menu==2.
Then i want to perform my tasks… i.e. pressing keys from my keypad, print it to LCD and serial and also store the values in String. when i finally press #, It shows all the typed keys at Serial.

@Richards static String kbdata=""; is working fine Thank you so much for your help :) :) It means a lot!

I hope you clicked his karma button....