LCD With Relay, RTC, And Button Press

I have an arduino mega 2560 set up with a lcd, rtc, relay, and button. Basically what I am trying to do is set the LCD to print the time. Control a light with the relay to come on a certain time and have an override button. What I am having problems with is sometimes when I use the button to override the light timer the LCD displays garbled characters and for some reason I cannot get the timer to turn the light on properly with a delay.

Here is my code:

#include <Wire.h>
#define DS1307_I2C_ADDRESS 0x68
#include <LiquidCrystal.h> // we need this library for the LCD commands
LiquidCrystal lcd(12, 11, 6, 5, 4, 3);
byte decToBcd(byte val)
{
return ( (val/1016) + (val%10) );
}
byte bcdToDec(byte val)
{
return ( (val/16
10) + (val%16) );
}
// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers
void setDateDs1307(byte second, // 0-59
byte minute, // 0-59
byte hour, // 1-23
byte dayOfWeek, // 1-7
byte dayOfMonth, // 1-28/29/30/31
byte month, // 1-12
byte year) // 0-99
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0);
Wire.write(decToBcd(second)); // 0 to bit 7 starts the clock
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour));
Wire.write(decToBcd(dayOfWeek));
Wire.write(decToBcd(dayOfMonth));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.write(0x10); // sends 0x10 (hex) 00010000 (binary) to control register - turns on square wave
Wire.endTransmission();
}
void getDateDs1307(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
// A few of these need masks because certain bits are control bits
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}

int Blue=22;
int switchPin=24;
boolean lastButton=HIGH;
boolean currentButton=HIGH;
boolean ledOn=false;
const int setupdelay=5000;
const long relaydelay=30000;
const int timedelay=1000;
unsigned long previousMillis=0;
unsigned long previousMillis1=0;
unsigned long previousMillis2=0;
unsigned long previousMillis3=0;

void setup()
{

pinMode(Blue, OUTPUT);
digitalWrite(Blue,HIGH);
pinMode(switchPin, INPUT);

Wire.begin();

lcd.begin(20, 4);
lcd.setCursor(0,0);
lcd.print("What Time Is It");
lcd.setCursor(0,1);
lcd.print("Mr.Wolf?");
delay(5000);
lcd.clear(); // clear LCD screen
}
boolean debounce(boolean last)
{
boolean current=digitalRead(switchPin);
if(last!=current)
{
delay(5);
current=digitalRead(switchPin);
}
return current;
}
void loop()
{

byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);

currentButton=debounce(lastButton);
if(lastButton==HIGH && currentButton ==LOW){
ledOn=!ledOn;}
lastButton=currentButton;
digitalWrite(Blue,ledOn);
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);

if((hour==21)&&(minute==58)&&(second==50)){
digitalWrite(Blue,LOW);
unsigned long currentMillis = millis();
if ((currentMillis - previousMillis1) >= relaydelay){
digitalWrite(Blue,HIGH);
previousMillis1 = currentMillis;}
}

unsigned long currentMillis = millis();
if (currentMillis - previousMillis2 >= timedelay) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" ");
switch(dayOfWeek){
case 1:
lcd.print("Sun");
break;
case 2:
lcd.print("Mon");
break;
case 3:
lcd.print("Tue");
break;
case 4:
lcd.print("Wed");
break;
case 5:
lcd.print("Thu");
break;
case 6:
lcd.print("Fri");
break;
case 7:
lcd.print("Sat");
break;
}

const char* AMPM = 0;
if (hour > 12) {
hour -= 12;
AMPM = " PM";
}
else AMPM = " AM";

lcd.print(" ");
lcd.print(month, DEC);
lcd.print("/");
lcd.print(dayOfMonth, DEC);
lcd.print("/20");
lcd.print(year, DEC);

lcd.setCursor(0,1);
lcd.print(" ");
if(hour>12){
lcd.print(hour-12, DEC);
}
else{
lcd.print(hour);
}

lcd.print(":");
if (minute<10)
{
lcd.print("0");
}
lcd.print(minute, DEC);
lcd.print(":");
if (second<10)
{
lcd.print("0");
}
lcd.print(second, DEC);
lcd.print(AMPM);

previousMillis2 = currentMillis;}

}

if you want LED(relay) on at 21:58:50 for 30 sec, and switch LED(relay) state by button any time - try this one.

P.S. try to format your code, and use CODE in message editor:)

