Please HELP!

Hello Friends!
I’m working on this clock code and I need some help.
I can’t find a way to get rid off ghost characters every time I display.
For example, when i press the button to get Temperature and Humidity information when the code goes back to displaying time I get ghost character. I try using lcd.clear(). but I get pulsing text and if I add a delay() the set alarm doesn’t response.

my question is: How can I clear the screen and between information without the pulsing.

thanks in advance.

#include <LiquidCrystal_I2C.h>
#include <dht.h>
#include "Wire.h"
#include <RTClib.h>
#define DS3231_I2C_ADDRESS 0x68

RTC_DS1307 rtc;
DateTime now;

boolean displayAlarmSet = false;

boolean alarm = false;
boolean armed = false;

int alarmHrs = 12;
int alarmMins = 00;

const int tempButton = 4;
const int alarmSetPin = 5;
const int incrementAlarmHrsPin = 6;
const int incrementAlarmMinsPin = 7;

const int piezoPin = 11;
const int alarmArmedPin = 10;




LiquidCrystal_I2C lcd(0x3F, 16, 2);

#define DHT11_PIN 2
dht DHT;

char weekDay[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };





void setup(){

  Wire.begin();
  rtc.begin();
  lcd.init();
  lcd.backlight();
  pinMode(tempButton, INPUT);
  pinMode(alarmSetPin, INPUT);
  pinMode(incrementAlarmHrsPin, INPUT);
  pinMode(incrementAlarmMinsPin, INPUT);
  pinMode(piezoPin, OUTPUT);
  
  
}

void updateDisplay(){

  if (displayAlarmSet){
    lcd.setCursor(0,0);
    lcd.print("Set alarm time");
    lcd.setCursor(4, 1);
    lcd.print(" ");
    lcd.setCursor(5,1);

    if (alarmHrs < 10){
      lcd.print(0);
    }
    lcd.print(alarmHrs);
    lcd.setCursor(7, 1);
    lcd.print(":");
    lcd.setCursor(8, 1);

    if (alarmMins < 10){
      lcd.print(0);
    }
    lcd.print(alarmMins);
    lcd.setCursor(10, 1);
    lcd.print(" ");
  }
  else {

  int h = now.hour();
  int m = now.minute();
  int s = now.second();
  
  int dd = now.dayOfWeek();
  int bb = now.day();
  int mm = now.month();
  int yy = now.year();

  lcd.setCursor(0,0);
  lcd.print("                ");
  lcd.setCursor(0,0);
  lcd.print(weekDay[dd]);
  lcd.setCursor(7, 0);
  lcd.print(bb);
  lcd.print("/");
  lcd.print(mm);
  lcd.print("/");
  lcd.print(yy);
  
  
 
  lcd.setCursor(4, 1);

  if( h < 10){
    lcd.print(0);
 }

  lcd.print(h);
  lcd.setCursor(6, 1);
  lcd.print(":");
  lcd.setCursor(7, 1);
  
  if (m < 10){
    lcd.print(0);
 }

   lcd.print(m);
   lcd.setCursor(9, 1);
   lcd.print(":");
   lcd.setCursor(10, 1);

   if (s < 10){
    lcd.print(0);
  }

   lcd.print(s);
 
}

}


void checkAlarm(){
  if (alarmHrs == now.hour() && alarmMins == now.minute() && now.second() == 0){

    alarm = true;
  }
}

void setAlarm(){
  int hrs = digitalRead(incrementAlarmHrsPin);
  int mins = digitalRead(incrementAlarmMinsPin);

  if ( hrs == HIGH){
    alarmHrs += 1;
    delay(200);
    if (alarmHrs > 23){
      alarmHrs = 0;
    }
  }

  if ( mins == HIGH){
    alarmMins += 1;
    delay(200);
    if (alarmMins > 59){
      alarmMins = 0;
    }
      
  }
}

