Hay guys,
I retrieve a String "Alarmtime" wich looks like this:12:28:01
Where
Hours = 12
Minutes = 28
Seconds = 01
Now I want to minus it with 30 minutes.
So it should be: 11:58:01
How can I do that?
Is there a certain format where you just do time1-time2=time
Or something?
I searched alot, but could not find any solution.
If you are intrested in the whole code (It is obviously an alarmclock, and quite a bunch of code), here it is:
#include <DS3231.h> //RTC Library
String fullDataIn = ""; //Recieved String
String alarmTime = ""; //What time turn alarm on?
int alarmOn = 0; // Is alarm on?
DS3231 rtc(SDA, SCL); //Make new DS3231, on pin SDA and SCL
void setup()
{
Serial.begin(9600);
rtc.begin();
}
void loop()
{
checkSerial();
if (alarmOn == 0) {
checkAlarm();
Serial.println("Current time: " + (String)rtc.getTimeStr() + " -- Alarm Set: " + alarmTime);
} else
{
Serial.println("DATATATA ALARMMM");
}
delay (100);
}
bool getSerial()
{
while (Serial.available() > 0) //While Serial recieved something, check if format is correct. If so, return true.
{
char charRecieved = Serial.read();
if (charRecieved != '#' && charRecieved != '%') {
fullDataIn += charRecieved;
}
if (charRecieved == '%')
{
fullDataIn.replace("\n", "");
fullDataIn.replace("\r", "");
return true;
}
}
}
void checkSerial() {
if (getSerial() == true) //If getserial returns true (Serial recieved something in correct format), look if the message starts with a known command, otherwise ignore.
{
if (fullDataIn.startsWith("SETALARM_"))
{
setAlarm();
}
else if (fullDataIn.startsWith("RESETALARM"))
{
resetAlarm();
}
else if (fullDataIn.startsWith("RESETALL"))
{
resetAll();
}
else if (fullDataIn.startsWith("SETTIME_"))
{
setClockTime();
}
else if (fullDataIn.startsWith("GETTIME"))
{
getClockTime();
}
else if (fullDataIn.startsWith("GETALARM"))
{
getAlarmTime();
} else
{
fullDataIn = "";
}
}
}
void checkAlarm() {
String currentTime = (String)rtc.getTimeStr(); // If the current time equals the setted alarmtime Set alarm On.
if (currentTime == alarmTime) {
alarmOn = 1;
}
}
void setAlarm()
{
alarmTime = ""; //Clear alarmTime so we can rewrite it. (Somehow it does not overwrite the old data?..)
alarmTime = fullDataIn.substring(9);
fullDataIn = "";
}
void resetAll()
{
alarmTime = "";
fullDataIn = "";
alarmOn = 0;
}
void resetAlarm()
{
alarmOn = 0;
fullDataIn = "";
}
void setClockTime()
{
int hours = fullDataIn.substring(8, 10).toInt(); //Rip the data appart in hours,minutes and seconds so we can set the time with it.
int minutes = fullDataIn.substring(11, 13).toInt();
int seconds = fullDataIn.substring(14, 16).toInt();
rtc.setTime(hours, minutes, seconds);
fullDataIn = "";
}
void getClockTime()
{
Serial.println((String)rtc.getTimeStr()); //Return the time.
fullDataIn = ""; //Always need the serialstring, after executing.
}
void getAlarmTime()
{
Serial.println(alarmTime); // Return the alarm time. (if alarmtime is set, otherwise empty return).
fullDataIn = "";
}