#include <Wire.h>
#define DS1307_I2C_ADDRESS 0x68
#include <LiquidCrystal.h> // we need this library for the LCD commands
LiquidCrystal lcd(12, 11, 6, 5, 4, 3);
byte decToBcd(byte val)
{
return ( (val/10*16) + (val%10) );
}
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}
// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers
void setDateDs1307(byte second, // 0-59
byte minute, // 0-59
byte hour, // 1-23
byte dayOfWeek, // 1-7
byte dayOfMonth, // 1-28/29/30/31
byte month, // 1-12
byte year) // 0-99
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(0);
  Wire.write(decToBcd(second)); // 0 to bit 7 starts the clock
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hour));
  Wire.write(decToBcd(dayOfWeek));
  Wire.write(decToBcd(dayOfMonth));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));
  Wire.write(0x10); // sends 0x10 (hex) 00010000 (binary) to control register - turns on square wave
  Wire.endTransmission();
}

void getDateDs1307(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
  // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(0);
  Wire.endTransmission();
  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
  // A few of these need masks because certain bits are control bits
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}

int Blue=22;
int switchPin=24;

// so, LOW/HIGH or false/true ? :)
boolean lastButton=HIGH;
boolean currentButton=HIGH;
boolean ledOn=LOW;

//boolean ledOn=false;


const int setupdelay=5000;
const long relaydelay=30000;
const int timedelay=1000;
unsigned long previousMillis=0;
unsigned long previousMillis1=0;
unsigned long previousMillis2=0;
unsigned long previousMillis3=0;

// to trace state
boolean relay_on = 0;

void setup()
{

  
  pinMode(Blue, OUTPUT);
  digitalWrite(Blue,HIGH);
  pinMode(switchPin, INPUT);
  
  Wire.begin();

  lcd.begin(20, 4);
  lcd.setCursor(0,0);
  lcd.print("What Time Is It");
  lcd.setCursor(0,1);
  lcd.print("Mr.Wolf?");
  delay(5000);
  lcd.clear(); // clear LCD screen

}

boolean debounce(boolean last)
{
  boolean current=digitalRead(switchPin);
  if(last!=current)
  {
    delay(5);
    current=digitalRead(switchPin);
  }
  return current;
}


void loop()
{

// why every loop iteration?
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);

unsigned long currentMillis = millis();


  currentButton=debounce(lastButton);
  
  //if(lastButton==HIGH && currentButton ==LOW)
  
  // try it simplier
  if(lastButton && !currentButton) 
  {
    ledOn = !ledOn;

    // disable relay timeout check if button pressed;
    relay_on = 0;
  }


  lastButton = currentButton;

  digitalWrite(Blue,ledOn);



// for what reason second time call?
//getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);



// will not work this way, because on next iteration will be still 50 sec, but relay delay active. so, level will be LOW!!


if((hour==21)&&(minute==58)&&(second==50))
{
	previousMillis1 = currentMillis;
	relay_on = 1;
	digitalWrite(Blue,HIGH);  
}


if ((currentMillis - previousMillis1) > relaydelay && relay_on)
{
  digitalWrite(Blue,LOW);
  relay_on = 0;
} 
  




  
// better use once at the loop start  
//unsigned long currentMillis = millis();

if (currentMillis - previousMillis2 >= timedelay) 
{
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(" ");
 
  char DoW[8][4] = {"\0","Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; 
  lcd.print(DoW[dayOfWeek]);

/*
  switch(dayOfWeek){
  case 1:
  lcd.print("Sun");
  break;
  case 2:
  lcd.print("Mon");
  break;
  case 3:
  lcd.print("Tue");
  break;
  case 4:
  lcd.print("Wed");
  break;
  case 5:
  lcd.print("Thu");
  break;
  case 6:
  lcd.print("Fri");
  break;
  case 7:
  lcd.print("Sat");
  break;
  }
*/
  const char* AMPM = 0;
  if (hour > 12) 
  {
    hour -= 12;
    AMPM = " PM";
  }
  else 
  	AMPM = " AM";
    
	lcd.print(" ");
	lcd.print(month, DEC);
	lcd.print("/");
	lcd.print(dayOfMonth, DEC);
	lcd.print("/20");
	lcd.print(year, DEC);

	lcd.setCursor(0,1);
	lcd.print(" ");

	/* Another AM/PM check?
	 if(hour>12)
	 {
	   lcd.print(hour-12, DEC);
	 }
	 else
	 {
	   lcd.print(hour);
	 }
   */

   lcd.print(hour);

   lcd.print(":");

	if (minute<10)
	{
	lcd.print("0");
	}
	lcd.print(minute, DEC);
	lcd.print(":");
	if (second<10)
	{
	lcd.print("0");
	}
	lcd.print(second, DEC);
	lcd.print(AMPM);

	previousMillis2 = currentMillis;
}


}

Thanks for your help! I am still learning and piecing how the code all fits together.

i need help for coding

i want to making lpg detect i want to showing 1 to 10000 ppm on screen but
control relay by manually set 1 to 10000 ppm some like buzzer