Previous Data is Appending to LCD Line

Hi all,

I'm trying to do something that I thought would be straightforward, and I can't sort out what the issue is. I have a Uno with an LCD and a keypad connected. I have a simple bit of code that is asking the user for entries on two screens. The first screen displays fine and works as expected when the user inputs via the keypad (the '#' character is replaced with the number they input). On the second screen, the initial showing of the screen has the data appended to the second line when it is displayed, which is not what I want (I want the second line to just show "$$$$.$$" as the initial display). Video of the behaviour attached. I've tried lcd.clear(), overwriting the line with spaces, neither of those changed the behavior. Is this an LCD display thing or is my brownmass variable not correctly initialized or something? What am I missing? Thanks for the help.

To clarify:

First screen should show:

Enter Green Mass
####.##

It does, all good. Say you enter 1234.56, no problem. The second screen should then show this:

Enter Brown Mass
$$$$.$$

It doesn't. Instead, it shows this:

Enter Brown Mass
$$$$.$$1234.56

// include the library code:
#include <LiquidCrystal.h>
#include <Keypad.h>

const byte maxdigits = 7;

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 10, 11, 12, 13);

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','e'}
};
byte rowPins[ROWS] = {1, 2, 3, 4}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 6, 7}; //connect to the column pinouts of the keypad

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

char greenmass[maxdigits] = {'#','#','#','#','.','#','#'};
char brownmass[maxdigits] = {'

IMG_1008.mpg (1.65 MB),'


[IMG_1008.mpg|attachment](upload://zwo7TYe8kIA3iHYb8W00YQsuELk.mpg) (1.65 MB),'

IMG_1008.mpg (1.65 MB),'


[IMG_1008.mpg|attachment](upload://zwo7TYe8kIA3iHYb8W00YQsuELk.mpg) (1.65 MB),'.','

IMG_1008.mpg (1.65 MB),'


[IMG_1008.mpg|attachment](upload://zwo7TYe8kIA3iHYb8W00YQsuELk.mpg) (1.65 MB)};
byte input_count = 0;

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);

}

void loop() {
  
  lcd.setCursor(0,0);
  lcd.print("Enter Green Mass");
  lcd.setCursor(0,1);
  lcd.print(greenmass);

  while (input_count < maxdigits) {
    char key = keypad.getKey();
    if (key){
      greenmass[input_count] = key;
      lcd.setCursor(input_count, 1);
      lcd.print(greenmass[input_count]);
      input_count++;
    }
  }

  delay(500);
  input_count = 0;

  lcd.setCursor(0,1);
  lcd.print("                ");
  delay(2000);
  
  lcd.setCursor(0,0);
  lcd.print("Enter Brown Mass");
  lcd.setCursor(0,1);
  lcd.print(brownmass);

  while (input_count < maxdigits) {
    char key = keypad.getKey();
    if (key){
      brownmass[input_count] = key;
      lcd.setCursor(input_count, 1);
      lcd.print(brownmass[input_count]);
      input_count++;
    }
  }

  input_count = 0;
  
}

IMG_1008.mpg (1.65 MB)

One thing I notice straight away is that there are no zero terminators on either of the mass variables.

In c from which Arduino script language is derived, character arrays are generally zero terminated. Try making the mass variables one character longer and add a 0x00 char at the end. I think that will help.

I tried growing the char variables to one digit larger, i.e. from this:

char greenmass[7] = {'#','#','#','#','.','#','#'};
char brownmass[7] = {'

to this:

char greenmass[8] = {'#','#','#','#','.','#','#'};
char brownmass[8] = {'

and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'


to this:

§DISCOURSE_HOISTED_CODE_1§


and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'

to this:

§_DISCOURSE_HOISTED_CODE_1_§

and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'


to this:

§DISCOURSE_HOISTED_CODE_1§


and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'.','

to this:

§_DISCOURSE_HOISTED_CODE_1_§

and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'


to this:

§DISCOURSE_HOISTED_CODE_1§


and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?};

to this:

§_DISCOURSE_HOISTED_CODE_1_§

and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'


and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'

to this:

§_DISCOURSE_HOISTED_CODE_1_§

and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'


to this:

§DISCOURSE_HOISTED_CODE_1§


and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'

to this:

§_DISCOURSE_HOISTED_CODE_1_§

and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'.','


to this:

§DISCOURSE_HOISTED_CODE_1§


and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'

to this:

§_DISCOURSE_HOISTED_CODE_1_§

and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?};


to this:

§DISCOURSE_HOISTED_CODE_1§


and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'

and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'


to this:

§DISCOURSE_HOISTED_CODE_1§


and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'

to this:

§_DISCOURSE_HOISTED_CODE_1_§

and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'


to this:

§DISCOURSE_HOISTED_CODE_1§


and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'.','

to this:

§_DISCOURSE_HOISTED_CODE_1_§

and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'


to this:

§DISCOURSE_HOISTED_CODE_1§


and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?};

to this:

§_DISCOURSE_HOISTED_CODE_1_§

and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'


and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'

to this:

§_DISCOURSE_HOISTED_CODE_1_§

and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'


to this:

§DISCOURSE_HOISTED_CODE_1§


and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'

to this:

§_DISCOURSE_HOISTED_CODE_1_§

and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'.','


to this:

§DISCOURSE_HOISTED_CODE_1§


and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'

to this:

§_DISCOURSE_HOISTED_CODE_1_§

and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?};


to this:

§DISCOURSE_HOISTED_CODE_1§


and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'.','

and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'


to this:

§DISCOURSE_HOISTED_CODE_1§


and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'

to this:

§_DISCOURSE_HOISTED_CODE_1_§

and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'


to this:

§DISCOURSE_HOISTED_CODE_1§


and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'.','

to this:

§_DISCOURSE_HOISTED_CODE_1_§

and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'


to this:

§DISCOURSE_HOISTED_CODE_1§


and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?};

to this:

§_DISCOURSE_HOISTED_CODE_1_§

and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'


and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'

to this:

§_DISCOURSE_HOISTED_CODE_1_§

and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'


to this:

§DISCOURSE_HOISTED_CODE_1§


and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'

to this:

§_DISCOURSE_HOISTED_CODE_1_§

and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'.','


to this:

§DISCOURSE_HOISTED_CODE_1§


and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'

to this:

§_DISCOURSE_HOISTED_CODE_1_§

and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?};


to this:

§DISCOURSE_HOISTED_CODE_1§


and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?};

and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'


to this:

§DISCOURSE_HOISTED_CODE_1§


and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'

to this:

§_DISCOURSE_HOISTED_CODE_1_§

and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'


to this:

§DISCOURSE_HOISTED_CODE_1§


and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'.','

to this:

§_DISCOURSE_HOISTED_CODE_1_§

and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?,'


to this:

§DISCOURSE_HOISTED_CODE_1§


and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?};

to this:

§_DISCOURSE_HOISTED_CODE_1_§

and it resolved the issue. So it seems like the char variables needed to be sized n+1 larger. Why was that?

It’s not just that it needs to be larger by one, as per my last post, functions like print expect to find a zero ( that is 0x00 and not ‘0’ at the end of the text). By increasing the array size, the value at the end just happened to be a zero and therefore terminated the string.

It would be better if you explicitly added the zero terminator to the end of the array. Then it is more explicit and it will always work in future without doubt.

Just after the last character in each of the mass variables add 0x00 as an extra value. This will guarantee to terminate the string.

Thanks. I did this and my code has been very stable. Good tip!