everyone Hello I am making a pill dispenser but I'm stuck at the program in making the alarm clock. I have the code in arduino but I don't know how to do the blocks. Can anyone help me. I want this alarm set in arduino and compare with the RTC time. When the RTC time is equal to alarm time, I need to print the message for ex: "The medicine is ready", or "Is time to take medicine".
#include <SoftwareSerial.h>
#include <DS1302.h>
// Initialize SoftwareSerial for HC-05 Bluetooth Module
SoftwareSerial bluetooth(0, 1); // RX, TX
// Initialize DS1302 RTC
DS1302 rtc(5, 6, 7); // RST, DAT, CLK
// Variables to store alarm time
int alarmHr;
int alarmMin;
int alarmSec;
int buzzer = 12;
// Function to set alarm time from received data
void setAlarmTime(int h, int m, int s) {
alarmHr = h;
alarmMin = m;
alarmSec = s;
}
void setup() {
// Start serial communication with the HC-05 module
bluetooth.begin(9600);
pinMode(buzzer, OUTPUT);
// Initialize communication with the DS1302 RTC
rtc.writeProtect(false); // Disable write protection
rtc.halt(false); // Enable the clock (if it was halted)
// // Set the current time (you can adjust this as needed)
// rtc.time(Time(2024, 5, 2, 12, 0, 0, Time::kMonday));
// // Optional: Set the alarm time (you can receive this via Bluetooth)
// setAlarmTime(8, 30, 0); // Example: 8:30 AM
}
void loop() {
// Get current time from DS1302 RTC
Time currentTime = rtc.time();
// Check if data has been received from phone via Bluetooth
if (bluetooth.available()) {
// Assuming the data is sent in 'hh:mm' format from two separate text boxes
// String receivedHr = bluetooth.readStringUntil(':'); // Read until the colon separator
// String receivedMin = bluetooth.readStringUntil('\n'); // Read until newline character
// alarmHr = receivedHr.toInt();
// alarmMin = receivedMin.toInt();
// alarmSec = 0; // If seconds are not set by the phone, default to 0
alarmHr = receiveData();
alarmMin = receiveData();
alarmSec = 0;
// Set alarm time
setAlarmTime(alarmHr, alarmMin, alarmSec);
Serial.print(alarmHr + ":" + alarmMin);
if(currentTime.hr == alarmHr && currentTime.min == alarmMin){
digitalWrite(buzzer, HIGH);
delay(500);
digitalWrite(buzzer, LOW);
}
}
// Compare current time with alarm time and take appropriate action
// (e.g., trigger an event, turn on a light, etc.)
// Function to receive data from Bluetooth module
}
int receiveData() {
int receivedData = 0;
while (bluetooth.available()) {
char receivedChar = bluetooth.read();
if (receivedChar == '\n') {
break;
}
receivedData = (receivedData * 10) + (receivedChar - '0');
}
return receivedData;
}
Can I edit those blocks or I must do the app from the beggining?