void soundAlarm(){
  
  float alarmFrequency = 1400;
  float period = (1.0 / alarmFrequency)* 1000000;
  long beepDuration = 250000;
  long elapseTime = 0;

  while (elapseTime < beepDuration){
    digitalWrite(piezoPin, HIGH);
    delayMicroseconds(period / 2);
    digitalWrite(piezoPin, LOW);
    delayMicroseconds(period / 2);
    elapseTime += (period);
  }
  digitalWrite(piezoPin, LOW);
  delayMicroseconds(beepDuration);

  int hrs = digitalRead(incrementAlarmHrsPin);
  int mins = digitalRead(incrementAlarmMinsPin);

  if (hrs == HIGH || mins == HIGH){
    alarm = false;
  }
}

void displayTemp(){

  int chk = DHT.read11(DHT11_PIN);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("                ");
  lcd.setCursor(0,0);
  lcd.print("Temp :");
  lcd.print(DHT.temperature);
  lcd.print(" C");
  lcd.setCursor(0,1);
  lcd.print("                  ");
  lcd.setCursor(0,1);
  lcd.print("Hum  :");
  lcd.print(DHT.humidity);
  lcd.print(" %");

  delay(2000);
}


 void loop(){
 
 if (digitalRead(tempButton) == HIGH){
  
  displayTemp();
 }
 else 
 {

  now = rtc.now();
  int alarmArmed = digitalRead(alarmArmedPin);

  if(alarmArmed == HIGH){
    armed = true;
  }else{
    armed = false;
  }

  if (armed){
    if (!alarm){
      checkAlarm();
    }
    else{
      soundAlarm();
    }
  }

  int setMode = digitalRead(alarmSetPin);
  if ( setMode == HIGH){
    displayAlarmSet = true;

    setAlarm();
  }
  else {
    displayAlarmSet = false;
  }


  updateDisplay();
 }
 }

my question is: How can I clear the screen and between information without the pulsing.

Basically you can't. The best you can do is to only refresh the parts of the screen where the information changes.

You only need to update the screen once or twice a second.

.

Before you display the time you have to clean out the display by adding this in updateDisplay() :

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

just after you do this:

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

This ensures that anything left in the display after you have displayed the temperature is overwritten with blanks.

This is what I did and it seems to work a lot better.
Now the problem I have I can’t figure out how to make the alarm sound I set the alarm to the time I need it to go off but it doesn’t do it.

thanks everybody for the response.

#include <LiquidCrystal_I2C.h>
#include <dht.h>
#include "Wire.h"
#include <RTClib.h>
#define DS3231_I2C_ADDRESS 0x68

RTC_DS1307 rtc;
DateTime now;

boolean displayAlarmSet = false;

boolean alarm = false;
boolean armed = false;

int alarmHrs = 12;
int alarmMins = 00;

const int displayDatePin = 4;
const int alarmSetPin = 5;
const int incrementAlarmHrsPin = 6;
const int incrementAlarmMinsPin = 7;

const int piezoPin = 11;
const int alarmArmedPin = 8;




LiquidCrystal_I2C lcd(0x3F, 16, 2);

#define DHT11_PIN 2
dht DHT;

char weekDay[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };




void setup(){

  Wire.begin();
  rtc.begin();
  lcd.init();
  lcd.backlight();
  pinMode(displayDatePin, INPUT);
  pinMode(alarmSetPin, INPUT);
  pinMode(incrementAlarmHrsPin, INPUT);
  pinMode(incrementAlarmMinsPin, INPUT);
  pinMode(piezoPin, OUTPUT);
  pinMode(alarmArmedPin, INPUT);
  
  
}

