Hi,
I am working on a project in which i want to set the RTC time using HC-05 Blutooth module. I have created a Android App using MIT App inventor. Which send out three text
a. hrs## (00 - 24 hours)
b. min## (00 - 59 minutes)
c. TimeSet (to update time and exit the loop)
Currently I do not care about date month.
I am not able to execute this using the below code, I am not sure what is causing the issue. When I try using this i get random dates uploaded in RTC.
#include <Wire.h>
#include "RTClib.h"
String fromAndroid;
int h;
int m;
int s;
RTC_DS1307 rtc;
void setup ()
{
Serial.begin(57600); //baud rate
rtc.begin();
if (! rtc.begin())
{
Serial.println("Couldn't find RTC");
while (1);
}
if (! rtc.isrunning())
{
Serial.println("RTC is NOT running!");
}
//Set the date and time
}
void loop()
{
if(Serial.available() > 0)
{
fromAndroid = Serial.readString();
if(fromAndroid.startsWith("hrs"))
{
fromAndroid.replace("hrs", "");
analogWrite(h, fromAndroid.toInt());
Serial.print("hrs VALUE: ");
Serial.println(fromAndroid);
}
if(fromAndroid.startsWith("min"))
{
fromAndroid.replace("min", "");
analogWrite(m, fromAndroid.toInt());
Serial.print("min VALUE: ");
Serial.println(fromAndroid);
}
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
if(fromAndroid.startsWith("TimeSet"))
{
analogWrite(s, fromAndroid.toInt());
Serial.print("TimeSet VALUE: ");
Serial.println(fromAndroid);
}
if (s == 25)
{
rtc.adjust(DateTime(2016,2,26,h,m,10));
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
exit(0);
}
delay(10000);
}
}