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(¤tTime);
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)