Setting RTC time using Bluetooth

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);
}
}

      analogWrite(h, fromAndroid.toInt());

Writing to the nth pin, which may, or may not, support PWM has NOTHING to do with setting the time on the RTC. What were you thinking?

I am fairly new to the arduino programming, hence my codes are really bad. :confused:

I tried the to chnage the code but no luck..


#include <Wire.h>
#include "RTClib.h"
String fromAndroid;
int h;
int m;
char s;
unsigned long previousMillis = 0;
const long interval = 1000;
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
s='a';
}
void loop(){

do
{
unsigned long currentMillis = millis();
if(Serial.available() > 0)
{
fromAndroid = Serial.readString();

if(fromAndroid.startsWith("hrs"))
{
fromAndroid.replace("hrs", "");
h=Serial.println(fromAndroid);
//analogWrite(h, fromAndroid.toInt());
//Serial.print("hrs VALUE: ");
//Serial.println(fromAndroid);
}

if(fromAndroid.startsWith("min"))
{
fromAndroid.replace("min", "");
m=Serial.println(fromAndroid);
//analogWrite(m, fromAndroid.toInt());
//Serial.print("min VALUE: ");
//Serial.println(fromAndroid);
}

if(fromAndroid.startsWith("TimeSet"))
{

//analogWrite(s, fromAndroid.toInt());
// Serial.print("TimeSet VALUE: ");
//Serial.println(fromAndroid);
s=Serial.println(fromAndroid);
}

if (s == "TimeSet")
{
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();

}

}
if(currentMillis - previousMillis >= interval){
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();
previousMillis = currentMillis;
}

}while(s="a");}

Please use code tags when posting code; see the sticky how to use this forum.

Please use the auto format option in the tools menu of the IDE to properly format your code before copying and posting code.

Sorry i hadn't used code tags. here you go.

#include <Wire.h>
#include "RTClib.h"
String fromAndroid;
int h;
int m;
char s;
unsigned long previousMillis = 0;
const long interval = 1000;
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
s='a';
}
void loop(){
  
do
{
  unsigned long currentMillis = millis();
  if(Serial.available() > 0)
  {
    fromAndroid = Serial.readString();

    if(fromAndroid.startsWith("hrs"))
    {
      fromAndroid.replace("hrs", "");
      h=Serial.println(fromAndroid);
     
    }

    if(fromAndroid.startsWith("min"))
    {
      fromAndroid.replace("min", "");
      m=Serial.println(fromAndroid);
    
    }



  if(fromAndroid.startsWith("TimeSet"))
    {
      
  
      s=Serial.println(fromAndroid);
    }

    if (s == "TimeSet")
    {
      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();
  
    }

 
  }
  if(currentMillis - previousMillis >= interval){
  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();
previousMillis = currentMillis;
}

  }while(s="a");}
  }while(s="a");}

Why are you using a do/while loop in loop()? Do you not understand why loop() is called loop()?

Why are you assigning the string "a" to s in the while statement? = != ==.

Why are you trying to assign a string to a char, anyway? Single quotes are for single characters. Double quotes are for strings - arrays of chars with a NULL terminator. You can NOT store a string in a char variable.

Im sorry i know it is late to reply, can you provide me with aia file?

Husseinsalah:
Im sorry i know it is late to reply, can you provide me with aia file?

Provide you with what?

To do what?