I am using Arduino mini 5V for my project and RTC - Real Time Clock Module DS1307
I would like to wake the board at a certain time and run a function.
When I use the TimeAlarm alone and manually set the time everything works fine:
#include "Time.h"
#include "TimeAlarms.h"
const int output = 3;
void setup(){
setTime(22,29,55,12,31,14); // set time to Saturday 8:29:00am Jan 1 2011
Alarm.alarmRepeat(10,30,0,buzzer); // 10:30am every day
Alarm.alarmRepeat(16,30,0,buzzer); // 4:30pm every day
Alarm.alarmRepeat(22,30,00,buzzer); // 10:30pm every day
pinMode( output , OUTPUT);//new line
Serial.begin(9600);
}
void loop(){
digitalClockDisplay();
Alarm.delay(1000); // wait one second between clock display
}
void buzzer(){
// Do some stuff here
}
However when I use RTC the buzzer function wouldn't get called, it still prints the time though
#include <Wire.h>
#include "RTClib.h"
#include "Time.h"
#include "TimeAlarms.h"
RTC_Millis rtc;
const int output = 3;
void setup(){
rtc.begin(DateTime(F(__DATE__), F(__TIME__)));
Alarm.alarmRepeat(10,30,0,buzzer); // 10:30am every day
Alarm.alarmRepeat(16,30,0,buzzer); // 4:30pm every day
Alarm.alarmRepeat(22,30,00,buzzer); // 10:30pm every day
pinMode( output , OUTPUT);//new line
Serial.begin(9600);
}
void loop(){
//printing the current time
DateTime now = rtc.now();
Serial.print(now.year());
Serial.print('/');
Serial.print(now.month());
Serial.print('/');
Serial.print(now.day());
Serial.print(' ');
Serial.print(now.hour());
Serial.print(':');
Serial.print(now.minute());
Serial.print(':');
Serial.print(now.second());
Serial.println();
Alarm.delay(1000); // wait one second between clock display
}
void buzzer(){
// Do some stuff
}
You have not properly integrated the RTC with the Time and Time Alarms libraries.
First you are using the wrong class from the library you neet to use RTC_DS1307 rtc instead of RTC_Millis rtc;
RTC_Millis is a software clock, and does not use the hardware rtc. You can verify this by removing all connections to the rtc, and the sketch will load and run. You need to be using the example "ds 1307" in the library, rather than "softrtc" as your template.
This change also requires some different syntax for setting the time on the RTC.
You can use the rtc for alarms by reading it, and testing for specified alarm times without using the Time and TimeAlarms libraries, but if you do use the libraries it is best to set them up with an external reference source of time using the function setSynchProvider(). If you look in the examples for the Time library you will see several examples including one with an RTC as the synch provider. Default behaviour of the Time library is to go check the reference every 5 minutes and keep the system clock synched to the reference.
Here is your code with these corrections. I have not tested the alarms or buzzers, etc.
#include <Wire.h>
#include "RTClib.h"
#include "Time.h"
#include "TimeAlarms.h"
//RTC_Millis rtc;
RTC_DS1307 rtc;
const int output = 3;
uint32_t syncProvider()//function which sets up the RTC as the source of external time
{
return rtc.now().unixtime();
}
void setup(){
//rtc.begin(DateTime(F(__DATE__), F(__TIME__)));
rtc.begin();
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(__DATE__, __TIME__));//comment this out when the RTC has been set
setSyncProvider(syncProvider); // the function to get the time from the RTC
Alarm.alarmRepeat(10,30,0,startTheCrap); // 10:30am every day
Alarm.alarmRepeat(16,30,0,startTheCrap); // 4:30pm every day
Alarm.alarmRepeat(22,30,00,startTheCrap); // 10:30pm every day
pinMode( output , OUTPUT);//new line
Serial.begin(9600);
}
void loop(){
//printing the current time
DateTime now = rtc.now();
Serial.print(now.year());
Serial.print('/');
Serial.print(now.month());
Serial.print('/');
Serial.print(now.day());
Serial.print(' ');
Serial.print(now.hour());
Serial.print(':');
Serial.print(now.minute());
Serial.print(':');
Serial.print(now.second());
Serial.println();
Alarm.delay(1000); // wait one second between clock display
}
void buzzer(){
// Do some stuff
}
Thank you very much cattledog. I used the code you sent. I also added Wire.begin() above rtc.begin()
Then I ralized rtc1307 example refers to 12C that my mini doesn't have. I soldered two pins above A3 and VCC and used M/F wires to connect them to SDA and SCL and now everything works! Thank you so much again for your help!
Good job getting it sorted out. I'd say I left out the Wire.begin() as a test, but I just missed it.
I see that you deleted one of your posts from within the thread. As a matter of etiquette you shouldn't really do that as others reading won't get the full context.