Hi Dudes,
I'm just trying use RTC_DS3231 in my class. why means in my project I'm turning on/off motor based on time and it will run for configured time duration. So for calculating it I'm using RTC_DS3231.
Implementation:
////// RTC.h
#ifndef _RTC_h
#define _RTC_h
#include "RTCLib.h"
class RTC
{
public:
RTC();
int CurrentHour, CurrentMinute, CurrentDay;
void SetNewTime(DateTime newTime);
void UpdateCurrentTime();
String GetDisplayTime();
RTC_DS3231 rtc;
};
#endif
/// RTC.cpp
#include "RTC.h"
RTC::RTC()
{
if (!rtc.begin())
{
Serial.println("Couldn't find RTC");
while (1);
}
}
void RTC::SetNewTime(DateTime newTime)
{
/*if (rtc.lostPower())
{
Serial.println("RTC lost power, lets set the time!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(DATE), F(TIME)));
// This line sets the RTC with an explicit date & time
// Example format (Year(2019), Month(5), Day(30), Hour(11), Minute(36), Second(0))
rtc.adjust(newTime);
}*/
}
void RTC::UpdateCurrentTime()
{
DateTime now = rtc.now();
Serial.println(now.hour());
CurrentHour = now.hour();
CurrentMinute = now.minute();
CurrentDay = now.day();
}
String RTC::GetDisplayTime()
{
String displayTime = "";
if (CurrentHour < 10)
{
displayTime = "0" + String(CurrentHour);
}
else
{
displayTime = String(CurrentHour);
}
if (CurrentMinute < 10)
{
displayTime += "0" + String(CurrentMinute);
}
else
{
displayTime += String(CurrentMinute);
}
return displayTime;
}
//// Main
#include <Wire.h>
#include "RTCLib.h"
#include "RTC.h"
RTC rtc;
#define Motor D1
int motorOnHour, motorOnMinute, motorOffHour, motorOffMinute = 0;
int motorOnTimeout = 15;
void setup()
{
Serial.begin(115200);
}
void loop()
{
delay(500);
rtc.UpdateCurrentTime();
MotorOn();
CheckMotorOffTimout();
}
void CalculateMotorOffTime()
{
motorOnHour = rtc.CurrentHour;
motorOnMinute = rtc.CurrentMinute;
motorOffMinute = motorOnMinute + motorOnTimeout;
motorOffHour = rtc.CurrentHour;
Serial.println("Motor Off Time :" + String(motorOffHour) + ":" + String(motorOffMinute));
if (motorOffMinute >= 60)
{
motorOffMinute = motorOffMinute - 60;
motorOffHour = motorOnHour + 1;
if (motorOffHour >= 24)
{
motorOffHour = 0;
}
}
Serial.println("Motor Off Time :" + String(motorOffHour) + ":" + String(motorOffMinute));
}
void CheckMotorOffTimout()
{
if (rtc.CurrentHour >= motorOffHour && rtc.CurrentMinute >= motorOffMinute)
{
MotorOff();
}
}
void MotorOn()
{
digitalWrite(Motor, HIGH);
}
void MotorOff()
{
digitalWrite(Motor, LOW);
}
Error:
RTC.cpp:1: In file included from
Error compiling project sources
RTC.h: 9:18: error: 'DateTime' has not been declared
Build failed for project 'Test1'
void SetNewTime(DateTime newTime)
RTC.h: 11:2: error: 'String' does not name a type
String GetDisplayTime()
RTC.h: 12:2: error: 'RTC_DS3231' does not name a type
RTC_DS3231 rtc
RTC.cpp: In constructor RTC::RTC()
RTC.cpp: 5:7: error: 'rtc' was not declared in this scope
if (!rtc.begin())
RTC.cpp: 7:3: error: 'Serial' was not declared in this scope
Serial.println("Couldnt find RTC")
RTC.cpp: At global scope
RTC.cpp: 12:22: error: variable or field 'SetNewTime' declared void
void RTC*: SetNewTime(DateTime newTime)
RTC.cpp: 12:22: error: 'DateTime' was not declared in this scope
Please tell me how to write a library or class that is using RTC_DS3231. Or any other way to achieve this?