Have I achieved a null- terminated C-string through the code?

#include <Wire.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header

hd44780_I2Cexp lcd; // declare lcd object: auto locate & config exapander chip

// LCD geometry
const int LCD_COLS = 16;
const int LCD_ROWS = 2;

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {6, 7, 8, 9}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 12, 11}; //connect to the column pinouts of the keypad

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

void setup()
{
  lcd.begin(LCD_COLS, LCD_ROWS);
  lcd.setCursor(3, 0);
  lcd.print("  PRIYA");
  lcd.setCursor(3, 1);
  lcd.print("ELECTRONICS");
  delay(100);
  for (int positionCounter = 0; positionCounter < 40; positionCounter++)
  {
    lcd.scrollDisplayLeft();
    delay(120);
  }
  delay(800);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Set Count:");
}

//unsigned long total;
void loop()
{
  const byte entryMaxSize = 5;
  static char count[entryMaxSize];
  static byte x; // index

  char key = keypad.getKey();

  if (key != NO_KEY) // Check for a valid key.
  {
    switch (key)
    {
      case 'A':
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Set Count:");
        x = 0; // reset the counter
        a = true;
        count[5] = '\0';
        break;

      case 'D':
        if (a) {
          if (x == 0)
          {
            invalidCount();
          }
          else {
            lcd.clear();
            lcd.print("Count Set:");
            for (byte i = 0; i < x; i++) {
              lcd.print(count[i]);
              a = false;
            }
          }
        }
        break;

      default:
        if (a)
        {
          // if not 5 characters yet
          if (x < entryMaxSize)
          {
            // add key to userinput array and increment counter
            if ( key >= '0' && key <= '9' ) // key is of type char and has a value between 0 and 9 so do something with it.
            {
              count[x++] = key;
              lcd.print( key ) ;  // new print statement <<<<<<<<<<<<<<<<
              //total = ((count[ 0 ] - '0') * 10000UL  + (count[ 1 ] - '0') * 1000 + (count[ 2 ] - '0') * 100 + (count[3] - '0') * 10 + (count[4] - '0'));
            }
          }
          else {
            countWarning();
          }
        }
        break;
    }
  }
}

void invalidCount()
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Invalid Count!!");
  lcd.setCursor(0, 1);
  lcd.print("Press A"); // suggesting the user to enter the count again
  a = false;
}

void countWarning()
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("5 Digits Only!!"); // warning for the user if more than permitted digits are entered
  lcd.setCursor(0, 1);
  lcd.print("Press A"); // suggesting the user to enter the count again
  a = false;
}

If corrections are required please suggest them. Thanks.

Gautam

  const byte entryMaxSize = 5;
  static char count[entryMaxSize];

...

        count[5] = '\0';

count[5] is no man's land.

You need to keep track of where you are writing into the count array. Each time you write a new value into the array, put a NULL in the next position.

If you want to store 5 digits in count, count needs to have 6 elements, not 5.

Could anybody make the necessary changes to the code please?

  const byte entryMaxSize = 6;

PaulS:
You need to keep track of where you are writing into the count array. Each time you write a new value into the array, put a NULL in the next position.

If you want to store 5 digits in count, count needs to have 6 elements, not 5.

I guess I am already tracking where I am writing the count, only the Null is missing..

And how do I use the null terminator with my current code?

And how do I use the null terminator with my current code?

If the string is always going to be 5 chars long then do as I suggested and write the '\0' to count[5] as you do now.

If not then write the '\0' to the next array position as you go along. I would suggest doing it that way anyway.

UKHeliBob:
If not then write the '\0' to the next array position as you go along. I would suggest doing it that way anyway.

Okay. How should the code look like then, if a null terminator is to be written as I go along the array?

Write a character into an element of the array, and write \0 into the next element.

Could anyone help me with a piece of code to implement it?

array [index++] = charRead;
array [index] = '\0';

AWOL:

array [index++] = charRead;

array [index] = '\0';

which place in my code should I use this?

GautamD:
which place in my code should I use this?

Where you add each character to the array

Now its allowing me to enter 6 digits instead of 5.

#include <Wire.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header

