//https://docs.arduino.cc/built-in-examples/digital/Debounce/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
#include <math.h>
#define alarmPin 13
const int button1 = 9; // button1 pin number
const int button2 = 10; // button1 pin number
const int button3 = 11; // button1 pin number
const int alarm_pin = 12; // Alarms pin number
const int alarm[]={2024,1,25,20,0,4};
int alarm1[]={2024,1,25,20,0,4};
const int waitTime =9000;
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_LINES 2
#define SERIAL_OPTION 0
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
RTC_DS1307 rtc;
int led=4;
int b = 2;
int val=0,led_num=0;
int ledstate=LOW;
int buttonState;
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 30;
void setup() {
if (SERIAL_OPTION) Serial.begin(9600); // 시리얼통신 초기화
lcd.init(); // LCD 초기화
lcd.backlight(); // LCD 백라이트 켜기
rtc.begin(); // 실시간시계 시작
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
pinMode(12, OUTPUT);
digitalWrite(alarm_pin, LOW);
/* lcd.begin(16,2);
lcd.backlight();*/
while (!Serial); // for Leonardo/Micro/Zero
Serial.begin(9600);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// 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(2019, 3, 3, 20, 0, 0));
}
rtc.adjust(DateTime(2024, 1, 24, 20, 0, 0));
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
void loop(){
DateTime now = rtc.now();
//val = val++;
DS3231_read(now);
DS3231_display() ;
// Alarm_display() ;
if(checkAlarm(now)){
digitalWrite(alarmPin, HIGH);//
lcd.setCursor(0,0);
lcd.print("Alarm Triggered");
Serial.print("Alarm Triggered");
delay(waitTime);
//while (1);// wait for ever
}else{
digitalWrite(alarmPin, LOW);//
}
Serial.println();
// save the reading. Next time through the loop, it'll be the lastButtonState:
}
boolean checkAlarm(DateTime timeNow){
if(
alarm[0] ==timeNow.year()
&&
alarm[1] ==timeNow.month()
&&
alarm[2] ==timeNow.day()
&&
alarm[3] ==timeNow.hour()
&&
alarm[4] ==timeNow.minute()
&&
alarm[5] ==timeNow.second()
){
return true;
}else{
return false;
}
}//checkAlarm
int second ;
int minute ; int hour ; int day ; int month ; int year ;
void DS3231_read(DateTime timeNow){ // Function to read time & calendar data
// Request 7 bytes from DS3231 and release I2C bus at end of reading
second = timeNow.second(); // Read seconds from register 0
minute = timeNow.minute(); // Read minuts from register 1
hour = timeNow.hour(); // Read hour from register 2
day = timeNow.day(); // Read day from register 3
// Read date from register 4
month = timeNow.month(); // Read month from register 5
year = timeNow.year(); // Read year from register 6
}
void DS3231_display()
{
lcd.setCursor(0,0); lcd.print("DATE: ");
lcd.print(year); lcd.print("/"); lcd.print(month); lcd.print("/"); lcd.print(day);
lcd.setCursor(0,1); lcd.print("TIME: ");
lcd.print(hour); lcd.print(":"); lcd.print(minute); lcd.print(":"); lcd.print(second);
}
void Alarm_display() {
lcd.setCursor(0,0); lcd.print("Alarm: ");
lcd.print(alarm[0]); lcd.print("/"); lcd.print(alarm[1]); lcd.print("/"); lcd.print(alarm[2]);
lcd.setCursor(0,1); lcd.print("TIME: ");
lcd.print(alarm[3]); lcd.print(":"); lcd.print(alarm[4]); lcd.print(":"); lcd.print(alarm[5]);
}
/*
void loop(){
//read the value of the button when pressed
val=digitalRead(b);
//in this example, the value of the button when pressed is 1
if(val==1){
//reverse the current state of the ledstate variable
ledstate=!ledstate;
//the ledstate variable controls the HIGH or LOW state
digitalWrite(led,ledstate);
if (ledstate==HIGH){
lcd.clear(); //erase all text on the lcd
lcd.setCursor(0,0);
lcd.print("LED is ON");}
else{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("LED is OFF");
}
//wait .5 second before the next reading
delay(500);
}
}*/