Adding multiple strings together

Im sure this will be very confusing but hopefully someone can help. I am trying to make a string which value is values entered into a keypad I do not know which keypad it is, I got it from elegoo

I have Programed it to Put numbers on a LCD But I need to Create a 1 string variable and 3 int variable based on the characters entered. My lcd is set up like this,

The code I tried to create this string only gives me 1 Character at a time in the serial monitor. I have made a string called theChar which changed when somethings printed on the LCD to what is being printed, and my code thats a problem is this,

String L1;
String L2;
String L3;
String L4=".";
String L5;
String L6;
String L7;
if(XLCD==7&&YLCD==0)
{
L1= theChar;
}
if(XLCD==8&&YLCD==0)
{
L2= theChar;
}
if(XLCD==9&&YLCD==0)
{
L3= theChar;
}
if(XLCD==11&&YLCD==0)
{
L5= theChar;
}
if(XLCD==12&&YLCD==0)
{
L6= theChar;
}
if(XLCD==13&&YLCD==0)
{
L7= theChar;
}
Length=L2+L3+L4+L5+L6+L7;
Serial.println(Length);

it is probably a very confusing question, Let me know if you need more details or need to see more of my code.
Thank you for your time!

If you need my full code Here it is,


///LCD///
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);

int XLCD;;
int YLCD;;
////

///Keypad///
#include <Keypad.h>

const byte ROWS = 4; 
const byte COLS = 4; 

char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {9, 8, 7, 6}; 
byte colPins[COLS] = {5, 4, 3, 2}; 

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

/////


int Value;
int Value2;
String Length;
String Width;
String Hole;
String Hook;
String theChar;


void setup() 
{
  XLCD=7;
  YLCD=0;
  lcd.init(); 
  lcd.backlight();
   Serial.begin(9600);
  //customKeypad.begin();
  lcd.setCursor(0, 0);
lcd.setCursor(0,0);
lcd.print("Length:");
lcd.setCursor(0,1);
lcd.print("Width:");
lcd.setCursor(0,2);
lcd.print("Hole Size:");
lcd.setCursor(0,3);
lcd.print("Hook:");
lcd.setCursor(XLCD,YLCD);
lcd.blink();
}

void loop() 
{
  char customKey = customKeypad.getKey();
  lcd.setCursor(XLCD,YLCD);
lcd.blink();

  if (customKey == '1')
  {
      //lcd.clear();
      lcd.setCursor(XLCD++,YLCD);
      lcd.print("1");
      Value++;
      theChar="1";
  }
  
  if (customKey == '2')
  {
      //lcd.clear();
      lcd.setCursor(XLCD++,YLCD);
      lcd.print("2");
      Value++;
      theChar="2";
  }
  
  if (customKey == '3')
  {
      //lcd.clear();
      lcd.setCursor(XLCD++,YLCD);
     lcd.print("3");
     Value++;
     theChar="3";
  }
    if (customKey == '4')
  {
      //lcd.clear();
      lcd.setCursor(XLCD++,YLCD);
      lcd.print("4");
      Value++;
      theChar="4";
  }
    if (customKey == '5')
  {
      //lcd.clear();
      lcd.setCursor(XLCD++,YLCD);
      lcd.print("5");
      Value++;
      theChar="5";
  }
    if (customKey == '6')
  {
      //lcd.clear();
      lcd.setCursor(XLCD++,YLCD);
      lcd.print("6");
      Value++;
      theChar="6";
  }
    if (customKey == '7')
  {
      //lcd.clear();
      lcd.setCursor(XLCD++,YLCD);
      lcd.print("7");
      Value++;
      theChar="7";
  }
    if (customKey == '8')
  {
      //lcd.clear();
      lcd.setCursor(XLCD++,YLCD);
      lcd.print("8");
      Value++;
      theChar="8";
  }
    if (customKey == '9')
  {
      //lcd.clear();
      lcd.setCursor(XLCD++,YLCD);
      lcd.print("9");
      Value++;
      theChar="9";
  }
      if (customKey == 'C')
  {
      //lcd.clear();
      lcd.setCursor(XLCD++,YLCD);
      lcd.print("C");
      Value++;
      theChar="C";
  }
      if (customKey == 'D')
  {
      //lcd.clear();
      lcd.setCursor(XLCD++,YLCD);
      lcd.print("D");
      Value++;
      theChar="D";
  }
      if (customKey == '0')
  {
      //lcd.clear();
      lcd.setCursor(XLCD++,YLCD);
      lcd.print("0");
      Value++;
      theChar="0";
  }
      if (customKey == '*')
  {
      //lcd.clear();
      lcd.setCursor(XLCD++,YLCD);
      lcd.print("*");
      Value++;
  }
      if (customKey == '#')
  {
      //lcd.clear();
      lcd.setCursor(XLCD++,YLCD);
      lcd.print("#");
      Value++;
  }
  //if (Value>3&&Value<5)
  if (Value == 3)
  {
    lcd.setCursor(XLCD++,YLCD);
    lcd.print(".");
    Value++;
    
  }




String L1;
String L2;
String L3;
String L4=".";
String L5;
String L6;
String L7;
if(XLCD==7&&YLCD==0)
{
L1= theChar;
}
if(XLCD==8&&YLCD==0)
{
L2= theChar;
}
if(XLCD==9&&YLCD==0)
{
L3= theChar;
}
if(XLCD==11&&YLCD==0)
{
L5= theChar;
}
if(XLCD==12&&YLCD==0)
{
L6= theChar;
}
if(XLCD==13&&YLCD==0)
{
L7= theChar;
}
Length=L2+L3+L4+L5+L6+L7;
Serial.println(Length);







   if (customKey == 'A')
  {
    if (YLCD== 0)
    {
      YLCD=1;
      XLCD = 6;
      lcd.blink();
    }
    else if (YLCD== 1)
    {
      YLCD=2;
      XLCD = 10;
      lcd.blink();
    }
    else if (YLCD== 2)
    {
      YLCD=3;
      XLCD = 5;
      lcd.blink();
    }
    else if (YLCD== 3)
    {
      YLCD=0;
      XLCD=7;
      lcd.blink();
    }
    Value=0;
  }

  
    if (customKey == 'B')
  {
    if (YLCD== 0)
    {
      YLCD=3;
      XLCD = 5;
      lcd.blink();
    }
    else if (YLCD== 1)
    {
      YLCD=0;
      XLCD = 7;
      lcd.blink();
    }
    else if (YLCD== 2)
    {
      YLCD=1;
      XLCD = 6;
      lcd.blink();
    }
    else if (YLCD== 3)
    {
      YLCD=2;
      XLCD=10;
      lcd.blink();
    }
    Value=0;
  }

}

