بسم الله
کد برنامه ساعت به همراه 2 Alarm داخلی ماژول DS3231 و DS3231
ترکیبی از example8 و example3 و کمی تغییرات از داخل کتابخانه DS3232RTC می باشد.
کد برنامه ساعت به همراه 2 Alarm داخلی ماژول DS3231 و DS3231
ترکیبی از example8 و example3 و کمی تغییرات از داخل کتابخانه DS3232RTC می باشد.
کد PHP:
// Arduino DS3232RTC Library
// https://github.com/JChristensen/DS3232RTC
// Copyright (C) 2018 by Jack Christensen and licensed under
// GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html
//
// DS3231/DS3232 Alarm Example Sketch #8
//
// Set both alarms to occur once per day at different times.
// Configure the RTC INT/SQW pin to be asserted when alarm match occurs.
// Detect the alarms by polling the INT/SQW pin.
// Assumes the RTC time is already set.
//
// Hardware:
// Arduino Uno, DS3231 RTC.
// Connect RTC SDA to Arduino pin A4.
// Connect RTC SCL to Arduino pin A5.
// Connect RTC INT/SQW to Arduino pin 2.
//
// Jack Christensen 17Dec2018
#include <DS3232RTC.h> // https://github.com/JChristensen/DS3232RTC
#include <Streaming.h> // http://arduiniana.org/libraries/streaming/
//*******RTClib.h****
#include "RTClib.h"
#include "Wire.h"
RTC_DS3231 rtc;
//*******END_RTClib.h****
//tabdile roz caat va daghighe be saniye
long t_to_sec(int h, int m , long days)
{
long t_to_sec = ((((days * 24) * 60) * 60) + ((h * 60) * 60) + (m * 60));
return t_to_sec;
}
int tA1_Hour = 0;
int tA1_Minute = 1;
long tA1_Day = 0;
int tA2_Hour = 0;
int tA2_Minute = 1;
long tA2_Day = 0;
const time_t ALARM_1_INTERVAL(t_to_sec(tA1_Hour, tA1_Minute, tA1_Day)); // alarm interval in seconds
const time_t ALARM_2_INTERVAL(t_to_sec(tA2_Hour, tA2_Minute, tA2_Day)); // alarm interval in seconds
// pin definitions
const uint8_t RTC_INT_PIN(2);
// current time from the RTC in text format
char timestamp[32];
void setup()
{
Serial.begin(115200);
//*******RTClib.h****
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
// If the RTC have lost power it will sets the RTC to the date & time this sketch was compiled in the following line
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
// If you need to set the time of the uncomment line 34 or 37
// following line sets the RTC to the date & time this sketch was compiled
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
//rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
//*******END_RTClib.h****
delay(1000);
Serial << F( "\n" __FILE__ " " __DATE__ " " __TIME__ "\n" );
pinMode(RTC_INT_PIN, INPUT_PULLUP);
// initialize the alarms to known values, clear the alarm flags, clear the alarm interrupt flags
RTC.setAlarm(ALM1_MATCH_DATE, 0, 0, 0, 1);
RTC.setAlarm(ALM2_MATCH_DATE, 0, 0, 0, 1);
RTC.alarm(ALARM_1);
RTC.alarm(ALARM_2);
RTC.alarmInterrupt(ALARM_1, false);
RTC.alarmInterrupt(ALARM_2, false);
RTC.squareWave(SQWAVE_NONE);
// print the current time
time_t t = RTC.get();
formatTime(timestamp, t);
Serial << millis() << F(" Current RTC time ") << timestamp << endl;
// set alarm 1 to occur in ?
// set alarm 2 to occur in ?
tmElements_t tm;
breakTime(t + 60 , tm);
//tm.Hour = 00; // set the RTC to an arbitrary time
//tm.Minute = 00;
tm.Second = 00;
time_t a1 = makeTime(tm) + ALARM_1_INTERVAL ; // calculate the alarm time
time_t a2 = makeTime(tm) + ALARM_2_INTERVAL; // calculate the alarm time
formatTime(timestamp, a1);
timestamp[17] = 0; // keep just hh:mm weekday DD MM
Serial << F("A1:") << timestamp << endl;
formatTime(timestamp, a2);
timestamp[17] = 0; // keep just hh:mm weekday DD MM
Serial << F("A2:") << timestamp << endl;
RTC.setAlarm(ALM1_MATCH_DATE, minute(a1), hour(a1), day(a1));
RTC.alarm(ALARM_1); // ensure RTC interrupt flag is cleared
RTC.alarmInterrupt(ALARM_1, true);
RTC.setAlarm(ALM2_MATCH_DATE, minute(a2), hour(a2), day(a2));
RTC.alarm(ALARM_2); // ensure RTC interrupt flag is cleared
RTC.alarmInterrupt(ALARM_2, true);
}
void loop()
{
//*******RTClib.h****
DateTime now = rtc.now();
char buffer1[] = "hh:mm:ss";
Serial.print(now.toString(buffer1));
char buffer2[] = " DDD ";
Serial.print(now.toString(buffer2));
char buffer3[] = "YYYY-MM-DD";
Serial.print(now.toString(buffer3));
Serial.print(" Temp:");
Serial.print(rtc.getTemperature());
Serial.println("C");
delay(1000);
//*******END_RTClib.h****
// check to see if the INT/SQW pin is low, i.e. an alarm has occurred
if (!digitalRead(RTC_INT_PIN))
{
formatTime(timestamp, RTC.get()); // get current RTC time
if (RTC.alarm(ALARM_1)) // resets the alarm flag if set
{
Serial << F(" Alarm1 Fired at:") << timestamp << endl;
// get the current time
time_t t = RTC.get();
// calculate the next alarm time
time_t a1 = t + ALARM_1_INTERVAL;
// set the alarm
RTC.setAlarm(ALM1_MATCH_DATE, second(a1), minute(a1), hour(a1), day(a1));
formatTime(timestamp, a1);
timestamp[17] = 0; // keep just hh:mm weekday DD MM
Serial << F("A1:") << timestamp << endl;
}
if (RTC.alarm(ALARM_2))
{
Serial << millis() << F(" Alarm2 Fired at:") << timestamp << endl;
// get the current time
time_t t = RTC.get();
// calculate the next alarm time
time_t a2 = t + ALARM_2_INTERVAL;
// set the alarm
RTC.setAlarm(ALM2_MATCH_DATE, second(a2), minute(a2), hour(a2), day(a2));
formatTime(timestamp, a2);
timestamp[17] = 0; // keep just hh:mm weekday DD MM
Serial << F("A2:") << timestamp << endl;
}
}
}
// format a time_t value, return the formatted string in buf (must be at least 25 bytes)
void formatTime(char *buf, time_t t)
{
char m[4]; // temporary storage for month string (DateStrings.cpp uses shared buffer)
strcpy(m, monthShortStr(month(t)));
sprintf(buf, "%.2d:%.2d:%.2d %s%.2d%s %d",
hour(t), minute(t), second(t), dayShortStr(weekday(t)), day(t), m, year(t));
}
دیدگاه