Problems with the seconds

Hi All
Complete and utter newbie here so please be gentle. I am following Paul Whorter's series on Utube and have completed the DT11 sensor project. I added the LCD and all was good but i wanted to add a RTC. I had a spare 20,4 LCD and wanted to add the time and date and just keep Humidity and temp in deg C. I got all this working but i am having a problem with the seconds in the time. It will start to count normally but when it gets to 50 seconds, will randomly start counting BUT keeps the correct hours and minutes.

#include <LiquidCrystal.h>
#include <virtuabotixRTC.h>
#include "DHT.h"
#define Type DHT11
virtuabotixRTC myRTC(3, 4, 5);


int rs = 7;
int en = 8;
int d4 = 9;
int d5 = 10;
int d6 = 11;
int d7 = 12;
LiquidCrystal lcd (rs, en, d4, d5, d6, d7);
int sensePin = 2;
DHT HT(sensePin, Type);
float humidity;
float tempC;
float tempF;
int setTime = 500;



void setup() {
  Serial.begin(9600);
  HT.begin();
  delay(setTime);
  lcd.begin(20, 4);

}


void loop() {

  myRTC.updateTime();
  lcd.setCursor(0, 0);
  lcd.print(myRTC.dayofmonth);
  lcd.print("/");
  lcd.print(myRTC.month);
  lcd.print("/");
  lcd.print(myRTC.year);
  lcd.print(" ");
  lcd.print(myRTC.hours);
  lcd.print(":");
  lcd.print(myRTC.minutes);
  lcd.print(":");
  lcd.print(myRTC.seconds);
  delay(1000);

  humidity = HT.readHumidity();
  tempC = HT.readTemperature();
  tempF = HT.readTemperature(true);

  lcd.setCursor(0, 1);
  lcd.print("Humidity= ");
  lcd.print(humidity);
  lcd.print(" %");
  lcd.setCursor(0, 2);
  lcd.print("Temp C= ");
  lcd.print(tempC);

  Serial.print(" Humidity ");
  Serial.print(humidity);
  Serial.print(" Temperature C ");
  Serial.print(tempC);
  Serial.print(" C ");
  Serial.print(tempF);
  Serial.println(" F ");

}

I believe the error is where i have combined the 2 sets of code but i don't have the experience to de-bug yet.
Please just point me in the right direction and let me try to fix it myself.
Many Thanks
Ian

I think the issue is you haven't cleared the LCD screen before writing the numbers to it, so that after displaying

23:59:59

for instance you'll see

0:0:0:59

as the previous digits are still on the display and you only have 5 characters in the new prints
compared to 8 for the previous.

You could also solve this by forcing two digits for the hours/minutes/seconds fields:

  lcd.print(myRTC.hours/10);
  lcd.print(myRTC.hours%10);
  lcd.print(":");
  lcd.print(myRTC.minutes/10);
  lcd.print(myRTC.minutes%10);
  lcd.print(":");
  lcd.print(myRTC.seconds/10);
  lcd.print(myRTC.seconds%10);

Many Thanks Mark

I changed the code to this and it is working great;

void loop() {

  myRTC.updateTime();
  lcd.setCursor(0, 0);
  lcd.print(myRTC.dayofmonth);
  lcd.print("/");
  lcd.print(myRTC.month);
  lcd.print("/");
  lcd.print(myRTC.year);
  lcd.print(" ");
  lcd.print(myRTC.hours/10);
  lcd.print(myRTC.hours%10);
  lcd.print(":");
  lcd.print(myRTC.minutes/10);
  lcd.print(myRTC.minutes%10);
  lcd.print(":");
  lcd.print(myRTC.seconds/10);
  lcd.print(myRTC.seconds%10);
  delay(1000);

I did not have to add a "clear" function.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.