Quite a few years have passed since I used that keypad in a project, but I recall using one of the IDE examples to learn how the keypad worked and how to program using it. Does your copy of the IDE still have keypad sample programs?

This might give you the idea; I wrote the function a little while ago for a 3x4 keypad. It does not use String (capital S).

/*
  read the keypad and build a cString
  Returns:
    true if '#' is entered else false
*/
bool readKeypad()
{
  char ch = customKeypad.getKey();
  static uint8_t index;

  // clear the keypad buffer when indexis zero
  if (index == 0)
  {
    memset(keypadBuffer, '\0', sizeof(keypadBuffer));
  }

  // if a key was pressed
  if (ch)
  {
    // if 'backspace'
    if (ch == '*' && index > 0)
    {
      // remove character
      keypadBuffer[index - 1] = '\0';
      index--;
      // done
      return false;
    }
    // if 'enter'
    if (ch == '#')
    {
      // add terminating '\0' to be sure
      keypadBuffer[index] = '\0';
      // reset index
      index = 0;
      // indicate complete
      return true;
    }

    // if buffer not full
    if (index < sizeof(keypadBuffer) - 1)
    {
      // add to cString
      keypadBuffer[index] = ch;
      index++;
    }
  }
  return false;
}

You will need to add a global variable char keypadBuffer[16];; size depends on your needs, this allows for a maximum of 15 characters.

And in e.g. loop() you can use it like

  if (readKeypad() == true)
  {
    Serial.println(keypadBuffer);
  }

These variables are local to loop() so each time through loop() they get created fresh. Since you set them one at a time, only one will ever be set when you print the string here:

To fix that problem, move the declarations of the strings to above loop() to make them global or add the 'static' keyword to the beginning of each declaration to allow them to keep their value when loop() re-starts.

  static String L1;
  static String L2;
  static String L3;
  static String L4=".";
  static String L5;
  static String L6;
  static String L7;

PROGRAMMING HINT:
When you see almost the same code repeated many times it is a good opportunity to use variables in place of constants. A large portion of your sketch can be reduced to:

  if ((customKey >= '0' && customKey <= '9')
    || customKey == 'C'
    || customKey == 'D'
    || customKey == '*'
    || customKey == '#')
  {
      //lcd.clear();
      lcd.setCursor(XLCD++,YLCD);
      lcd.print(cutomKey);
      Value++;
      theChar = cutomKey;
  }

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