from machine import Pin, time_pulse_us
from time import sleep, sleep_us
# === 핀 설정 ===
led = Pin(1, Pin.OUT)
button1 = Pin(4, Pin.IN, Pin.PULL_DOWN)
button2 = Pin(5, Pin.IN, Pin.PULL_DOWN)
trig = Pin(3, Pin.OUT)
echo = Pin(2, Pin.IN)
# === 주기 설정
T_list = [1, 2, 4]
T_index = 0
T = T_list[T_index]
is_led_active = False
# === 거리 측정 함수
def measure_distance():
trig.low()
sleep_us(2)
trig.high()
sleep_us(10)
trig.low()
duration = time_pulse_us(echo, 1, 30000) # timeout: 30ms
distance = duration * 0.0343 / 2
return distance
# === 실행 시작
print("시작됐다 이놈아")
# === 메인 루프
while True:
if not is_led_active:
if button1.value() == 1:
T_index = (T_index + 1) % len(T_list)
T = T_list[T_index]
print(f"주기 변경됨: T = {T}초")
sleep(0.3)
if button2.value() == 1:
is_led_active = True
distance = measure_distance()
C = int(distance // 100) + 1
print(f"거리: {distance:.1f} cm → LED {C}번 깜빡임")
for _ in range(C):
led.on()
sleep(T / 2)
led.off()
sleep(T / 2)
is_led_active = False
print("LED 깜빡임 완료.")