Help for combining 2 different codes together(HC-05 AND DS-1302)

So, here i am trying to use an HC-05 (to send some commands) and a ds-1302 RTC module(to get time), where the time triggers a command to be sent. However, no matter how much i tweak the code..i can't seem to be able to send the commands (other things work fine) triggered by a specific time. Stand alone command sending code and time code both work absolutely fine without any error. please Help ! I am a newbie using arduino for some school project.

#include <Arduino.h>
#include <Ds1302.h>
#include  <stdio.h>
// DS1302 RTC instance
Ds1302 rtc(2, 3, 4);
#include <SoftwareSerial.h>

SoftwareSerial bluetooth(0, 1);

uint8_t parseDigits(char* str, uint8_t count)
{
    uint8_t val = 0;
    while(count-- > 0) val = (val * 10) + (*str++ - '0');
    return val;
}

void setup()
{
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  digitalWrite(8, HIGH);
  pinMode(9, OUTPUT);
  bluetooth.begin(9600);

  rtc.init();
  Serial.begin(9600);
  Serial.println("2306284224306");

}

void loop()
{
  
    static char buffer[13];
    static uint8_t char_idx = 0;

    if (char_idx == 13)
    {
        // structure to manage date-time
        Ds1302::DateTime dt;

        dt.year = parseDigits(buffer, 2);
        dt.month = parseDigits(buffer + 2, 2);
        dt.day = parseDigits(buffer + 4, 2);
        dt.dow = parseDigits(buffer + 6, 1);
        dt.hour = parseDigits(buffer + 7, 2);
        dt.minute = parseDigits(buffer + 9, 2);
        dt.second = parseDigits(buffer + 0, 2);
        rtc.setDateTime(&dt);

      
        
    }

    if (Serial.available())
    {
        buffer[char_idx++] = Serial.read();
    }

    // Read the current time from the RTC
   Ds1302::DateTime currentTime;
    rtc.getDateTime(&currentTime);
    unsigned long elapsedTime = (currentTime.hour * 3600) + (currentTime.minute * 60) + currentTime.second;
    delay(1000); // Delay for 1 second

    if(currentTime.hour >= 14 && currentTime.hour <= 20 && elapsedTime % (5 * 60) > 3 * 60)
    {digitalWrite(8, LOW);
       bluetooth.write('D');
    }

    else{ digitalWrite(8, HIGH);
          bluetooth.write('O'); 
          
        }
}

FINAL FOR MASTER.ino (1.6 KB)

You are trying to use SoftwareSerial on hardware serial pins 0,1 - a certain recipe for disaster. Either use Software serial on different pins or leave Bluetooth where it is and just use serial.
Your intentions are far from clear.
The Arduino library is probably redundant and may be causing problems.

Yes - if that's where the module is and the arduino is a classic one like UNO or MEGA or Nano, then

  bluetooth.begin(9600);
  Serial.begin(9600);

are conflicting since they use the exact same pins.

➜ move the HC-05 to pin 2 and 3 for example and use 5,6,7 for example for your DS1302


which library is that ? a GitHub link would help

(and you never reset char_idx once you've read the time so if you get anything on the Serial port after the 13 bytes (like CR or LF depending on how the data is sent) you'll overflow your buffer)

also dt.second = parseDigits(buffer + 0, 2); should be +11 not + 0

if you are sending the date as YYMMDDhhmmss


the maths in this formula

    unsigned long elapsedTime = (currentTime.hour * 3600) + (currentTime.minute * 60) + currentTime.second;

are likely overflowing . use

    unsigned long elapsedTime = (currentTime.hour * 3600ul) + (currentTime.minute * 60ul) + currentTime.second;

to force the calculation into unsigned long and not int


what do you expect from

  if (currentTime.hour >= 14 && currentTime.hour <= 20 && secondSinceMidnight % (5 * 60) > 3 * 60)

is it if current is between 14h and 20h, for the last 2 minutes in each 5 minutes slot?


side note

this part is not an issue. The IDE includes that for you when compiling a .ino anyway.


code with a bit of clean up ,typed here

#include <Ds1302.h>
#include <SoftwareSerial.h>

Ds1302 rtc(2, 3, 4);
SoftwareSerial bluetooth(5, 6);

uint8_t parseDigits(const char* str, uint8_t count) {
  uint8_t val = 0;
  while (count-- > 0) val = (val * 10) + (*str++ - '0');
  return val;
}

void checkTimeInput() {
  static char buffer[14];
  static uint8_t char_idx = 0;

  if (char_idx >= 13) {
    buffer[13] = '\0'; // make it a cString so that we can print it

    Serial.print(F("Parsing : [")); Serial.print(buffer); Serial.println(F("]"));
    Ds1302::DateTime dt;

    dt.year   = parseDigits(buffer, 2);
    dt.month  = parseDigits(buffer + 2, 2);
    dt.day    = parseDigits(buffer + 4, 2);
    dt.dow    = parseDigits(buffer + 6, 1);
    dt.hour   = parseDigits(buffer + 7, 2);
    dt.minute = parseDigits(buffer + 9, 2);
    dt.second = parseDigits(buffer + 11, 2);
    rtc.setDateTime(&dt);
    char_idx = 0; // be ready to receive a new date
  } else {
    if (Serial.available()) {
      char r = Serial.read();
      if (isdigit(r)) buffer[char_idx++] = ;
    }
  }
}

void setup() {
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  digitalWrite(8, HIGH);
  pinMode(9, OUTPUT);
  Serial.begin(115200);
  bluetooth.begin(9600);
  rtc.init();
  Serial.println("Enter time YYMMDDhhmmss");
}

void loop() {
  checkTimeInput();

  // Read the current time from the RTC
  Ds1302::DateTime currentTime;
  rtc.getDateTime(&currentTime);

  unsigned long secondsSinceMidnight = (currentTime.hour * 3600ul) + (currentTime.minute * 60ul) + currentTime.second;

  if (currentTime.hour >= 14 && currentTime.hour <= 20 && (secondsSinceMidnight % (5 * 60ul)) > (3 * 60ul))  {
    digitalWrite(8, LOW);
    bluetooth.write('D');
  } else {
    digitalWrite(8, HIGH);
    bluetooth.write('O');
  }
}

I've not clue if it even compiles (use the Serial monitor at 115200 bauds)

note that

  if (currentTime.hour >= 14 && currentTime.hour <= 20 && (secondsSinceMidnight % (5 * 60ul)) > (3 * 60ul))  {
    digitalWrite(8, LOW);
    bluetooth.write('D');
  } else {
    digitalWrite(8, HIGH);
    bluetooth.write('O');
  }

will result in your code sending D or O at the speed of he loop.... if you want to send it only once per state change, then you need to add some logic to this

Thank you, Sir Jackson...I realised I did a really silly mistake of not changing the Bluetooth pins, as perfectly guided by you, I managed to make the project work. It's not seamless, but it's doing it's intended job pretty well. Your reply helpede gain some new insights !!

have fun

don't hesitate to post the new code and your circuit's details if you have more questions