본문 바로가기
스마트팜 with 아두이노/시계모듈 활용

시계모듈 활용 시간으로 led 제어

by SwMaker_Jun 2023. 4. 3.
728x90
반응형

 

 

 

 

#include <ThreeWire.h>  
#include <RtcDS1302.h>
#define RELAY_CH1 13  // 디지털 13번핀으로 설정


ThreeWire myWire(4,5,2); // IO, SCLK, CE  디지털 연결 번호
RtcDS1302<ThreeWire> Rtc(myWire);

int h ;
int m ;

void setup () {    
    Serial.begin(9600);
    pinMode(RELAY_CH1, OUTPUT); // 릴레이 1채널 (식물성장 led 연결)
    Rtc.Begin();
    RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
    printDateTime(compiled);
    Serial.println();
    RtcDateTime now = Rtc.GetDateTime();
}


void ledon() {
  digitalWrite(RELAY_CH1, HIGH);     // Turn on Relay channel 1
}

void ledoff(){
  digitalWrite(RELAY_CH1, LOW);      // Turn off Relay channel 1
}

// 시계 모듈 년도, 달, 시간, 분, 초 받아오는 함수
#define countof(a) (sizeof(a) / sizeof(a[0]))

void printDateTime(const RtcDateTime& dt){
    char datestring[20];                      //문자로 19개 출력 ex) 04/03/2023 11:12:53  우리는 이분을 슬라이싱하여 원하는 시간과 분을 찾을 수 있다.
    snprintf_P(datestring, 
            countof(datestring),
            PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
            dt.Month(),
            dt.Day(),
            dt.Year(),
            dt.Hour(),
            dt.Minute(),
            dt.Second() );
            Serial.print(datestring);     
            int h = dt.Hour();
            if(h>=8 and h<=20){
              ledon();
            }
            else{
              ledoff();
            }
}

void loop () {
    RtcDateTime now = Rtc.GetDateTime();
    printDateTime(now);   // 현재시간 함수 호출
    Serial.println();

    }

728x90
반응형