hd44780_I2Cexp lcd; // declare lcd object: auto locate & config exapander chip

// LCD geometry
const int LCD_COLS = 16;
const int LCD_ROWS = 2;

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {6, 7, 8, 9}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 12, 11}; //connect to the column pinouts of the keypad

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

void setup()
{
  lcd.begin(LCD_COLS, LCD_ROWS);
  lcd.setCursor(3, 0);
  lcd.print("  PRIYA");
  lcd.setCursor(3, 1);
  lcd.print("ELECTRONICS");
  delay(100);
  for (int positionCounter = 0; positionCounter < 40; positionCounter++)
  {
    lcd.scrollDisplayLeft();
    delay(120);
  }
  delay(800);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Set Count:");
}

//unsigned long total;
void loop()
{
  const byte entryMaxSize = 5;
  static char count[entryMaxSize];
  static byte x; // index

  char key = keypad.getKey();

  if (key != NO_KEY) // Check for a valid key.
  {
    switch (key)
    {
      case 'A':
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Set Count:");
        x = 0; // reset the counter
        a = true;
        break;

      case 'D':
        if (a) {
          if (x == 0)
          {
            invalidCount();
          }
          else {
            lcd.clear();
            lcd.print("Count Set:");
            for (byte i = 0; i < x; i++) {
              lcd.print(count[i]);
              a = false;
            }
          }
        }
        break;

      default:
        if (a)
        {
          // if not 5 characters yet
          if (x < entryMaxSize)
          {
            // add key to userinput array and increment counter
            if ( key >= '0' && key <= '9' ) // key is of type char and has a value between 0 and 9 so do something with it.
            {
              count[x++] = key;
              count[x] = '\0';
              lcd.print( key ) ;  // new print statement <<<<<<<<<<<<<<<<
              //total = ((count[ 0 ] - '0') * 10000UL  + (count[ 1 ] - '0') * 1000 + (count[ 2 ] - '0') * 100 + (count[3] - '0') * 10 + (count[4] - '0'));
            }
          }
          else {
            countWarning();
          }
        }
        break;
    }
  }
}

void invalidCount()
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Invalid Count!!");
  lcd.setCursor(0, 1);
  lcd.print("Press A"); // suggesting the user to enter the count again
  a = false;
}

void countWarning()
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("5 Digits Only!!"); // warning for the user if more than permitted digits are entered
  lcd.setCursor(0, 1);
  lcd.print("Press A"); // suggesting the user to enter the count again
  a = false;
}

Change

          if (x < entryMaxSize)

to

          if (x < entryMaxSize - 1)

Did you write this code or copy it from somewhere, not that it would be a bad thing, but you should make some effort to understand how it works if you did.

UKHeliBob:
Did you write this code or copy it from somewhere, not that it would be a bad thing, but you should make some effort to understand how it works if you did.

Part of it I've written it myself and for some part I got help from the guys on the forum.

Latest:

#include <Wire.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header

hd44780_I2Cexp lcd; // declare lcd object: auto locate & config exapander chip

// LCD geometry
const int LCD_COLS = 16;
const int LCD_ROWS = 2;

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {6, 7, 8, 9}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 12, 11}; //connect to the column pinouts of the keypad

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

void setup()
{
  lcd.begin(LCD_COLS, LCD_ROWS);
  lcd.setCursor(3, 0);
  lcd.print("  PRIYA");
  lcd.setCursor(3, 1);
  lcd.print("ELECTRONICS");
  delay(100);
  for (int positionCounter = 0; positionCounter < 40; positionCounter++)
  {
    lcd.scrollDisplayLeft();
    delay(120);
  }
  delay(800);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Set Count:");
}

