# Removing garbage Values from an array

**URL:** <https://forum.arduino.cc/t/removing-garbage-values-from-an-array/388424>\
**Category:** Programming\
**Created:** [May 27, 2016, 7:51am UTC](https://forum.arduino.cc/t/removing-garbage-values-from-an-array/388424 "2016-05-27T07:51:33Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![VivuStriker](https://avatars.discourse-cdn.com/v4/letter/v/9de0a6/32.png) [@VivuStriker](https://forum.arduino.cc/u/VivuStriker)\
**Post date:** [May 27, 2016, 7:51am UTC](https://forum.arduino.cc/t/removing-garbage-values-from-an-array/388424/1 "2016-05-27T07:51:33Z")

</div>

Hello all,

I have written a code for displaying Keyboard data on LCD.  
When i press the RETURN key it shows data stored in the array InputMsg[20]. However, it also fills in the unused space of array with garbage value, how will i overcome this?

Attached an image showing the problem.

the input i gave was "Hello All" the remaining inputs are garbage value.

Regards.

```cpp
#include <LiquidCrystal.h>
#include <PS2Keyboard.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 13, 14, 15, 16, 17);

const int DataPin = 2;
const int IRQpin =  3;

char InputMsg[20];
static int i = 0;
char KbChars;

PS2Keyboard keyboard;
 

void setup() {
  lcd.begin(16, 2);
  delay(100);
  keyboard.begin(DataPin, IRQpin);
  lcd.setCursor(0, 0);
  lcd.print("Input Message:");
  lcd.setCursor(0, 1);
}
 
void loop() {

 
 // Turn off the cursor:
  lcd.noCursor();
  delay(100);
  // Turn on the cursor:
  lcd.cursor();
  delay(100);
 
if (keyboard.available()) {
 

  // read the next key
    KbChars = keyboard.read();
        

  if (KbChars == PS2_ESC) {
      clear();
                          }
 /////////////////////FUNCTION/////////////////////    
    else if (KbChars == PS2_ENTER) {
                lcd.clear();
                delay(100);
                for(int k=0;k<20;k++)
                {
                lcd.print(InputMsg[k]);
                }
              }
 ///////////////////////FUNCTION/////////////////////  
   
  else
  {
      // otherwise, just print all normal characters
      lcd.print(KbChars);
    }
    InputMsg[i] = KbChars;
    i++;
    InputMsg[i] = '\0';  // terminate the string

    }
  }

void clear()
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Input Message:");
  KbChars = keyboard.read();
  lcd.setCursor(0, 1);
}

```

 ![DSC_3666.jpg](https://europe1.discourse-cdn.com/arduino/original/4X/3/8/e/38e1fd405efead39296e54cdc0399f2c437ac913.jpeg)

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [May 27, 2016, 7:57am UTC](https://forum.arduino.cc/t/removing-garbage-values-from-an-array/388424/2 "2016-05-27T07:57:34Z")

</div>

```cpp
                int k=0;
                while(InputMsg[k] != '\0')
                {
                lcd.print(InputMsg[k]);
                  k++:
                }

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [May 27, 2016, 8:00am UTC](https://forum.arduino.cc/t/removing-garbage-values-from-an-array/388424/3 "2016-05-27T08:00:09Z")

</div>

> When i press the RETURN key

At first sight I cannot see a test for the Return key in your code.

However, I do see that if you receive more than 8 characters you print the entire contents of the buffer. Why not just print the number of characters that you have received instead of all 20 ?

---

<div class="post-metadata">

**Author:** ![VivuStriker](https://avatars.discourse-cdn.com/v4/letter/v/9de0a6/32.png) [@VivuStriker](https://forum.arduino.cc/u/VivuStriker)\
**Post date:** [May 27, 2016, 8:06am UTC](https://forum.arduino.cc/t/removing-garbage-values-from-an-array/388424/4 "2016-05-27T08:06:30Z")

</div>

Sorry, that was an older code, thanks for pointing it out **UKHeliBob**.

```cpp
#include <LiquidCrystal.h>
#include <PS2Keyboard.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 13, 14, 15, 16, 17);

const int DataPin = 2;
const int IRQpin =  3;

char InputMsg[20];
static int i = 0;
char KbChars;

PS2Keyboard keyboard;
 

void setup() {
  lcd.begin(16, 2);
  delay(100);
  keyboard.begin(DataPin, IRQpin);
  lcd.setCursor(0, 0);
  lcd.print("Input Message:");
  lcd.setCursor(0, 1);
}
 
void loop() {

 
 
 // Turn off the cursor:
  lcd.noCursor();
  delay(100);
  // Turn on the cursor:
  lcd.cursor();
  delay(100);
 
if (keyboard.available()) {
 

  // read the next key
    KbChars = keyboard.read();
        

  if (KbChars == PS2_ESC) {
      clear();
                          }
 /////////////////////FUNCTION/////////////////////    
    else if (KbChars == PS2_ENTER) {
                lcd.clear();
                delay(100);
                for(int k=0;k<20;k++)
                {
                lcd.print(InputMsg[k]); 
                }
              }
 ///////////////////////FUNCTION/////////////////////  
   
  else
  {
      // otherwise, just print all normal characters
      lcd.print(KbChars);
    }
    InputMsg[i] = KbChars;
    i++;
    InputMsg[i] = '\0';  // terminate the string

    }
  }

void clear()
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Input Message:");
  KbChars = keyboard.read();
  lcd.setCursor(0, 1);
}

```

---

<div class="post-metadata">

**Author:** ![VivuStriker](https://avatars.discourse-cdn.com/v4/letter/v/9de0a6/32.png) [@VivuStriker](https://forum.arduino.cc/u/VivuStriker)\
**Post date:** [May 27, 2016, 8:19am UTC](https://forum.arduino.cc/t/removing-garbage-values-from-an-array/388424/5 "2016-05-27T08:19:25Z")

</div>

Also, in the above program how will i clear the data of array once i press Escape key?

i tried writing

```cpp
if (KbChars == PS2_ESC) {

      InputMsg[i] = '\0';
      clear();
                          }

```

but it doesn't help.

Thank You.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [May 27, 2016, 9:26am UTC](https://forum.arduino.cc/t/removing-garbage-values-from-an-array/388424/6 "2016-05-27T09:26:35Z")

</div>

Instead of

```arduino
      delay(100);
      for (int k = 0; k < 20; k++)
      {
        lcd.print(InputMsg[k]);
      }

```

just do

```arduino
lcd.print(InputMsg);

```

It will stop at the terminating zero in the array.

> Also, in the above program how will i clear the data of array once i press Escape key?

You don't need to as long as when you receive a character you put the zero after it.

Incidentally, you are not counting the number of characters entered, so if the user enters 20 characters or more you will write outside of the memory allocated to the array.

---

<div class="post-metadata">

**Author:** ![VivuStriker](https://avatars.discourse-cdn.com/v4/letter/v/9de0a6/32.png) [@VivuStriker](https://forum.arduino.cc/u/VivuStriker)\
**Post date:** [May 27, 2016, 9:42am UTC](https://forum.arduino.cc/t/removing-garbage-values-from-an-array/388424/7 "2016-05-27T09:42:52Z")

</div>

> UKHeliBob:  
> You don't need to as long as when you receive a character you put the zero after it.

But if i don't clear the array, and press ESCAPE key, the data still remains in the Array and the LCD gets cleared, according to what have i written in the code for "void clear()".  
Now,when i type new from keyboard, it gets concatenated to the previous data.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [May 27, 2016, 11:55am UTC](https://forum.arduino.cc/t/removing-garbage-values-from-an-array/388424/8 "2016-05-27T11:55:31Z")

</div>

> Now,when i type new from keyboard, it gets concatenated to the previous data.

Only if you don't set the array index variable back to zero before handling the first character in the new string

---

<div class="post-metadata">

**Author:** ![VivuStriker](https://avatars.discourse-cdn.com/v4/letter/v/9de0a6/32.png) [@VivuStriker](https://forum.arduino.cc/u/VivuStriker)\
**Post date:** [May 30, 2016, 7:02am UTC](https://forum.arduino.cc/t/removing-garbage-values-from-an-array/388424/9 "2016-05-30T07:02:24Z")

</div>

Hi everyone,

I'm using my PS2 keyboard and an LCD to set a password.  
The program i have written works as follows:  
Read Keyboard Characters--\> If TAB key pressed--\> LCD displays "Enter Password"--\> I enter password of characters of length not more than password[6],i.e. 5 Characters.--\> Then i press Enter Key--\> LCD shows "Password Set".  
However, right after the TAB key operation, i face some bugs:  
1)Garbage values written on LCD.(in the attached picture,please see)  
2)When i press ENTER key,it gets printed on the LCD instead of showing "PASSWORD is...."

I know where am I wrong--\>in the declaration of IF loop concerning the ENTER key.  
What should i do to correct this?  
Regards. 🙂

```cpp
#include <LiquidCrystal.h>
#include <PS2Keyboard.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 13, 14, 15, 16, 17);

const int DataPin = 2;
const int IRQpin =  3;

PS2Keyboard keyboard;

char KbChars;
char array[50];
char inByte;
char password[6];
int i=0;

void setup() {

  lcd.begin(16, 2);
  delay(100);
  keyboard.begin(DataPin, IRQpin);
  lcd.setCursor(0, 0);
  lcd.print("Input Message:");
  lcd.setCursor(0,1);
 } 

void loop() {
 
   if (keyboard.available()) {
  KbChars=keyboard.read();
  
  if (KbChars == PS2_TAB) {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Enter Password:");
      lcd.setCursor(0, 1);
      KbChars=keyboard.read();
      lcd.print(KbChars);
        password[i]=KbChars;
        i++;
        password[i]='\0';
      if(i==6 || KbChars == PS2_ENTER){
                lcd.clear();
                lcd.setCursor(0, 0);
                lcd.print("Password is:");
                delay(100);
                while(password[i] != '\0')
                {
                 lcd.setCursor(0, 1);
                 lcd.print(password[i]);
                  i++;
                }
               }
      }
      lcd.print(KbChars);
      }
      
  }

```

![DSC_3664.jpg](https://europe1.discourse-cdn.com/arduino/original/4X/c/e/7/ce7b8197a620edefced534264e12b014231d816e.jpeg)

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [May 30, 2016, 7:23am UTC](https://forum.arduino.cc/t/removing-garbage-values-from-an-array/388424/10 "2016-05-30T07:23:35Z")

</div>

The program that you have posted has no chance of working.  
Once the user presses the tab key you immediately read the keyboard again. How likely is it that the user will have pressed enter by then ?

Even if that worked you never set the array index i back to zero in the program.

If I were you I would have a variable whose state, perhaps 0, 1, 2 etc indicates what stage the program is at, test it, perhaps using switch/state, to take the appropriate action

```cpp
state is 0
  read keyboard until use presses tab then move to state 1
state 1
  read keyboard
  if user presses enter move to state 2
  if index equal 6 move to state 2
  display character entered
state 2
  do whatever you want/need with the password

```

---

<div class="post-metadata">

**Author:** ![VivuStriker](https://avatars.discourse-cdn.com/v4/letter/v/9de0a6/32.png) [@VivuStriker](https://forum.arduino.cc/u/VivuStriker)\
**Post date:** [May 30, 2016, 3:09pm UTC](https://forum.arduino.cc/t/removing-garbage-values-from-an-array/388424/11 "2016-05-30T15:09:59Z")

</div>

As suggested by **UKHeliBob** i tried using the Switch Case statement, it works good with ENTER and TAB keys,i.e. cases of the switch, but doesn't accept any characters inside the case loop.  
the problem occurs where ////Problem///// sign is shown.

Thank you.

```cpp
#include <LiquidCrystal.h>
#include <PS2Keyboard.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 13, 14, 15, 16, 17);

const int DataPin = 2;
const int IRQpin =  3;

PS2Keyboard keyboard;

char KbChars;
char array[50];
char inByte;
char password[6];
int i=0;

void setup() {

  lcd.begin(16, 2);
  delay(100);
  keyboard.begin(DataPin, IRQpin);
  lcd.setCursor(0, 0);
  lcd.print("Input Message:");
  lcd.setCursor(0,1);
 } 

void loop() {
 // read the sensor:
  KbChars=keyboard.read();
  
  switch (KbChars) {
    case PS2_TAB:
      lcd.clear(); 
      lcd.setCursor(0,0);
      lcd.print("Password:");
      lcd.setCursor(0,1);
///////////////////////////////////////////Problem/////
      KbChars=keyboard.read();
      lcd.print(KbChars);
      break;
   
    case PS2_ENTER: 
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Password Set");
      lcd.setCursor(0,1);
///////////////////////////////////////////Problem/////
      KbChars=keyboard.read();
      lcd.print(KbChars);
      break;
    
  }
  delay(1);        // delay in between reads for stability
}

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [May 30, 2016, 3:28pm UTC](https://forum.arduino.cc/t/removing-garbage-values-from-an-array/388424/12 "2016-05-30T15:28:31Z")

</div>

The code in case PS2\_ENTER: will only be executed when enter is received, not for any other character, which is what is causing the problem.

My suggestion was based on using a state variable whose value would determine which section of code was executed.

This is the sort of structure that I had in mind

```cpp
byte state = 0;

void loop()
{
  KbChars = keyboard.read();

  switch (state)
  {
    case 0:
      if (KbChars == PS2_TAB)
      case = 1;
      break;

    case 1:
      if (KbChars == PS2_ENTER || inputIndex == 6)
      case = 2;
      lcd.print(KbChars);
      inputIndex++;
      break;

    case 2:
      inputIndex = 0;
      //do what you want with the password
      break;
  }
}

```

NOTE that this is not a complete solution, merely an idea. For instance no messages are printed nor is the password saved to the array but these can be added at appropriate points.

---

<div class="post-metadata">

**Author:** ![VivuStriker](https://avatars.discourse-cdn.com/v4/letter/v/9de0a6/32.png) [@VivuStriker](https://forum.arduino.cc/u/VivuStriker)\
**Post date:** [May 30, 2016, 4:01pm UTC](https://forum.arduino.cc/t/removing-garbage-values-from-an-array/388424/13 "2016-05-30T16:01:41Z")

</div>

Actually its not just the password and keyboard, i have many other functions to be performed:  
1)input message-\>Display it  
2)Cipher that message--\>Display it  
3)Set a key(password), This is the only case where i need to use the keyboard keys THRICE in one function (One for Entering the Password Loop,And second to Enter the Key,third for breaking out from the loop)

Hence Im afraid that using what you suggested will not be advisable for whole process.  
Is there a way i can use Three keys in a row, like i explained for setting password?

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [May 30, 2016, 4:10pm UTC](https://forum.arduino.cc/t/removing-garbage-values-from-an-array/388424/14 "2016-05-30T16:10:40Z")

</div>

The state machine will work fine alongside other functions or you can incorporate other functions within it. For instance you could encrypt the password once it reached state 2. If you set state to a number not recognised by the switch/case then the code will simply be ignored if that is what you want.

> Is there a way i can use Three keys in a row, like i explained for setting password?

That is exactly what the state machine does

---

<div class="post-metadata">

**Author:** ![VivuStriker](https://avatars.discourse-cdn.com/v4/letter/v/9de0a6/32.png) [@VivuStriker](https://forum.arduino.cc/u/VivuStriker)\
**Post date:** [May 30, 2016, 4:14pm UTC](https://forum.arduino.cc/t/removing-garbage-values-from-an-array/388424/15 "2016-05-30T16:14:46Z")

</div>

Thanks Bob,  
Ill try it and repost about the results.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [May 30, 2016, 4:20pm UTC](https://forum.arduino.cc/t/removing-garbage-values-from-an-array/388424/16 "2016-05-30T16:20:47Z")

</div>

I suggest that you get the password input, including saving to a zero terminated array of chars working first.

---

<div class="post-metadata">

**Author:** ![VivuStriker](https://avatars.discourse-cdn.com/v4/letter/v/9de0a6/32.png) [@VivuStriker](https://forum.arduino.cc/u/VivuStriker)\
**Post date:** [May 30, 2016, 4:23pm UTC](https://forum.arduino.cc/t/removing-garbage-values-from-an-array/388424/17 "2016-05-30T16:23:56Z")

</div>

okay,as you say. 🙂 Will work out that first.

---

<div class="post-metadata">

**Author:** ![VivuStriker](https://avatars.discourse-cdn.com/v4/letter/v/9de0a6/32.png) [@VivuStriker](https://forum.arduino.cc/u/VivuStriker)\
**Post date:** [May 30, 2016, 5:14pm UTC](https://forum.arduino.cc/t/removing-garbage-values-from-an-array/388424/18 "2016-05-30T17:14:28Z")

</div>

I have done displaying of message and ciphering it using the ROT13, all works well but:

1. When i press tab key for encrypting the text, Ciphered message is shown,however,a Garbage value is also printed on LCD, now this is NOT actually GARBAGE value,but its the value of TAB key. how?

As many times i press TAB, the text gets ciphered and another Garbage(TAB value) gets added at the end. See attachment 1 and 2.  
Same is for the ENTER Key.

What should i do so that it is not printed on LCD?

1. When ESCAPE (ESC) pressed, the loop is refreshed, but LCD starts from the position (1,1) instead of (0,1). I dont get why.

The Code:

```cpp
#include <LiquidCrystal.h>
#include <PS2Keyboard.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 13, 14, 15, 16, 17);

const int DataPin = 2;
const int IRQpin =  3;

PS2Keyboard keyboard;

char KbChars;
char array[50];
char inByte;
int i=0;

void setup() {

  lcd.begin(16, 2);
  delay(100);
  keyboard.begin(DataPin, IRQpin);
  i=0;
  lcd.setCursor(0, 0);
  lcd.print("Input Message:");
  lcd.setCursor(0,1);
 } 

void loop() {
 
//Blinking  
  lcd.noCursor();
  delay(100);
  lcd.cursor();
  delay(100);
//Blinking
  
  if (keyboard.available()) {
  KbChars=keyboard.read();
  
  if (KbChars == PS2_ESC)
      {
      lcd.clear();
      delay(500);
      setup();
      }
  if (KbChars == PS2_TAB) {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Ciphered Text:");
      lcd.setCursor(0, 1);
      rot13();
      lcd.print(array);
    }
    if (KbChars == PS2_ENTER) {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Main:");
      lcd.setCursor(0, 1);
      lcd.print(array);
    }
    
    
    array[i]=KbChars;
    i++;
    lcd.print(KbChars);
  }
}
   

void rot13() {

        for (int i = 0; i <= 50; ++i) {  
            inByte = array[i];
      if (!inByte) return; //end of string reached
          
        if (inByte >= 'A' && inByte <= 'M')
            inByte +=13;
        else if (inByte >= 'a' && inByte <= 'm')
            inByte +=13;
        else if (inByte >= 'N' && inByte <= 'Z')
            inByte -=13;
        else if (inByte >= 'n' && inByte <= 'z')
            inByte -=13;

        array[i] = inByte;
    }
    }

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [May 30, 2016, 5:31pm UTC](https://forum.arduino.cc/t/removing-garbage-values-from-an-array/388424/19 "2016-05-30T17:31:55Z")

</div>

I thought that you were going to get the password input working first.

As to the program that you posted, nowhere do you put the terminating zero in the array.

I can't help feeling that we are going round in circles.

---

<div class="post-metadata">

**Author:** ![VivuStriker](https://avatars.discourse-cdn.com/v4/letter/v/9de0a6/32.png) [@VivuStriker](https://forum.arduino.cc/u/VivuStriker)\
**Post date:** [May 30, 2016, 5:52pm UTC](https://forum.arduino.cc/t/removing-garbage-values-from-an-array/388424/20 "2016-05-30T17:52:11Z")

</div>

But adding a terminating Zero wont really help, the output is still the same. See the pictures in attachments.  
Pic 1&2 refers to problem 1.  
Pic 3 is for problem 2.

Thank You.

_I will be working on my **PASSWORD** problem as soon as i remove these small bugs._

 ![pic1.jpg](https://europe1.discourse-cdn.com/arduino/original/4X/8/d/7/8d795ac0f5e73cd22f7a8a94f318764862d823cf.jpeg)

 ![pic2.jpg](https://europe1.discourse-cdn.com/arduino/original/4X/d/9/3/d930f964a99f7108ba505ad0b56da97bb58a38dc.jpeg)

 ![pic3.jpg](https://europe1.discourse-cdn.com/arduino/original/4X/4/4/c/44c5666f627da1751743ed10fc46301259337300.jpeg)

[Next page](https://forum.arduino.cc/t/removing-garbage-values-from-an-array/388424.md?page=2)
