I have copied a simple sketch using the DS3231 RTC and it outputs an alarm every minute. I simply want to change it so that it outputs every hour but have had no success.
The code below is the example used. I haven't used the forum for quite some time and it has changed considerably particularly on entering code.` I tried to use the </> but it didn't work!
/*----------------------------------------------------------------------*
* Minimal example of setting a recurring alarm with the DS3232 RTC *
* Attach an LED to pin 7, which will flash when the alarm fires. *
* *
* Attach the RTC SQ/INT pin to Uno pin 2, with a 10K pull-up to VCC. *
* Ard pin A4 -> RTC SDA, Ard A5 -> RTC SCL, 5v -> vcc, GND -> GND *
*----------------------------------------------------------------------*/
#include <DS3232RTC.h> //http://github.com/JChristensen/DS3232RTC
#include <Time.h> //http://playground.arduino.cc/Code/Time
#include <Wire.h> //http://arduino.cc/en/Reference/Wire
#include <TimeLib.h>
#define WAKE_PIN 2
#define LED_PIN 7
tmElements_t tm;
void setup(void)
{
pinMode(LED_PIN, OUTPUT);
// RTC config:
RTC.squareWave(SQWAVE_NONE);
pinMode(WAKE_PIN, INPUT_PULLUP);
attachInterrupt(INT0, wakeUp, FALLING); // INT0 == 0 for Atmega328p
RTC.setAlarm(ALM2_EVERY_MINUTE, 0, 0, 0, 1);
RTC.alarm(ALARM_2);
RTC.alarmInterrupt(ALARM_2, true);
}
void loop(void)
{
if(RTC.alarm(ALARM_2)){
digitalWrite(LED_PIN, HIGH);
delay(5000);
digitalWrite(LED_PIN, LOW);
}
}
void wakeUp (){
//no-op
}
please edit your post, select the code part and press the </> icon in the tool bar to mark it as code. It’s barely readable as it stands. (also make sure you indented the code in the IDE before copying, that’s done by pressing ctrlT on a PC or cmdT on a Mac)
Unfortunately I made a mistake in formatting etc with this post but don't know how to delete it. However it should not be too difficult to examine the code. If this is too onerous, I will not bother this forum with my inadequate knowledge again but being in my 80's I am not too adept at getting on top of the requirements of the forum especially as I haven't used it for years.
Thanks J-M-L
What you are recommending is that I use the RTClib library instead of the DS3232RTC library. I have looked at the example and it isn't any clearer.
As I understand from the DS3232 data sheet
Table 2 Alarm mask bits give
DY/DT A2M4 A2M3 A2M2
X 1 0 0
Alarm when hours and minutes match so I should enter
RTC.setAlarm(ALARM_2, 1, 0, 0); Or am I totally wrong in this assumption?
then Alarm1 will be triggered at 3pm and 4pm and 5pm etc as DS3231_A1_Minute tells to set the Alarm when minutes and seconds match (so here at the full hour)
At a glance that looks like all the original sketch woukd have needed to go to a once per hour on the whatever minutes alarm. In the original, at 0 minutes or on the hour.
alto777, your suggestion worked like a charm. The alarm triggers every hour and is exactly what I want. I should have looked at the JChristensen library and noticed the section on Alarm Type Enumerations. And thank you JML. However, I'm not sure if what you suggested would have got me what I wanted as it looked like it was geared to producing an alarm on a certain day and time etc rather than an output that didn't care about date or set time. Do you think that the RTClib from adafruit is better than that of JChristensen? Anyway, it's all good. Incidently the sketch was a cut down version of what I am developing.
I have used both. The Christensen library is designed to be integrated with the time library while RTCLib is more stand alone with its own Date/Time class. For a long time RTCLib did not have alarm support.
You can use either one with confidence. As you said, reading the library source code will help you better understand which ever library you use.
Word. That's where I found the alarm type constants…
It's nice that we have so many libraries but documentation doesn't seem to be anyone's favorite part of writing one. Even examples don't reveal all features, so you gotta go digging.
All the code that goes into anything you get working in the IDE is on your computer somewhere. Makes for good reading.
The function did indeed require a specific day but the parameter which tells the RTC what to use for triggering the alarm was just looking at minutes and seconds so the rest of the date was irrelevant and you would have got a match at every top of the hour. (Basically when you match minutes and seconds you ask a match for a date like XX/XX/XX XX:MM:SE where XX can be anything and just MM and SS matter)
The other library works too - the reason I recommended RTCLib was indeed the facilities of the DateTime and TimeSpan classes that make dealing with time like comparing dates or calculating how long you have between two dates easy.