lcd works in setup but not loop

Hey guys!

I am building a reverse geocache box using an on-breadboard atmega 328 with Due on it. I ran have run into a slight issue while writing the code. For some reason, my lcd cases to work at the end of the setup function. It shows random scrolling characters for approximately 30 seconds and then displays the correct thing.

Is there a reason its hitting some kind of roadblock in communication for that time and then randomly connecting again? I originally thought it could be loose wires but that was ruled out by it always occurred as setup function ended. I wonder if it could also possibly have something to do with the arduino memory running low but that does not seem possible in this case...

Any suggestions would be greatly appreciated!

#include <LiquidCrystal.h> //include LCD library
#include <Servo.h>  //Include servo library
#include <Time.h>
#include <Flash.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //Sets up pins for LCD
const int buttonPin = 10;
Servo lockservo;                       // create servo object to control a servo 
int LCDbtnlight = 6;
int lock = 0;
int unlock = 170;
int buttonState = 0;                   // variable for reading the pushbutton status

void setup() { 
  lcd.begin(16, 2);
  lockservo.attach(13);            // attaches the servo on pin 13 to the servo object 
  delay(7000);                     // Delay 7 seconds to allow time for the box to be closed. 
  lockservo.write(lock);           // sets the servo position according to the scaled value 
  pinMode(LCDbtnlight, OUTPUT);    //Set up the LCD Backlight and 3 green leds
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(buttonPin, INPUT);       //Tactile on/off button
  
  
  buttonState = digitalRead(buttonPin);
  while (buttonState == LOW) {buttonState = digitalRead(buttonPin);}
  
  
     resetTime();
     digitalWrite(LCDbtnlight, HIGH);   //turn on the lcd backlight as well as the pushbutton's backlight
     lcd.clear();
     
     FLASH_STRING_ARRAY(intro, 
     PSTR("Hi -------"),
     PSTR(""),
     PSTR("Inside of me is"),
     PSTR("a gift for you!"),
     PSTR("Why am I locked?"),
     PSTR("Great Question!"),
     PSTR("------- is pretty"),
     PSTR("smart, he put a"),
     PSTR("GPS inside of me"),
     PSTR(""),
     PSTR("For me to unlock"),
     PSTR("you'll have to bring me!"),  
     PSTR("Where?"),  
     PSTR("You'll find out soon enough"),  
     PSTR("Let's go!>>>>>>>>>>>>>>>"));
     
     for (int i=0; i<intro.count(); i=(i+2)) {
       lcd.setCursor(0,0);
       intro[i].print(lcd);
       if (intro[i].length() >= 17) {
         delay(2000);
         for (int positionCounter = 0; positionCounter < ((intro[i].length())-16); positionCounter++) { //will make the program figure out the amount to scroll by string size
           lcd.scrollDisplayLeft(); 
           delay(200);
         }
       }
       
       lcd.setCursor(0,1);
       intro[i+1].print(lcd);
       if (intro[i+1].length() >= 17) {
         delay(2000);
         for (int positionCounter = 0; positionCounter < ((intro[i+1].length())-16); positionCounter++) { //will make the program figure out the amount to scroll by string size
           lcd.scrollDisplayLeft(); 
           delay(200);
         }
       }
       
       delay(3000);
       lcd.clear();
     }

  }
  
  



  
void loop() { 
  


  lcd.print("5"); //this is test code to just put on the LCD. Instead of showing 555555555555555 etc, 
                  //it shows random characters for ~30s and then it finally shows the corect 555555555555 lines.
                              
                  




                  
  if (minute() >= 2 && digitalRead(buttonPin) == LOW){  //Standby countdown and initiation
     
       for (int i=0; i <= 20; i++){
         lcd.clear();
         lcd.setCursor(0,0);
         lcd.print("Going on Standby");
         lcd.setCursor(0,1);
         lcd.print(20-i);
         lcd.setCursor(4,1);
         lcd.print("seconds");
         if (digitalRead(buttonPin) == HIGH) {
           resetTime();
           break;
           }
         delay(1000);
         }
     lcd.clear();
     digitalWrite(LCDbtnlight, LOW);
     digitalWrite(7, LOW);
     digitalWrite(8, LOW);
     digitalWrite(9, LOW); 
     
    }
   }
   
    
 void resetTime(){ //Resets the time in the timer
 setTime(0, 0, 0, 1, 1, 2014);  
 }

On every itteration of the loop you do this

 void resetTime(){ //Resets the time in the timer
 setTime(0, 0, 0, 1, 1, 2014);

So your time never reaches the 2 minutes

I suspect that the random characters you describe are the processor trying to print the messages in the FLASH_STRING_ARRAY called intro. I don't really have a good reason to think that. Here are my crummy reasons:

You don't mention that the lines of intro show up on the LCD.
There's a delay(2000) for each line of intro, and there are 15 lines. That'd make 30 seconds in all. You mention 30 seconds of gibberish, followed by the 5's that you print as a test. At least, I think that's what you're saying.

You didn't post a link to the flash library, but I think it's here: Flash | Arduiniana. Please note this exchange in the comments, from 8 months ago:

charles:
is the library working with arduino due ?
thanks charles

Mistral:
Hi Charles–
No, it doesn’t work on Due. I hadn’t thought to port it because the Due has so much RAM already, but I ought to make it at least compile correctly… :slight_smile:

I can't tell if the author, or someone else, has gotten the program to work on the Due, and I can't tell if you got the library from some other source. But, it looks like at least some versions don't work on the Due. Mistral has a point: there's not much point in putting things in flash on the Due, since it has a whole bunch of RAM. You might try eliminating the flash library, storing things in RAM, and seeing whether things improve. If you're running this on a Due, that is.

I find this text hard to interpret:

jroaz:
... an on-breadboard atmega 328 with Due on it

Can you clarify that? What device is this code running on?

Please read the sticky post, "How to use this forum - please read," listed at the top of the listings for each section of the forum.