Go Down

Topic: LCD problems printing time with RTC (Read 123 times) previous topic - next topic

albertaborn

I have a project i want to print the RTC time to an LCD. Everything works but at 23:59 when the next minute arrive it prints 03:09 then 03:19 and so on to 03:99 then 03:10. How do i fix this? my code i that i will freely admit is borrowed and i see provision for something like this at the very end with the print digit function but not sure how to incorporate that into my code. Or maybe another function to simply call up the time and print it. idk


Code: [Select]

#include <Wire.h>
#include <LiquidCrystal.h>
#include <Time.h>
#include <TimeLib.h>
#include <DS3232RTC.h>

LiquidCrystal lcd(12, 11, 6, 5, 4, 3);

//First we add the libraries.
//Then we create the datatype integer to read the value from the hours.

int val;

//Next in void setup() we set the solid state relay pin to 7
//at line 14 you can set the time for the RTC.
//You only need to do this once!
//The format is:Hours / minutes / second / day of month / month / year
//After setting time upload the code.
//On the next upload you need to comment out line 14 and 15 shown below.
//setTime(17, 59, 45, 20, 10, 2017);//Hours / minutes / second / day of month / month / year
//RTC.set(now());
//The rtc will remember the time, if you do not comment out line 14 and 15 after setting the time, the RTC will reset every time you upload the code.

void setup()
{
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  Serial.begin(9600);
  pinMode(7, OUTPUT); //relay1
  pinMode(8, OUTPUT); //relay2

  /////////Uncomment both lines below when you want to set the time
  /////////AFTER SETTING TIME COMMENT the lines.
  //setTime(23, 58, 45, 10, 10, 2018);//Hours / minutes / second / day of month / month / year
   //RTC.set(now());

  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if (timeStatus() != timeSet)
    Serial.println("Unable to sync with the RTC");

  else Serial.println("RTC has set the system time");
  lcd.print("RTC set");

}

//At the function void loop() we set the functions digitalClockDisplay(); and AutomaticProgram(); to run forever

void loop()
{
  digitalClockDisplay();
  AutomaticProgram();

}

//At the function void AutomaticProgram() we set the if statements to run the automatic program between 2 values/hours.
//in this example we used the value (hour()); from the function voiddigitalClockDisplay(void){
//The if statements simply read the first value of the output in the serial monitor.
//So in this case if 4 oclock in the midday is the value that hits, the Solid state relay will turn HIGH/ON and turn on the LAMP/Group/Or whatever device you like.
//If the value = 5 the solid state relay will turn LOW/OFF. Default is low.

void AutomaticProgram()
{ //we are using hour as a value the rest is ignored.
  val = (hour()); //Grow/Day - Groei/Dag between 4:00 and 22:00
  if ((20 == val))
  {
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
  }
  else if (val == 21)
  {
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
  }
  else
  {
    digitalWrite(7, LOW);
    digitalWrite(7, LOW);
  }
}

//At the function void digitalClockDisplay(void) { we send the values to the serial monitor.
//You can uncomment all of these and send them to the serial monitor if you like i just chose to use hours());
//as a value to read, the rest will be ignored as you can see in the serial monitor it prints out 4:00:00 for example
//but the code only reads the first value which is hours()); the rest :00:00 is ignored.

void digitalClockDisplay(void)
{
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  // Serial.print(' ');
  // Serial.print(day());
  // Serial.print(' ');
  // Serial.print(month());
  // Serial.print(' ');
  //Serial.print(year());
  Serial.println();

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

  lcd.setCursor(2, 1);
  lcd.print(":");

  lcd.setCursor(3, 1);
  lcd.print(minute());





}

//At the function void printDigits(int digits) we print the digits to the serial monitor.

//You can change line 66 from : to . or , or whatever.

void printDigits(int digits)
{
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(':');//line 66
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

Delta_G

Code: [Select]
lcd.setCursor(0, 1);
  lcd.print(hour());


The problem is that sometimes that prints two digits and sometimes it only prints one.  You need to add an if statement line to see if hour() is less than 10 and if it is then print an extra 0 or a space before you print the hour. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

albertaborn

Thanks. I was thinking this but it does not work either.

Code: [Select]

 lcd.setCursor(0, 1);
  if (hour < 10)
     lcd.print("0");
  lcd.print(hour());


Code: [Select]

Arduino: 1.6.11 (Windows 7), Board: "Arduino BT, ATmega328"

C:\Users\Christian\Documents\Arduino\test_rtc_2b_working_time_and_relay\test_rtc_2b_working_time_and_relay.ino: In function 'void digitalClockDisplay()':

test_rtc_2b_working_time_and_relay:101: error: invalid operands of types '<unresolved overloaded function type>' and 'int' to binary 'operator<'

   if (hour < 10)

              ^

exit status 1
invalid operands of types '<unresolved overloaded function type>' and 'int' to binary 'operator<'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.


Delta_G



You seem to understand here that you need the parenthesis to actually call the function and get the hour here:

Code: [Select]
lcd.print(hour());

So why not here?

Code: [Select]
if (hour < 10)
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Pablo_Lucini


Delta_G

|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

odometer

#6
Oct 12, 2018, 10:23 am Last Edit: Oct 12, 2018, 10:24 am by odometer
Thanks. I was thinking this but it does not work either.

Code: [Select]

 lcd.setCursor(0, 1);
  if (hour < 10)
     lcd.print("0");
  lcd.print(hour());

It looks like you have the right idea, but you are missing the empty parentheses after hour.
It should be: (look carefully at the second line of code here)
Code: [Select]

 lcd.setCursor(0, 1);
  if (hour() < 10)
     lcd.print("0");
  lcd.print(hour());

Go Up