//unsigned long total;
void loop()
{
  const byte entryMaxSize = 6;
  static char count[entryMaxSize];
  static byte x; // index

  char key = keypad.getKey();

  if (key != NO_KEY) // Check for a valid key.
  {
    switch (key)
    {
      case 'A':
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Set Count:");
        x = 0; // reset the counter
        a = true;
        break;

      case 'D':
        if (a) {
          if (x == 0)
          {
            invalidCount();
          }
          else {
            lcd.clear();
            lcd.print("Count Set:");
            for (byte i = 0; i < x; i++) {
              lcd.print(count[i]);
              a = false;
            }
          }
        }
        break;

      default:
        if (a)
        {
          // if not 5 characters yet
          if (x < entryMaxSize - 1)
          {
            // add key to userinput array and increment counter
            if ( key >= '0' && key <= '9' ) // key is of type char and has a value between 0 and 9 so do something with it.
            {
              count[x++] = key;
              count[x] = '\0';
              lcd.print( key ) ;  // new print statement <<<<<<<<<<<<<<<<
              //total = ((count[ 0 ] - '0') * 10000UL  + (count[ 1 ] - '0') * 1000 + (count[ 2 ] - '0') * 100 + (count[3] - '0') * 10 + (count[4] - '0'));
            }
          }
          else {
            countWarning();
          }
        }
        break;
    }
  }
}

void invalidCount()
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Invalid Count!!");
  lcd.setCursor(0, 1);
  lcd.print("Press A"); // suggesting the user to enter the count again
  a = false;
}

void countWarning()
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("5 Digits Only!!"); // warning for the user if more than permitted digits are entered
  lcd.setCursor(0, 1);
  lcd.print("Press A"); // suggesting the user to enter the count again
  a = false;
}

Now you might consider the declaration of your variables. It will work as it is but could be confusing at a later date.

Perhaps

  const byte entryMaxSize = 5;
  static char count[entryMaxSize + 1];

Then remove the -1 from the test for max entry size later in the code

Latest :

#include <Wire.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header

hd44780_I2Cexp lcd; // declare lcd object: auto locate & config exapander chip

// LCD geometry
const int LCD_COLS = 16;
const int LCD_ROWS = 2;

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {6, 7, 8, 9}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 12, 11}; //connect to the column pinouts of the keypad

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

void setup()
{
  lcd.begin(LCD_COLS, LCD_ROWS);
  lcd.setCursor(3, 0);
  lcd.print("  PRIYA");
  lcd.setCursor(3, 1);
  lcd.print("ELECTRONICS");
  delay(100);
  for (int positionCounter = 0; positionCounter < 40; positionCounter++)
  {
    lcd.scrollDisplayLeft();
    delay(120);
  }
  delay(800);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Set Count:");
}

//unsigned long total;
void loop()
{
  const byte entryMaxSize = 5;
  static char count[entryMaxSize + 1];
  static byte x; // index

  char key = keypad.getKey();

  if (key != NO_KEY) // Check for a valid key.
  {
    switch (key)
    {
      case 'A':
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Set Count:");
        x = 0; // reset the counter
        a = true;
        break;

      case 'D':
        if (a) {
          if (x == 0)
          {
            invalidCount();
          }
          else {
            lcd.clear();
            lcd.print("Count Set:");
            for (byte i = 0; i < x; i++) {
              lcd.print(count[i]);
              a = false;
            }
          }
        }
        break;

      default:
        if (a)
        {
          // if not 5 characters yet
          if (x < entryMaxSize)
          {
            // add key to userinput array and increment counter
            if ( key >= '0' && key <= '9' ) // key is of type char and has a value between 0 and 9 so do something with it.
            {
              count[x++] = key;
              count[x] = '\0';
              lcd.print( key ) ;  // new print statement <<<<<<<<<<<<<<<<
              //total = ((count[ 0 ] - '0') * 10000UL  + (count[ 1 ] - '0') * 1000 + (count[ 2 ] - '0') * 100 + (count[3] - '0') * 10 + (count[4] - '0'));
            }
          }
          else {
            countWarning();
          }
        }
        break;
    }
  }
}

void invalidCount()
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Invalid Count!!");
  lcd.setCursor(0, 1);
  lcd.print("Press A"); // suggesting the user to enter the count again
  a = false;
}

void countWarning()
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("5 Digits Only!!"); // warning for the user if more than permitted digits are entered
  lcd.setCursor(0, 1);
  lcd.print("Press A"); // suggesting the user to enter the count again
  a = false;
}

Am I ready to convert count to an unsigned long value?

Am I ready to convert count to an unsigned long value?

You tell us. Do you have all 5 values? Do you need to have 5 values to do the conversion?