# LCD "Shift" issue

**URL:** <https://forum.arduino.cc/t/lcd-shift-issue/879282>\
**Category:** Displays\
**Created:** [June 28, 2021, 8:13am UTC](https://forum.arduino.cc/t/lcd-shift-issue/879282 "2021-06-28T08:13:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![justjay1986](https://avatars.discourse-cdn.com/v4/letter/j/e0b2c6/32.png) [@justjay1986](https://forum.arduino.cc/u/justjay1986)\
**Post date:** [June 28, 2021, 8:13am UTC](https://forum.arduino.cc/t/lcd-shift-issue/879282/1 "2021-06-28T08:13:16Z")

</div>

Hi all

I'm working on a prop with a 16x2 LCD and keypad, which i've managed to wire up and get them communicating, but the screen text "shifts" to the side when i enter the code i'm trying to use. The next screen is then well off the lcd. My section of code for this is below, any ideas please? I am relatively new to this and haven't done anything of similar in years! It's after enter code line

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

const byte ROWS = 4;  
const byte COLS = 3;  
char keys[ROWS][COLS] = {  
{'1', '2', '3'},  
{'4', '5', '6'},  
{'7', '8', '9'},  
{'\*', '0', '#'}  
};  
byte rowPins[ROWS] = {A0, A1, A2, A3};  
byte colPins[COLS] = {A4, A5, 2};

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

void setup() {  
pinMode(redled, OUTPUT);  
pinMode(yellowled, OUTPUT);  
pinMode(greenled, OUTPUT);  
lcd.begin(16, 2);  
Serial.begin(9600);

readVcc();  
if (readVcc() \>= 3800)  
{  
lcd.setCursor(0, 0);  
lcd.print("voltage is:");  
lcd.print(readVcc() / 1000.00);  
lcd.print("v");  
lcd.setCursor(0, 1);  
lcd.print("voltage good!");  
delay(3000);  
}

if (readVcc() \<= 3800)  
{  
lcd.setCursor(0, 0);  
lcd.print("voltage is:");  
lcd.print(readVcc() / 1000.00);  
lcd.print("v");  
lcd.setCursor(0, 1);  
lcd.print("replace battery!");  
delay(1000000);  
}

lcd.clear();  
lcd.setCursor(0, 0);  
lcd.print("Bomb activated!");  
lcd.setCursor(0, 1);  
lcd.print("Enter Code:");  
while (keycount \< 4)  
{  
lcd.setCursor(keycount + 12, 1);  
lcd.blink();  
char armcode = keypad.getKey();  
armcode == NO\_KEY;  
if (armcode != NO\_KEY)  
{  
if ((armcode != '\*') && (armcode != '#'))  
{  
lcd.print(armcode);  
tone(7, 5000, 100);  
password[keycount] = armcode;  
keycount++;  
}  
}  
}

---

<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:** [June 28, 2021, 9:08am UTC](https://forum.arduino.cc/t/lcd-shift-issue/879282/2 "2021-06-28T09:08:02Z")

</div>

Posting just part of a sketch is almost always a waste of time

**The easier you make it to read and copy your code the more likely it is that you will get help**

Please follow the advice given in the link below when posting code , **use code tags** and post the code here to make it easier to read and copy for examination

> [@How to get the best out of this forum](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966#posting-code):
>
> See also [FAQ - Arduino Forum](https://forum.arduino.cc/faq) for general rules on forum behaviour and etiquette. Hello, Welcome to the Arduino Forum. This guide explains how to get the best out of this forum. Please read and follow the instructions below. Being new here you might think this is having rules for the sake of rules, but that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is fru…

---

<div class="post-metadata">

**Author:** ![justjay1986](https://avatars.discourse-cdn.com/v4/letter/j/e0b2c6/32.png) [@justjay1986](https://forum.arduino.cc/u/justjay1986)\
**Post date:** [June 28, 2021, 9:22am UTC](https://forum.arduino.cc/t/lcd-shift-issue/879282/3 "2021-06-28T09:22:38Z")

</div>

```cpp
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Prop bomb for paintball/airsoft objective game, bomb uses a 12 armcode keypad, a LCD screen, 3 LED's and a speaker.
// Code reads chip input voltage (after the regulator) and if voltage is above minimum required to run it starts the bomb code.
// If voltage is low it will tell you to replace the battery and stays on that screen.
// User sets the disarm code (4 digits).
// Then user sets timer ( HH:MM:SS )
// You have 3 tries to get the code entered coorectly, 1st wrong attempt cuts the time in half,
// 2nd wrong attempt cuts time in half again and speeds time up by a multiple 10,
// The 3rd wrong attempt detonates the bomb.
// Must press * to enter the disarm code.
// If you mess up entering your disarm code press "#" to reset your try.
//
// Code used from draymond @ https://create.arduino.cc/projecthub/draymond/prop-bomb-for-paintball-airsoft-8ea257?f=1#comments
//
// Modified by romaytech@yahoo.com
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <Keypad.h>
#include <LiquidCrystal.h>

int Hours = 00;
int Minutes = 00;
int Seconds = 00;
int trycount = 0;
int keycount = 0;
int i = 0;

int redled = 4;
int yellowled = 1;
int greenled = 2;

int hourstenscode;
int hoursonescode;
int mintenscode;
int minonescode;
int sectenscode;
int seconescode;

long secMillis = 0;
long interval = 1000;

char password[4];
char entered[4];

int readVcc() {
  // Read 1.1V reference against AVcc
  // set the reference to Vcc and the measurement to the internal 1.1V reference
#if defined( __AVR_ATmega32U4__ ) || defined( __AVR_ATmega1280__ ) || defined( __AVR_ATmega2560__ )
  ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#elif defined ( __AVR_ATtiny24__ ) || defined( __AVR_ATtiny44__ ) || defined( __AVR_ATtiny84__ )
  ADMUX = _BV(MUX5) | _BV(MUX0) ;
#else
  ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#endif

  delay(2); // Wait for Vref to settle
  ADCSRA |= _BV(ADSC); // Start conversion
  while (bit_is_set(ADCSRA, ADSC)); // measuring

  uint8_t low = ADCL; // must read ADCL first - it then locks ADCH
  uint8_t high = ADCH; // unlocks both

  long result = (high << 8) | low;

  result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
  return result; // Vcc in millivolts
}

LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // the pins we use on the LCD
// pin 8 to RS, pin 9 to E, pin 10 to D4, pin 11 to D5, pin 12 to D6, pin 13 to D7.
// Ground to pins VSS,RW,K.
//+5vdc to pin VDD.
//220 ohm resistor between pins A and VDD.
//Potentiometer center pin to V0, positive pin to VDD, Negative pin to VSS.

const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};
byte rowPins[ROWS] = {A0, A1, A2, A3};
byte colPins[COLS] = {A4, A5, 2};

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

void setup() {
  pinMode(redled, OUTPUT);
  pinMode(yellowled, OUTPUT);
  pinMode(greenled, OUTPUT);
  lcd.begin(16, 2);
  Serial.begin(9600);

  readVcc();
  if (readVcc() >= 3800)
  {
    lcd.setCursor(0, 0);
    lcd.print("Voltage is:");
    lcd.print(readVcc() / 1000.00);
    lcd.print("v");
    lcd.setCursor(0, 1);
    lcd.print("Voltage Good!");
    delay(3000);
  }

  if (readVcc() <= 3800)
  {
    lcd.setCursor(0, 0);
    lcd.print("voltage is:");
    lcd.print(readVcc() / 1000.00);
    lcd.print("v");
    lcd.setCursor(0, 1);
    lcd.print("replace battery!");
    delay(1000000);
  }

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Bomb activated!");
  lcd.setCursor(0, 1);
  lcd.print("Enter Code:");
  while (keycount < 4)
  {
    lcd.setCursor(keycount + 12, 1);
    lcd.blink();
    char armcode = keypad.getKey();
    armcode == NO_KEY;
    if (armcode != NO_KEY)
    {
      if ((armcode != '*') && (armcode != '#'))
      {
        lcd.print(armcode);
        tone(7, 5000, 100);
        password[keycount] = armcode;
        keycount++;
      }
    }
  }

  if (keycount == 4)
  {
    delay(500);
    lcd.noBlink();
    lcd.clear();
    lcd.home();
    lcd.print("Disarm Code is: ");
    lcd.setCursor(6, 1);
    lcd.print(password[0]);
    lcd.print(password[1]);
    lcd.print(password[2]);
    lcd.print(password[3]);
    delay(3000);
    lcd.clear();
  }
  lcd.setCursor(0, 0);
  lcd.print("Timer: HH:MM:SS");
  lcd.setCursor(0, 1);
  lcd.print("SET: : :");
  keycount = 5;

  while (keycount == 5)
  {
    char hourstens = keypad.getKey();
    lcd.setCursor(5, 1);
    lcd.blink();
    if (hourstens >= '0' && hourstens <= '9')
    {
      hourstenscode = hourstens - '0';
      tone(7, 5000, 100);
      lcd.print(hourstens);
      keycount++;
    }
  }

  while (keycount == 6)
  {
    char hoursones = keypad.getKey();
    lcd.setCursor(6, 1);
    lcd.blink();
    if (hoursones >= '0' && hoursones <= '9')
    {
      hoursonescode = hoursones - '0';
      tone(7, 5000, 100);
      lcd.print(hoursones);
      keycount++;
    }
  }

  while (keycount == 7)
  {
    char mintens = keypad.getKey();
    lcd.setCursor(8, 1);
    lcd.blink();
    if (mintens >= '0' && mintens <= '9')
    {
      mintenscode = mintens - '0';
      tone(7, 5000, 100);
      lcd.print(mintens);
      keycount++;
    }
  }

  while (keycount == 8)
  {
    char minones = keypad.getKey();
    lcd.setCursor(9, 1);
    lcd.blink();
    if (minones >= '0' && minones <= '9')
    {
      minonescode = minones - '0';
      tone(7, 5000, 100);
      lcd.print(minones);
      keycount++;
    }
  }

  while (keycount == 9)
  {
    char sectens = keypad.getKey();
    lcd.setCursor(11, 1);
    lcd.blink();
    if (sectens >= '0' && sectens <= '9')
    {
      sectenscode = sectens - '0';
      tone(7, 5000, 100);
      lcd.print(sectens);
      keycount = 10;
    }
  }

  while (keycount == 10)
  {
    char secones = keypad.getKey();
    lcd.setCursor(12, 1);
    lcd.blink();
    if (secones >= '0' && secones <= '9')
    {
      seconescode = secones - '0';
      tone(7, 5000, 100);
      lcd.print(secones);
      keycount = 11;
    }
  }

  if (keycount == 11);
  {
    Hours = (hourstenscode * 10) + hoursonescode;
    Minutes = (mintenscode * 10) + minonescode;
    Seconds = (sectenscode * 10) + seconescode;
    delay(100);
    lcd.noBlink();
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(" Timer set at:");
    if (Hours >= 10)
    {
      lcd.setCursor (7, 1);
      lcd.print (Hours);
    }
    if (Hours < 10)
    {
      lcd.setCursor (7, 1);
      lcd.write ("0");
      lcd.setCursor (8, 1);
      lcd.print (Hours);
    }
    lcd.print (":");

    if (Minutes >= 10)
    {
      lcd.setCursor (10, 1);
      lcd.print (Minutes);
    }
    if (Minutes < 10)
    {
      lcd.setCursor (10, 1);
      lcd.write ("0");
      lcd.setCursor (11, 1);
      lcd.print (Minutes);
    }
    lcd.print (":");

    if (Seconds >= 10)
    {
      lcd.setCursor (13, 1);
      lcd.print (Seconds);
    }

    if (Seconds < 10)
    {
      lcd.setCursor (13, 1);
      lcd.write ("0");
      lcd.setCursor (14, 1);
      lcd.print (Seconds);
    }
    delay(3000);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(" Press # to arm");
    delay (50);
    keycount = 12;
  }

  while (keycount == 12)
  {
    char armkey = keypad.getKey();

    if (armkey == '#')
    {
      tone(7, 5000, 100);
      delay(50);
      tone(7, 0, 100);
      delay(50);
      tone(7, 5000, 100);
      delay(50);
      tone(7, 0, 100);
      delay(50);
      tone(7, 5000, 100);
      delay(50);
      tone(7, 0, 100);
      lcd.clear();
      lcd.print (" Bomb Armed!");
      lcd.setCursor(0, 1);
      lcd.print("Countdown start");
      delay(3000);
      lcd.clear();
      keycount = 0;
    }
  }
}
void loop()
{
  timer();
  char disarmcode = keypad.getKey();

  if (disarmcode == '*')
  {
    tone(7, 5000, 100);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Code: ");

    while (keycount < 4)
    {
      timer();

      char disarmcode = keypad.getKey();
      if (disarmcode == '#')
      {
        tone(7, 5000, 100);
        keycount = 0;
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Code: ");
      }
      else if (disarmcode != NO_KEY)
      {
        lcd.setCursor(keycount + 7, 0);
        lcd.blink();
        lcd.print(disarmcode);
        entered[keycount] = disarmcode;
        keycount++;
        tone(7, 5000, 100);
        delay(100);
        lcd.noBlink();
        lcd.setCursor(keycount + 6, 0);
        lcd.print("*");
        lcd.setCursor(keycount + 7, 0);
        lcd.blink();
      }
    }

    if (keycount == 4)
    {
      if (entered[0] == password[0] && entered[1] == password[1] && entered[2] == password[2] && entered[3] == password[3])
      {
        lcd.noBlink();
        lcd.clear();
        lcd.home();
        lcd.print(" Bomb Defused!");
        lcd.setCursor(0, 1);
        lcd.print(" Great job!");
        keycount = 0;
        digitalWrite(greenled, HIGH);
        delay(15000);
        lcd.clear();
        lcd.setCursor(0, 1);
        lcd.print(" Reset the Bomb");
        delay(1000000);
      }
      else
      {
        lcd.noBlink();
        lcd.clear();
        lcd.home();
        lcd.print("Wrong Password!");
        trycount++;

        if (Hours > 0)
        {
          Hours = Hours / 2;
        }

        if (Minutes > 0)
        {
          Minutes = Minutes / 2;
        }
        if (Seconds > 0)
        {
          Seconds = Seconds / 2;
        }
        if (trycount == 2)
        {
          interval = interval / 10;
        }
        if (trycount == 3)
        {
          Minutes = Minutes - 59;
          Hours = Hours - 59;
          Seconds = Seconds - 59;
        }
        delay(1000);
        keycount = 0;
      }
    }
  }
}

void timer()
{
  Serial.print(Seconds);
  Serial.println();

  if (Hours <= 0)
  {
    if ( Minutes < 0 )
    {
      lcd.noBlink();
      lcd.clear();
      lcd.home();
      lcd.print(" The Bomb Has");
      lcd.setCursor (0, 1);
      lcd.print(" Exploded!");

      while (Minutes < 0)
      {
        digitalWrite(redled, HIGH);
        tone(7, 7000, 100);
        delay(100);
        digitalWrite(redled, LOW);
        tone(7, 7000, 100);
        delay(100);
// digitalWrite(yellowled, HIGH);
// tone(7, 7000, 100);
// delay(100);
// digitalWrite(yellowled, LOW);
// tone(7, 7000, 100);
// delay(100);
// digitalWrite(greenled, HIGH);
// tone(7, 7000, 100);
// delay(100);
// digitalWrite(greenled, LOW);
// tone(7, 7000, 100);
// delay(100);
      }
    }
  }
  lcd.setCursor (0, 1);
  lcd.print ("Timer:");

  if (Hours >= 10)
  {
    lcd.setCursor (7, 1);
    lcd.print (Hours);
  }
  if (Hours < 10)
  {
    lcd.setCursor (7, 1);
    lcd.write ("0");
    lcd.setCursor (8, 1);
    lcd.print (Hours);
  }
  lcd.print (":");

  if (Minutes >= 10)
  {
    lcd.setCursor (10, 1);
    lcd.print (Minutes);
  }
  if (Minutes < 10)
  {
    lcd.setCursor (10, 1);
    lcd.write ("0");
    lcd.setCursor (11, 1);
    lcd.print (Minutes);
  }
  lcd.print (":");

  if (Seconds >= 10)
  {
    lcd.setCursor (13, 1);
    lcd.print (Seconds);
  }

  if (Seconds < 10)
  {
    lcd.setCursor (13, 1);
    lcd.write ("0");
    lcd.setCursor (14, 1);
    lcd.print (Seconds);
  }

  if (Hours < 0)
  {
    Hours = 0;
  }

  if (Minutes < 0)
  {
    Hours --;
    Minutes = 59;
  }

  if (Seconds < 1)
  {
    Minutes --;
    Seconds = 59;
  }

  if (Seconds > 0)
  {
    unsigned long currentMillis = millis();

    if (currentMillis - secMillis > interval)
    {
      tone(7, 7000, 50);
      secMillis = currentMillis;
      Seconds --;
      digitalWrite(greenled, HIGH);
      digitalWrite(redled, HIGH);
      delay(10);
      digitalWrite(greenled, LOW);
      digitalWrite(redled, LOW);      
      delay(10);
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![justjay1986](https://avatars.discourse-cdn.com/v4/letter/j/e0b2c6/32.png) [@justjay1986](https://forum.arduino.cc/u/justjay1986)\
**Post date:** [June 28, 2021, 9:24am UTC](https://forum.arduino.cc/t/lcd-shift-issue/879282/4 "2021-06-28T09:24:14Z")

</div>

apologies, i have added the code i think, correctly. ....

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 26, 2021, 9:25am UTC](https://forum.arduino.cc/t/lcd-shift-issue/879282/5 "2021-10-26T09:25:07Z")

</div>

Closed on October 26, 2021, 9:25am UTC

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