책상 위 나만의 작은 식물 공장
LED는 매일 08:00에 켜지고 20:00에 꺼집니다.
하루 총 12시간 0분 동안 LED가 켜집니다.적당해요!
1from makerpack.components.mosfet import MOSFET
2from makerpack.components.ds1307 import DS1307
3import time
4
5# 모스펫 초기화
6mosfet = MOSFET(gate_pin=15)
7
8# RTC 모듈 초기화
9rtc = DS1307(sda_pin=18, scl_pin=19, i2c_num=1, i2c_addr=0x68)
10
11# 켜는 시간과 끄는 시간 설정 (시:분:초 형식)
12ON_TIME = (8, 0, 0)
13OFF_TIME = (20, 0, 0)
14
15while True:
16 current_time = rtc.get_datetime()
17 current_hour = current_time[4]
18 current_minute = current_time[5]
19 current_second = current_time[6]
20
21 current_seconds_of_day = current_hour * 3600 + current_minute * 60 + current_second
22 on_seconds_of_day = ON_TIME[0] * 3600 + ON_TIME[1] * 60 + ON_TIME[2]
23 off_seconds_of_day = OFF_TIME[0] * 3600 + OFF_TIME[1] * 60 + OFF_TIME[2]
24
25 if on_seconds_of_day <= current_seconds_of_day < off_seconds_of_day:
26 if not mosfet.state:
27 mosfet.fade_in(target_power=100, duration_ms=2000)
28 else:
29 if mosfet.state:
30 mosfet.fade_out(duration_ms=2000)
31
32 time.sleep(60)