title: Arduino lcd screen showing broken characters at random times
body: <p>So I've been working on an arduino project for an escape room puzzle. It's essentially a bomb with a timer on an lcd screen. After all of the elements of the bomb are solved the timer stops and the lcd screen displays that the bomb has been defused and gives the group a clue to the next puzzle. 9 times out of 10 it works perfectly. But every once in a while when it is supposed to display that the bomb is defused the lcd screen just shows random broken characters. I haven't had any luck diagnosing the problem. Hoping somebody here might have an idea. </p>
  
  <pre><code>#include &lt;Keypad.h&gt;
  #include &lt;LiquidCrystal.h&gt;
  #include &lt;Tone.h&gt;
  #define pound 14
  
  Tone tone1;
  
  int Scount = 0;
  int Mcount = 0;
  int Hcount = 1;
  int DefuseTimer = 0;
  
  long secMillis = 0;
  long interval = 1000;
  
  char password[6] = "594432";
  int currentLength = 0;
  int i = 0;
  char entered[6];
  
  int ledPin = 23;
  int ledPin2 = 25;
  int ledPin3 = 27;
  int ledPin4 = 29;
  int ledPin5 = 31;
  int ledPin6 = 34;
  
  const int plugin1 = 44;
  const int plugin2 = 46;
  const int plugin3 = 48;
  const int plugin4 = 50;
  const int plugin5 = 52;
  
  int plugin1State = 0;
  int plugin2State = 0;
  int plugin3State = 0;
  int plugin4State = 0;
  int plugin5State = 0;
  
  const int switch1 = 37;
  
  int switch1State = 0;
  
  const int key1 = 40;
  
  int key1State = 0;
  
  int puzzle1 = 0;
  int puzzle2 = 0;
  int puzzle3 = 0;
  
  int solved = 0;
  
  LiquidCrystal lcd(7, 8, 10, 11, 12, 13);
  
  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, A6};
  
  Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
  
  void setup() {
    pinMode(ledPin, OUTPUT);
    pinMode(ledPin2, OUTPUT);
    pinMode(ledPin3, OUTPUT);
    pinMode(ledPin4, OUTPUT);
    pinMode(ledPin5, OUTPUT);
    pinMode(ledPin6, OUTPUT);
  
    pinMode(plugin1, INPUT);
    pinMode(plugin2, INPUT);
    pinMode(plugin3, INPUT);
    pinMode(plugin4, INPUT);
    pinMode(plugin5, INPUT);
  
    digitalWrite(plugin1, HIGH);
    digitalWrite(plugin2, HIGH);
    digitalWrite(plugin3, HIGH);
    digitalWrite(plugin4, HIGH);
    digitalWrite(plugin5, HIGH);
  
    pinMode(switch1, INPUT);
    digitalWrite(switch1, HIGH);
  
    pinMode(key1, INPUT_PULLUP);
    digitalWrite(key1, HIGH);
  
    tone1.begin(9);
    lcd.begin(16, 2);
    Serial.begin(9600);
    lcd.clear();
    lcd.setCursor(0, 0);
  
    tone1.play(NOTE_E6, 200);
    delay(3000);
    lcd.clear();
    currentLength = 0;
  }
  
  
  void loop()
  {
    timer();
  
    plugin1State = digitalRead(plugin1);
    plugin2State = digitalRead(plugin2);
    plugin3State = digitalRead(plugin3);
    plugin4State = digitalRead(plugin4);
    plugin5State = digitalRead(plugin5);
  
    if (plugin1State == LOW &amp;&amp; plugin2State == LOW &amp;&amp; plugin3State == LOW &amp;&amp; plugin4State == LOW &amp;&amp; plugin5State == LOW)
    {
      puzzle1 = 1;
    }
  
  
    if (puzzle1 == 1)
    {
      digitalWrite(ledPin4, HIGH);
      switch1State = digitalRead(switch1);
      if (switch1State == LOW)
      {
        puzzle2 = 1;
      }
    }
  
    if (puzzle2 == 1)
    {
      digitalWrite(ledPin5, HIGH);
      key1State = digitalRead(key1);
      if (key1State == LOW)
      {
        puzzle3 = 1;
      }
      if (key1State == HIGH)
      {
        digitalWrite(ledPin6, LOW);
      }
    }
  
    if (puzzle3 == 1)
    {
      digitalWrite(ledPin6, HIGH);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Code: ");
  
      while (currentLength &lt; 6)
      {
        timer();
  
        char key2 = keypad.getKey();
        if (key2 == "#")
        {
          currentLength = 0;
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("Code: ");
        }
        else if (key2 != NO_KEY)
        {
          lcd.setCursor(currentLength + 7, 0);
          lcd.cursor();
  
          lcd.print(key2);
          entered[currentLength] = key2;
          currentLength++;
          tone1.play(NOTE_C6, 200);
          delay(100);
          lcd.noCursor();
          lcd.setCursor(currentLength + 6, 0);
          lcd.print("*");
          lcd.setCursor(currentLength + 7, 0);
          lcd.cursor();
        }
      }
      if (currentLength == 6)
      {
        if (entered[0] == password[0] &amp;&amp; entered[1] == password[1] &amp;&amp; entered[2] == password[2] &amp;&amp; entered[3] == password[3] &amp;&amp; entered[4] == password[4] &amp;&amp; entered[5] == password[5])
        {
          solved = 1;
  
          while (solved == 1)
          {
            lcd.noCursor();
            lcd.clear();
            lcd.home();
            lcd.print("BOMB 1 DEFUSED");
            currentLength = 0;
            digitalWrite(ledPin3, HIGH);
            delay(1500);
  
            lcd.noCursor();
            lcd.clear();
            lcd.home();
            lcd.print("RELEASE");
            delay(1500);
  
            lcd.noCursor();
            lcd.clear();
            lcd.home();
            lcd.print("TOXIC GAS");
            delay(1500);
  
            lcd.noCursor();
            lcd.clear();
            lcd.home();
            lcd.print("CLUE: %&amp;@$#");
            delay(6000);
          }
        }
        else
        {
          lcd.noCursor();
          lcd.clear();
          lcd.home();
          lcd.print("Wrong Password!");
  
          delay(1500);
          currentLength = 0;
        }
      }
    }
  }
  
  void timer()
  {
  
    if (Hcount &lt;= 0)
    {
      if ( Mcount &lt; 0 )
      {
        lcd.noCursor();
        lcd.clear();
        lcd.home();
        lcd.print("The Bomb Has ");
        lcd.setCursor (0, 1);
        lcd.print("Exploded!");
  
        while (Mcount &lt; 0)
        {
          digitalWrite(ledPin, HIGH); // sets the LED on
          tone1.play(NOTE_A2, 90);
          delay(100);
          digitalWrite(ledPin, LOW); // sets the LED off
          tone1.play(NOTE_A2, 90);
          delay(100);
          digitalWrite(ledPin2, HIGH); // sets the LED on
          tone1.play(NOTE_A2, 90);
          delay(100);
          digitalWrite(ledPin2, LOW); // sets the LED off
          tone1.play(NOTE_A2, 90);
          delay(100);
          digitalWrite(ledPin3, HIGH); // sets the LED on
          tone1.play(NOTE_A2, 90);
          delay(100);
          digitalWrite(ledPin3, LOW); // sets the LED off
          tone1.play(NOTE_A2, 90);
          delay(100);
        }
      }
    }
  
    lcd.setCursor (0, 1); // sets cursor to 2nd line
    lcd.print ("Timer:");
  
    if (Hcount &gt;= 10)
    {
      lcd.setCursor (7, 1);
      lcd.print (Hcount);
    }
    if (Hcount &lt; 10)
    {
      lcd.setCursor (7, 1);
      lcd.write ("0");
      lcd.setCursor (8, 1);
      lcd.print (Hcount);
    }
  
    lcd.print (":");
  
    if (Mcount &gt;= 10)
    {
      lcd.setCursor (10, 1);
      lcd.print (Mcount);
    }
    if (Mcount &lt; 10)
    {
      lcd.setCursor (10, 1);
      lcd.write ("0");
      lcd.setCursor (11, 1);
      lcd.print (Mcount);
    }
  
    lcd.print (":");
  
    if (Scount &gt;= 10)
    {
      lcd.setCursor (13, 1);
      lcd.print (Scount);
    }
    if (Scount &lt; 10)
    {
      lcd.setCursor (13, 1);
      lcd.write ("0");
      lcd.setCursor (14, 1);
      lcd.print (Scount);
    }
  
    if (Hcount &lt; 0)
    {
      Hcount = 0;
    }
  
    if (Mcount &lt; 0)
    {
      Hcount --;
      Mcount = 59;
    }
  
    if (Scount &lt; 1) // if 60 do this operation
    {
      Mcount --; // add 1 to Mcount
      Scount = 59; // reset Scount
    }
  
    if (Scount &gt; 0) // do this oper. 59 times
    {
      unsigned long currentMillis = millis();
  
      if (currentMillis - secMillis &gt; interval)
      {
        tone1.play(NOTE_G5, 200);
        secMillis = currentMillis;
        Scount --; // add 1 to Scount
        digitalWrite(ledPin2, HIGH); // sets the LED on
        delay(10); // waits for a second
        digitalWrite(ledPin2, LOW); // sets the LED off
        delay(10); // waits for a second
        //lcd.clear();
      }
    }
  }
  </code></pre>