DS1302 how to get parts of TimeDate ?

I managed to get the Time Date set and to stop it from resetting it every time I run the program.

Now I am interested in only certain parts of the TimeDate.

I do not find the variable that say denotes the Hours although I opened the .cpp file and the it has this
"t.hr = hourFromRegisterValue(readIn());" I assume this is the Hour variable, but when I put it into the example code that comes with the library "#include <DS1302.h>" it gives me the following error.

Compiling sketch...
"C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10810 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino" "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\eightanaloginputs" "-IC:\Users\Herman\Documents\Arduino\libraries\DS1302" "C:\Users\Herman\AppData\Local\Temp\arduino_build_173247\sketch\1302clockHR.ino.cpp" -o "C:\Users\Herman\AppData\Local\Temp\arduino_build_173247\sketch\1302clockHR.ino.cpp.o"
C:\Users\Herman\My Scetches\1302clockHR\1302clockHR.ino: In function 'void loop()':

1302clockHR:77:16: error: 'hr' was not declared in this scope

Serial.print(hr());

^~

Multiple libraries were found for "DS1302.h"
Used: C:\Users\Herman\Documents\Arduino\libraries\DS1302
Using library DS1302 in folder: C:\Users\Herman\Documents\Arduino\libraries\DS1302 (legacy)
exit status 1
'hr' was not declared in this scope

// Example sketch for interfacing with the DS1302 timekeeping chip.
//
// Copyright (c) 2009, Matt Sparks
// All rights reserved.
//
// http://quadpoint.org/projects/arduino-ds1302
#include <stdio.h>
#include <DS1302.h>

namespace {

// Set the appropriate digital I/O pin connections. These are the pin
// assignments for the Arduino as well for as the DS1302 chip. See the DS1302
// datasheet:
//
//   http://datasheets.maximintegrated.com/en/ds/DS1302.pdf
const int kCePin   = 5;  // Chip Enable
const int kIoPin   = 6;  // Input/Output
const int kSclkPin = 7;  // Serial Clock

// Create a DS1302 object.
DS1302 rtc(kCePin, kIoPin, kSclkPin);

String dayAsString(const Time::Day day) {
  switch (day) {
    case Time::kSunday: return "Sunday";
    case Time::kMonday: return "Monday";
    case Time::kTuesday: return "Tuesday";
    case Time::kWednesday: return "Wednesday";
    case Time::kThursday: return "Thursday";
    case Time::kFriday: return "Friday";
    case Time::kSaturday: return "Saturday";
  }
  return "(unknown day)";
}

void printTime() {
  // Get the current time and date from the chip.
  Time t = rtc.time();

  // Name the day of the week.
  const String day = dayAsString(t.day);

  // Format the time and date and insert into the temporary buffer.
  char buf[50];
  snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d",
           day.c_str(),
           t.yr, t.mon, t.date,
           t.hr, t.min, t.sec);

  // Print the formatted string to serial so we can see the time.
  Serial.println(buf);
}

}  // namespace

void setup() {
  Serial.begin(9600);

  // Initialize a new chip by turning off write protection and clearing the
  // clock halt flag. These methods needn't always be called. See the DS1302
  // datasheet for details.
  //rtc.writeProtect(false);
  //rtc.halt(false);

  // Make a new time object to set the date and time.
  // Sunday, September 22, 2013 at 01:38:50.
  //Time t(2013, 9, 22, 1, 38, 50, Time::kSunday);

  // Set the time and date on the chip.
 //rtc.time(t);
}

// Loop and print the time every second.
void loop() {
  printTime();
  Serial.print(hr());
  delay(1000);
}

I trust I have included all relevant info.

I do not find the variable that say denotes the Hours although I opened the .cpp file and the it has this
"t.hr = hourFromRegisterValue(readIn());" I assume this is the Hour variable, but when I put it into the example code that comes with the library "#include <DS1302.h>" it gives me the following error.

1302clockHR:77:16: error: 'hr' was not declared in this scope
Serial.print(hr());

Are you are using this library GitHub - msparks/arduino-ds1302: Arduino library for the DS1302 Real Time Clock chip

The syntax is t.hr