void updateDisplay(){

  if (displayAlarmSet){
    lcd.setCursor(0,0);
    lcd.print("Set alarm time");
    lcd.setCursor(0, 1);
    lcd.print("                 ");
    lcd.setCursor(5,1);

    if (alarmHrs < 10){
      lcd.print(0);
    }
  
    lcd.print(alarmHrs);
    lcd.setCursor(7, 1);
    lcd.print(":");
    lcd.setCursor(8, 1);

    if (alarmMins < 10){
      lcd.print(0);
    }
    lcd.print(alarmMins);
    lcd.setCursor(10, 1);
    lcd.print(" ");
  }
  else {

  int h = now.hour();
  int m = now.minute();
  int s = now.second();
  int dd = now.dayOfWeek();
  
  lcd.setCursor(0,0);
  lcd.print("                ");
  lcd.setCursor(0,0);
  lcd.print(weekDay[dd]);
  
  lcd.setCursor(6, 0);

  if( h < 10){
    lcd.print(0);
 }

  lcd.print(h);
  
  lcd.print(":");
 
  
  if (m < 10){
    lcd.print(0);
 }

   lcd.print(m);
  
   lcd.print(":");
   

   if (s < 10){
    lcd.print(0);
  }

   lcd.print(s);
 

int chk = DHT.read11(DHT11_PIN);
  
  
  lcd.setCursor(0,1);
  lcd.print("                ");
  lcd.setCursor(0,1);
  lcd.print(DHT.temperature);
  lcd.print(" C");
  
  
  lcd.setCursor(9,1);
  lcd.print(DHT.humidity);
  lcd.setCursor(15, 1);
  lcd.print("%");
 
  delay(1000);
}
}

void checkAlarm(){
  if (alarmHrs == now.hour() && alarmMins == now.minute() && now.second() == 0){

    alarm = true;
  }
}

void setAlarm(){
  int hrs = digitalRead(incrementAlarmHrsPin);
  int mins = digitalRead(incrementAlarmMinsPin);

  if ( hrs == HIGH){
    alarmHrs += 1;
    delay(200);
    if (alarmHrs > 23){
      alarmHrs = 0;
    }
  }

  if ( mins == HIGH){
    alarmMins += 1;
    delay(200);
    if (alarmMins > 59){
      alarmMins = 0;
    }
      
  }
}

void soundAlarm(){
  
  float alarmFrequency = 1400;
  float period = (1.0 / alarmFrequency)* 1000000;
  long beepDuration = 250000;
  long elapseTime = 0;

  while (elapseTime < beepDuration){
    digitalWrite(piezoPin, HIGH);
    delayMicroseconds(period / 2);
    digitalWrite(piezoPin, LOW);
    delayMicroseconds(period / 2);
    elapseTime += (period);
  }
  digitalWrite(piezoPin, LOW);
  delayMicroseconds(beepDuration);

  int hrs = digitalRead(incrementAlarmHrsPin);
  int mins = digitalRead(incrementAlarmMinsPin);

  if (hrs == HIGH || mins == HIGH){
    alarm = false;
  }
}




void displayDate(){
 
  
  int bb = now.day();
  int mm = now.month();
  int yy = now.year();

  lcd.setCursor(0,1);
  lcd.print("                ");
  lcd.setCursor(4, 1);
  lcd.print(bb);
  lcd.print("/");
  lcd.print(mm);
  lcd.print("/");
  lcd.print(yy);

  delay(1000);
}


 void loop(){
 
 if (digitalRead(displayDatePin) == HIGH){
  
  displayDate();
 }
 else 
 {

  now = rtc.now();
  int alarmArmed = digitalRead(alarmArmedPin);

  if(alarmArmed == HIGH){
    armed = true;
  }else{
    armed = false;
  }

  if (armed){
    if (!alarm){
      checkAlarm();
    }
    else{
      soundAlarm();
    }
  }

  int setMode = digitalRead(alarmSetPin);
  if ( setMode == HIGH){
    displayAlarmSet = true;

    setAlarm();
  }
  else {
    displayAlarmSet = false;
  }
 
  updateDisplay();
 }
 }

Use print statements to see where the code is going. I suspect the now.second is not returning zero, at the time you are looking at it, especially with a one second delay in the loop,