#include <LiquidCrystal_I2C.h> // LCD I2C용 라이브러리
#include <RTClib.h> // 실시간시계 라이브러리
#include <Wire.h> // I2C통신 라이브러리
#define RS 29 // RED LED 켜는 시점(분) : 2분간 지속
#define GS 30 // GREEN LED 켜는 시점(분) : 2분간 지속
#define BS 31 // BLUE LED 켜는 시점(분) : 2분간 지속
LiquidCrystal_I2C lcd(0x27,16,2); // 접근주소: 0x3F or 0x27
RTC_DS1307 RTC;
DateTime now;
int redSwitch,greenSwitch,blueSwitch;
int normalLCDUpdate=0;
void normalLCD();
void setup() {
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
lcd.init(); // LCD 초기화
lcd.backlight(); // LCD 백라이트 켜기
RTC.begin(); // 실시간시계 시작
// RTC.adjust(DateTime(2022,9,30,18,13,40)); // 처음 한번만 적절한 날짜 시간으로 설정, 이후 주석처리
}
void loop() {
now=RTC.now();
//RED LED는 매시간 RS분00 ~ RS+2분 직전까지 2분간 켜기
if ((now.minute()>=RS && now.minute()<RS+2) && redSwitch==LOW) { redSwitch=HIGH; digitalWrite(11,HIGH); }
if ((now.minute()<RS || now.minute()>=RS+2) && redSwitch==HIGH) { redSwitch=LOW; digitalWrite(11,LOW); }
//GREEN LED는 매시간 GS분00 ~ GS+2분 직전까지 2분간 켜기
if ((now.minute()>=GS&& now.minute()<GS+2) && greenSwitch==LOW) { greenSwitch=HIGH; digitalWrite(10,HIGH); }
if ((now.minute()<GS || now.minute()>=GS+2) && greenSwitch==HIGH) { greenSwitch=LOW; digitalWrite(10,LOW); }
//BLUE LED는 매시간 BS분00 ~ BS+2분 직전까지 2분간 켜기
if ((now.minute()>=BS && now.minute()<BS+2) && blueSwitch==LOW) { blueSwitch=HIGH; digitalWrite(9,HIGH); }
if ((now.minute()<BS || now.minute()>=BS+2) && blueSwitch==HIGH) { blueSwitch=LOW; digitalWrite(9,LOW); }
delay(25);
if (millis()%1000<50 && normalLCDUpdate==0) { normalLCD(); normalLCDUpdate=1; }
if (millis()%1000>=50 && normalLCDUpdate==1) { normalLCDUpdate=0; }
}
void normalLCD(void){
{
char buf[25];
lcd.setCursor(0,0); sprintf(buf,"<%02d/%02d> %02d:%02d:%02d",now.month(),now.day(),now.hour(),now.minute(),now.second());
lcd.print(buf);
lcd.setCursor(0,1); sprintf(buf,"SW = R:%d G:%d B:%d",redSwitch,greenSwitch,blueSwitch);
lcd.print(buf);
}
}