Why did you use hr() and change the syntax from what the library code indicated with t.hr = hourFromRegisterValue(readIn())?

See how t.hr is used in the snprintf statement in the example you use

char buf[50];
  snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d",
           day.c_str(),
           t.yr, t.mon, t.date,
           t.hr, t.min, t.sec);

Yes that is the Library I am using.

I tried it with the "t.hr()", but then it gives me this error.

I also tried it with "()" and with-out it keeps saying "t was not declared.

1302clockHR:77:16: error: 't' was not declared in this scope

Serial.print(t.hr);

^

Multiple libraries were found for "DS1302.h"
Used: C:\Users\Herman\Documents\Arduino\libraries\DS1302
Using library DS1302 in folder: C:\Users\Herman\Documents\Arduino\libraries\DS1302 (legacy)
exit status 1
't' was not declared in this scope

just get rid of that

Serial.print(t.hr); or

Serial.print(t.hr); from your code

as you don't need serial-print same thing twice

Serial.println(buf); does the printing thing

That Serial.print is just development statement, I want to use the hours to do some calculation, and then I only want to print the Time, not the Time and Date. So needed to know what is the hour variable.

Or how can I get the date and time split up so I can display the time at the top of and OLED and the Date at the bottom?

Your variable 't' is local to the 'printTime()' function. Thus 't', and hence t.hr, is out of scope when you try to access it in the 'loop()' function.

So by knowing "THAT" how do I get to print only the Time part or only the Date part or only the Day part?

I did not write the original code. It is the Example that comes with the Library.

HermanJFourie:
I did not write the original code. It is the Example that comes with the Library.

The example code from the library compiles error-free for me.

Your variable 't' is local to the 'printTime()' function. Thus 't', and hence t.hr, is out of scope when you try to access it in the 'loop()' function.

So by knowing "THAT" how do I get to print only the Time part or only the Date part or only the Day part?

Make the variable local to loop() as well.

void loop() {
  Time t = rtc.time();
  Serial.println(t.hr);
  printTime();
  delay(1000);
}

Thanks, all new to me, so need to get to know about Local variables per loop.

My understanding was if I declare a variable in Setup it can be used by all procedures, but if I declare it in a specific procedure it can only be used in that procedure.

Give me another 100 years and I will get there. :slight_smile:

Thanks, Now it is sorted.

Learnt a lot with one question.

HermanJFourie:
Or how can I get the date and time split up so I can display the time at the top of and OLED and the Date at the bottom?

In the following example, the date and time are in variables; now, you can process/manipulate them if you wish.

date.png
Figure-1:

Codes: (IDE Example + additions-subtractions)

#include <stdio.h>
#include <DS1302.h>

namespace {
const int kCePin   = 5;  // Chip Enable
const int kIoPin   = 6;  // Input/Output
const int kSclkPin = 7;  // Serial Clock
DS1302 rtc(kCePin, kIoPin, kSclkPin);

String dayAsString(const Time::Day day) {
  switch (day) {
    case Time::kSunday: return "Sunday";
    case Time::kMonday: return "Monday";
    case Time::kTuesday: return "Tuesday";
    case Time::kWednesday: return "Wednesday";
    case Time::kThursday: return "Thursday";
    case Time::kFriday: return "Friday";
    case Time::kSaturday: return "Saturday";
  }
  return "(unknown day)";
}
}

void setup()
{
  Serial.begin(9600);
  rtc.writeProtect(false);
  rtc.halt(false);
  Time t(2020, 01, 22, 18, 00, 50, Time::kSunday);
}

void loop()
{
  Serial.println();
  Time t = rtc.time();
  //-----------------------------
  int myYrs = t.yr;
  int myMon = t.mon;
  int myDate = t.date;
  Serial.print("Date: ");
  Serial.print(myYrs); Serial.print('-');
  Serial.print(myMon); Serial.print('-');
  Serial.println(myDate);
  //------------------------------------
  int myHrs = t.hr;
  int myMin = t.min;
  int mySec = t.sec;
  Serial.print("Time: ");
  Serial.print(myHrs); Serial.print('-');
  Serial.print(myMin); Serial.print('-');
  Serial.println(mySec);
  delay(1000);
}

date.png