Serial monitor just gives back 'K' when serial printing value of an array

Hi. I am working a project which can organize your food in the kitchen. Every time you get some food then You have to key in the expiry date of it every time you get some food and when the expiry date comes it will notify you.

But I’m having a problem that after I key in the numbers and press the hashtag button(enter button), the value it shows is just ‘K’, which I have no idea what went wrong. This problem doesn’t happen when I serial print the ‘product’ variable and only happens when I try to serial print the food array.

Here’s the code:

#include <LiquidCrystal.h>

  #include <Keypad.h>

  //initialize the library with the numbers of the interface pins

  LiquidCrystal lcd(12,11,5,4,3,2);

  //4x4 Matrix key pad

  const byte ROWS = 4; // Four rows

  const byte COLS = 4; // Four columns
  
  int letter = 0; //cursor position

  int productn = 0; //product number

  char product[15]; //value keyed in with keypad

  char food[100]; //food in kitchen

  // Define the Keymap

  char keys[ROWS][COLS] = 

  {

   
  {'1','2','3','A'},

   
  {'4','5','6','B'},

   
  {'7','8','9','C'},

   
  {'*','0','#','D'}

  };

  // Connect keypad ROW0, ROW1, ROW2 and ROW3 to Arduino pins.

  byte rowPins[ROWS] = { 14, 15, 16, 17 };

  // Connect keypad COL0, COL1, COL2 and COL3 to Arduino pins.

  byte colPins[COLS] = { 18, 19, 20, 21 }; 

  // Create the Keypad

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

  void setup() 

  {
  Serial.begin(9600);
    // set up the LCD's number of columns and rows:

   
  lcd.begin(16, 2);

    // Print a message to the LCD.


  }

  void loop() 

  {
  
    char key
  = kpd.getKey();

  if(key)  {
    if(key == '*'){
      letter = 0;
      lcd.clear();
      memset(product, 0, sizeof(product));
    }
    
    if(key == '#'){
      letter = 0;
      lcd.clear();
      food[productn] = product;
      Serial.println(food[productn]);
      memset(product, 0, sizeof(product));
      productn++;
    }
    
    if(key != '*' && key != '#') {
      product[letter] = key;
      lcd.setCursor(letter, 0);
      //Print the detected key
      lcd.print(key);
      letter++;
    }
    }  

  }

I also attached the value shown on the serial monitor.

      food[productn] = product;

Product is an array. What did you mean to do here ?

Oops, sorry. What I wanted to do was to saving the reading from the keypad into an array then copying it to one of the 'food' array. May I know how I can combine and copy all the values inside 'product' into another variable inside an array?

What do you mean, "one of the food array"? It is not a multidimensional array, thus holds only one string value.

Try the following sketch and check that you can print a 5-digit food name on Serial Monitor.

#include <LiquidCrystal.h>
#include <Keypad.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
int letter = 0; //cursor position
int productn = 0; //product number
char product[15]; //value keyed in with keypad
char food[100]; //food in kitchen

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

byte rowPins[ROWS] = { 14, 15, 16, 17 };
byte colPins[COLS] = { 18, 19, 20, 21 };
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{
  Serial.begin(9600);
  lcd.begin(16, 2);
  memset(product, 0, sizeof(product));
}

void loop()
{
  char key = kpd.getKey();
  if (key)
  {
    if (key != '*' && key != '#')
    {
      product[letter] = key;
      lcd.setCursor(letter, 0);
      lcd.print(key);
      letter++;
      if (letter == 5) //five charcaters are entered
      {
        Serial.println(product);
        letter = 0;
      }
    }
  }
}

Hi. I tried it but it only shows the five digit value on the serial monitor. What I want to do is to save the food name into the ‘food’ array, and the serial monitor is just to check if it’s working.

Thanks in advance!

Hi. Please click the link below and answer there. Thanks.

Don't ever do that again or you run the risk of a suspension or a ban. Other moderators may not be as forgiving as me and may have suspended you immediately

If you want to bring a thread to the attention of forum members then simply add an update to it

Try this

#include <Keypad.h>

const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
int charIndex = 0; //cursor position
int productNumber = 0; //product number
char product[15]; //value keyed in with keypad
char foodList[50][15]; //food in kitchen
byte foodCount;

char keys[ROWS][COLS] =
{
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = { 2, 4, 7, 8};
byte colPins[COLS] = { 10, 11, 12, A4};
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

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

void loop()
{
  char key = kpd.getKey();
  if (key)
  {
    if (key == '*')
    {
      charIndex = 0;
      Serial.println("reset");
    }
    else if (key == '#')
    {
      Serial.println();
      charIndex = 0;
      Serial.println();
      strcpy(foodList[productNumber], product);
      printFoods();
      productNumber++;
    }
    else if (key != '*' && key != '#')
    {
      product[charIndex] = key;
      charIndex++;
      product[charIndex] = '\0';
      Serial.print(key);
    }
  }
}

void printFoods()
{
  Serial.println("foods in stock");
  for (int x = 0; x <= productNumber; x++)
  {
    Serial.println(foodList[x]);
  }
}