Do the example sketches use this function?
I have not found any so far.
Most seem to be concerned with the alarm system.
mattbaum:
I have not found any so far.
I don't have that library installed. But on its GitHub Page that function is used in 4 of the 7 examples:
ALog_DS3231_set_echo
DS3231_test
echo_time
set_echo
Thanks - I'll look into those examples.
Meanwhile,
I think I finally figured out how to use this function:
(Clock.getHour(h12, PM), DEC);
This seems to compile and to return the values h12 and PM.
I did this based on something that worked previously.
(Why this particular format, I don't know.)
Also, I thought that h12 was a flag, and not the actual hours register.
After dinner I'll see if it yields sensible results!
I was wrong in the above (Clock.getHour(h12, PM), DEC); This does NOT return the time in h12.
I looked into the examples cited, with the following results:
ALog_DS3231_set_echo
Serial.print(Clock.getHour(h12, PM), DEC); //24-hr in line 158 produces a value for h12, which is not hours.
DS3231_test
has a read function Serial.print(Clock.getHour(h12, PM), DEC); same as above, which does not seem to return hours.
It has no write function to set the hours.
now.pde (not referenced) has in it:
DateTime now = RTC.now();
Serial.print(now.hour(), DEC);
This produces a value of 10, which I cannot change. It seems to be reading hours OK, but writing to hours is the problem.
echo_time - has the same (unworkable) get, but no set.
set_echo - has the line Clock.setHour(Hour); - which does not seem to produce a change in hours.
As a test, I'm reading the hours, incrementing the value returned, and writing the new hours back.
On the next pass I expect to see the new value - but I find no change.
So I seem to be reading the hours, but not able to write to them. Any further ideas?
The library repository certainly suggests that setHour should work, I'd suggest you post your test code.
Post your latest complete code that does not work the way you want. If this code is too full of unrelated clutter, post an MCVE.
Here it is -
It compiles and runs, but does NOT increment the hours!
// This used for hour test only.
//==================
#include <DS3231.h>
#include <Wire.h>
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
#include <SPI.h> //Serial Peripheral Interface
//#include <Time.h> // should include //#include <TimeLib.h>
// #include <RTCLib.h>
//#include <CurieTime.h>
//#include <RTC.h>
//SDA = 19=A5; SCL = 18=A4
DS3231 Clock; // Not including this was the problem!!
// Clock.begin(); // prevents compile
bool Century = false;
byte Year;
byte Month;
byte Date;
byte DoW;
byte Hour;
byte Minute;
byte Second;
bool h12;
bool PM;
int hr;
int Dow;
// For LCD:
//======================== stuff before setup
const int rs = 8, en = 9, d4 = 5, d5 = 4, d6 = 3, d7 = 2; //rs was originally pin 10. (either works OK)
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); //pins 8,9 work the lcd
byte getYear();
byte getMonth();
byte getDate();
// Example has these before setup:
int swPlusPin = 15; // The + switch is wired to Pin 15 = A1
int swMinusPin = 16; // The - switch is wired to Pin 16 = A2 // Inpt_Pullup MUST be placed after Setup()!!
void setup() {
Wire.begin();
Serial.begin(9600);
Serial.print("Starting Hours Test:");
Serial.println('/n'); // What is the "12142" which appears after the "newline" command?
pinMode(swPlusPin, INPUT_PULLUP); // A1 = 15 sw1 - Does not compile if these are placed befor setup()
pinMode(swMinusPin, INPUT_PULLUP); // A2 = 16 sw2
// For lcd:
lcd.begin(16, 2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display }
// lcd display is:(Column, Row)
} // end of setup - ready for loop
void loop() { // ===========================================================
lcd.clear();
Clock.setClockMode(false); // set to 24 hour (true for 12 hour)
Serial.print("Hr=");
lcd.print("H");
//(Clock.getHour(h12, PM), DEC); // 24 hour
//+++++++++++++
Serial.print(Clock.getHour(h12, PM), DEC);
(Clock.getHour(h12, PM), DEC);
lcd.print(h12);
lcd.print(PM);
lcd.print(" ");
RTClib RTC;
DateTime now = RTC.now();
hr = (now.hour(), DEC);
Serial.print(hr);
lcd.print(hr);
Serial.print("N");
lcd.print("N");
hr = hr + 1;
//lcd.print(hr);
lcd.print(" ");
lcd.print("X");
Serial.print(" New hr = ");
Serial.print(hr);
lcd.print(hr);
// Now put the new, incremented hour back to the clock
Hour = hr;
Clock.setHour(Hour);
// Read back the incremented hour
hr = (now.hour(), DEC);
lcd.print(" ");
lcd.print(hr);
lcd.print("D");
Serial.print(" Gotten hr = ");
Serial.print(hr);
Serial.print(" ");
Serial.println('/n'); // What aare the funny numbers (12142) printed here??
delay(1000);
} // end of loop - go again with new hour
hr = (now.hour(), DEC);Let me guess - it is always ten o'clock.
AWOL:
hr = (now.hour(), DEC);Let me guess - it is always ten o'clock.
I suspected as much from the snippets, but needed full code to confirm.
OP, read about the Comma Operator. Then, stop using it.
So, you're trying to read the hours two different ways -- both incorrectly.
First, as @AWOL pointed out, you're using the Comma Operator to throw away the results from the call to 'now.hour()'.
Second, you're suffering under a misconception of how 'Clock.getHour()' works. You posted its prototype in Reply #19, and then ignored it:
byte getHour(bool& h12, bool& PM_time);
So, it returns a byte. That's the value you want to tell you the hours (except if you again throw it away with the Comma Operator). Quit obsessing about 'h12' and 'PM_time'. You can see in the prototype they are of type 'bool'. You can also see that they are passed to the function by reference. That usually means they will be set by the function you're calling. It's a slick C++ technique to return more than one value from a function. You couldn't do that in good old C. But, in this case, you want the "traditional" return value (i.e. the byte).
Thanks for all the help.
I now have the incrementing of hours working, using
hr = now.hr();
and I understand why the "now" function wouldn't work before.
However I have two questions - for my own education.
-
Why in the original "now" function that I blindly copied was the " , DEC" used? Where should that kind of structure be used, and what does it do?
-
In the original line (which I also blindly copied but no longer need because of the "now" function work-around))
byte getHour(bool& h12, bool& PM_time);
how do I access the "byte" that is returned?
I tried saying hr = getHour(bool& h12, bool& PM_time);
but this will not compile.
I'm trying to learn the Arduino language while developing this project. It has been both interesting and frustrating.
My prior experience has all been with assembly languages.
Explanations will be appreciated, even though I think that my immediate problem has been solved with the "now" function work-around.
TIA - Matt
mattbaum:
- Why in the original "now" function that I blindly copied was the " , DEC" used? Where should that kind of structure be used, and what does it do?
See the Wikipedia link for the Comma Operator that I supplied. From that page:
The use of the comma token as an operator is distinct from its use in function calls and definitions, variable declarations, enum declarations, and similar constructs, where it acts as a separator.
This:
Serial.print(now.hour(), DEC);
Is a function call to the 'print()' method on the object 'Serial'. The call supplies two arguments to the function:
-
The return result of the call to the 'now.hour()' function. This is the byte containing the hour you want to print.
-
The symbol 'DEC'. This has a value of 10. It tells the print function to print in decimal representation. It was totally superfluous since that function prints in decimal by default. The code's author would have been well-advised to leave it out.
So, bottom line, in the ' Serial.print()' function call the comma (',') acts a separator between the arguments (as mentioned on the Wikipedia page).
This, however:
hr = (now.hour(), DEC);
is an assignment statement. It assigns the value on the right hand side of the '=' to the variable on the left hand side. That value happens to be the result of your use of the Comma Operator (again, see Wikipedia page). The 'now.hour()' function is called and its return value promptly discarded by the Comma Operator which then returns the value that follows the ','. This is DEC which has a value of 10.
- In the original line (which I also blindly copied but no longer need because of the "now" function work-around))
byte getHour(bool& h12, bool& PM_time);
how do I access the "byte" that is returned?
I tried saying hr = getHour(bool& h12, bool& PM_time);
but this will not compile.
Why did you put the argument types in the function call? You didn't do that for any other functions that you used. Why this one?
-
I'm still trying to digest the Wikipedia item on this - although I think I get the general idea.
-
As I said, I had simply copied these two lines from example programs that were supposed to work. I didn't understand the logic behind them, and still don't with this one.
I now realize that the line
byte getHour(bool& h12, bool& PM_time);
returns three variables, of which the only one I want is the "byte" with the hours.
However, I still don't understand how I access this byte.
When I tried saying hr = getHour();
the compiler complains "too few elements."
The same with hr = (get.Hour());
There is something major that I am missing!
hr = Clock.getHour(h12, PM);
Thank you SOOOOOO much!
I thought that I had tried that a long time ago, but I guess I was misled by the other two values.
Now all seems to be OK and working, so I can continue.
I also have a little bit better understanding of some of the language.
Thanks again -
- Matt