Cannot get code to write LCD unless Serial monitor is opened

Hello guys!

I´ve been grinding on this project for the last few days, and as always, my projects consists of cut&paste from things I need.

So you can view this code as a "bastard" code :stuck_out_tongue:

The code is running perfectly with the computer plugged into the arduino and as long as the serial monitor i opened/up.

If I recycle power to the arduino the code refuses to print anything to the LCD if the serial monitor is not opened.

As soon as I open the Serial Monitor, the LCD shows the desired data, and I can close down the serial monitor and even disconnect the computer entirely, and the code still works.

As per now, I need to open up the serial monitor Each time I recycle the power on the arduino.
This is very frustrating for me, so please help.

See code for troubleshooting.

Thanks in advance

/*
  Doorbell immobilizer dependent on clock-time
*/

/////////////PIN CONFIG ARDUINO MICRO////////////////
//RTC Pin       MICRO PIN
//    SCL <-->  3
//    SDA <-->  2


/////////////PIN CONFIG ARDUINO UNO////////////////
//RTC Pin       UNO PIN
//    SCL <-->  A5
//    SDA <-->  A4

//DS1307 from Adafruit has I2C address 0x68


//====================================================================================//

// include the library code:
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib

#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>

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

RTC_DS1307 rtc;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
const int ledPin = 13;        // pin that the LED is attached to
int Relay_out = 7;      //define pin 7 as Relay_out static
//int RelayStateOpen = 5;   //too be used if LED is the indication
//int RelayStateClosed = 6; //too be used if LED is the indication


//====================================================================================//

// the setup function runs once when you press reset or power the board
void setup() {
  
  
  // RELAY output/////////////////////////////////
  // initialize digital pin Relay_out as an output.
     pinMode(Relay_out, OUTPUT);

  // LED OUT FROM STATE//////////////////////////
  // initialize the LED pin as an output:
     pinMode(ledPin, OUTPUT);
   //pinMode(RelayStateOpen, OUTPUT);
   //pinMode(RelayStateClosed, OUTPUT);

  // initialize serial///////////////////////////
  // Serial is neccessary for RTC Monitoring
     while (!Serial); // for Leonardo/Micro/Zero

     Serial.begin(57600);
            if (! rtc.begin()) {
               //Serial.println("Couldn't find RTC");
               while (1);
                        }

            if (! rtc.isrunning()) {
               //Serial.println("RTC is NOT running!");
               // following line sets the RTC to the date & time this sketch was compiled
               // rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); //remove comment for activating
               
               // This line sets the RTC with an explicit date & time, for example to set
               // January 21, 2014 at 3am you would call:
               //rtc.adjust(DateTime(2017,04,06,22,44,00));          // rtc.adjust(DateTime(yyyy,mm,dd,hh,mm,ss));remove comment for activating
                }

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

//====================================================================================//

// the loop function runs over and over again forever
void loop() {

      // READ RTC TIME 
         DateTime now = rtc.now();
    
    
    //USE RTC TIME TO DO LOGIC OPERATION

    if (now.hour() >= 17) { 
      digitalWrite(Relay_out, HIGH);
      digitalWrite(ledPin, HIGH);
      digitalWrite(RelayStateOpen, HIGH);
      digitalWrite(RelayStateClosed, LOW);

        //print string, status and time to LCD if condition is met
        
      lcd.clear();                    //Flash LCD screen
      lcd.setCursor(0, 0);            //set cursor to top line
      lcd.print("Doorbell Status:");  //write to lcd
      lcd.setCursor(0, 1);            //set cursor to bottom line
      lcd.print ("ISOLATE");         //write to lcd
      lcd.setCursor(8, 1);            //set cursor to bottom line at column 8
      lcd.print(now.hour(), DEC);     //write hours to lcd
      lcd.print(':');                 //write : to lcd
      lcd.print(now.minute(), DEC);   //write minutes to lcd
      lcd.print(':');                 //write : to lcd
      lcd.print(now.second(), DEC);   //write seconds to lcd
      }
    else { 
      digitalWrite(Relay_out, LOW);
      digitalWrite(ledPin, LOW);
      digitalWrite(RelayStateOpen, LOW);
      digitalWrite(RelayStateClosed, HIGH);

       //print string, status and time to LCD if condition is NOT met
      
      lcd.clear();                    //Flash LCD
      lcd.setCursor(0, 0);            //set cursor to top line
      lcd.print("Doorbell Status:");  //write to lcd
      lcd.setCursor(0, 1);            //set cursor to bottom line
      lcd.print ("NORMAL");           //write to lcd
      lcd.setCursor(8, 1);            //set cursor to bottom line
      lcd.print(now.hour(), DEC);     //write hours to lcd
      lcd.print(':');                 //write : to lcd
      lcd.print(now.minute(), DEC);   //write minutes to lcd
      lcd.print(':');                 //write : to lcd
      lcd.print(now.second(), DEC);   //write seconds to lcd
       }

// write RTC time to serial for debugging purposes

    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(" (");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print("     ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();

    delay(1000);

}
     while (!Serial); // for Leonardo/Micro/Zero

Do you have one of these? If so, why are you waiting for Serial to be ready when you clearly don't want to open the Serial Monitor app, or any other app that talks to the serial port?

If you don't have one of them, why are you including this call?

Don't see how this code compiles with:

digitalWrite(RelayStateOpen, LOW);
digitalWrite(RelayStateClosed, HIGH);

After:

//int RelayStateOpen = 5;   //too be used if LED is the indication
//int RelayStateClosed = 6; //too be used if LED is the indication

Are you sure this is the same code that's running on your Arduino?

Hey guys.

PaulS:

This was something I copied from another sketch i found, and it worked, so I just left it in there as I didtn quite know what the call actually does.

You will have to excuse my ignorance as I´m quite new to arduino.

Anyways, thanks for the answer as this fixed the issue.

756E6C:

You are completely right, I forgot to comment out the digitalWrite sections in both "if" and "else" statements as this corrupts the code and makes it unable to compile.

I forgot to copy the code into the post after these minor changes.

Thanks for pointing it out!