I've connected a real time clock ZS-042 to my Arduino Uno. I tried to set the time, using an example from DS1307RTC library. Here is my code:
#include <DS1307RTC.h>
#include <Time.h>
#include <Wire.h>
*const* char *monthName[12] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
tmElements_t tm;//it says that problem is in here
void setup() {
bool parse=false;
bool config=false;
// get the date and time the compiler was run
if (getDate(__DATE__) && getTime(__TIME__)) {
parse = true;
// and configure the RTC with this info
if (RTC.write(tm)) {
config = true;
}
}
Serial.begin(9600);
while (!Serial) ; // wait for Arduino Serial Monitor
delay(200);
if (parse && config) {
Serial.print("DS1307 configured Time=");
Serial.print(__TIME__);
Serial.print(", Date=");
Serial.println(__DATE__);
} else if (parse) {
Serial.println("DS1307 Communication Error :-{");
Serial.println("Please check your circuitry");
} else {
Serial.print("Could not parse info from the compiler, Time=\"");
Serial.print(__TIME__);
Serial.print("\", Date=\"");
Serial.print(__DATE__);
Serial.println("\"");
}
}
void loop() {
}
bool getTime(const char *str)
{
int Hour, Min, Sec;
if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
tm.Hour = Hour;
tm.Minute = Min;
tm.Second = Sec;
return true;
}
bool getDate(const char *str)
{
char Month[12];
int Day, Year;
uint8_t monthIndex;
if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
for (monthIndex = 0; monthIndex < 12; monthIndex++) {
if (strcmp(Month, monthName[monthIndex]) == 0) break;
}
if (monthIndex >= 12) return false;
tm.Day = Day;
tm.Month = monthIndex + 1;
tm.Year = CalendarYrToTm(Year);
return true;
}
and this is my output:
Arduino: 1.6.12 (Windows 10), Плата:"Arduino/Genuino Uno"
In file included from C:\Users6939円~1\AppData\Local\Temp\arduino_modified_sketch_566010\SetTime.ino:1:0:
C:\Users\папа\Documents\Arduino\libraries\DS1307RTC/DS1307RTC.h:19:22: error: 'tmElements_t' has not been declared
static bool read(tmElements_t &tm);
^
C:\Users\папа\Documents\Arduino\libraries\DS1307RTC/DS1307RTC.h:20:23: error: 'tmElements_t' has not been declared
static bool write(tmElements_t &tm);
^
SetTime:10: error: 'tmElements_t' does not name a type
tmElements_t tm;
^
C:\Users6939円~1\AppData\Local\Temp\arduino_modified_sketch_566010\SetTime.ino: In function 'void setup()':
SetTime:21: error: expected primary-expression before ')' token
if (RTC.write(tm)) {
^
C:\Users6939円~1\AppData\Local\Temp\arduino_modified_sketch_566010\SetTime.ino: In function 'bool getTime(const char*)':
SetTime:54: error: expected unqualified-id before '.' token
tm.Hour = Hour;
^
SetTime:55: error: expected unqualified-id before '.' token
tm.Minute = Min;
^
SetTime:56: error: expected unqualified-id before '.' token
tm.Second = Sec;
^
C:\Users6939円~1\AppData\Local\Temp\arduino_modified_sketch_566010\SetTime.ino: In function 'bool getDate(const char*)':
SetTime:71: error: expected unqualified-id before '.' token
tm.Day = Day;
^
SetTime:72: error: expected unqualified-id before '.' token
tm.Month = monthIndex + 1;
^
SetTime:73: error: expected unqualified-id before '.' token
tm.Year = CalendarYrToTm(Year);
^
exit status 1
'tmElements_t' does not name a type
asked Aug 13, 2017 at 13:44
-
Check the order of header files and check the dependencies. Do you have the right libraries. Looks like you have modified this example sketch; github.com/PaulStoffregen/DS1307RTC/blob/master/examples/…Mikael Patel– Mikael Patel2017年08月13日 14:33:57 +00:00Commented Aug 13, 2017 at 14:33
-
IDE 1.6.10: compilation of sketches wich time library fails001– 0012017年08月13日 14:40:43 +00:00Commented Aug 13, 2017 at 14:40
-
Welcome to Arduino Stack Exchange. You can take the tour at arduino.stackexchange.com/Tour to get the best out of this site.SDsolar– SDsolar2017年08月13日 14:57:01 +00:00Commented Aug 13, 2017 at 14:57
1 Answer 1
Clearly the compiler does not know what tmElements_t means.
It is defined in TimeLib.h
In your program you reference Time.h instead, where it is not defined.
So: Change
include <Time.h>
to
include <TimeLib.h>
and it should clear that error.
answered Aug 13, 2017 at 15:04
-
2
Time.h
was changed toTimeLib.h
specifically to combat this problem. The compiler started providingtime.h
, and on operating systems like Windows that use a case-insensitive filesystem it confusesTime.h
andtime.h
.Majenko– Majenko2017年08月13日 15:22:36 +00:00Commented Aug 13, 2017 at 15:22 -
1Thank you for help! It works! The problem was in TimeLib.h. Literally, it wasn't in my lib, so i had to re-install it. Then I changed include as you said and it works.Anton Sachko– Anton Sachko2017年08月13日 16:00:54 +00:00Commented Aug 13, 2017 at 16:00
-
But for now I've got another trouble. My serial port doesn't print anything.Anton Sachko– Anton Sachko2017年08月13日 16:16:26 +00:00Commented Aug 13, 2017 at 16:16
-
Take out the
while (!Serial)
- once you do theSerial.begin(9600)
you can assume it is ready to receive output. Also, be aware that in the Arduino IDE, opening the serial monitor will restart your program.SDsolar– SDsolar2017年08月13日 18:38:34 +00:00Commented Aug 13, 2017 at 18:38
lang-cpp