from machine import Pin
import time
import utime
button1 = Pin(13, Pin.IN, Pin.PULL_UP)
button2 = Pin(18, Pin.IN, Pin.PULL_UP)
TRIG = Pin(22, Pin.OUT)
ECHO = Pin(21, Pin.IN)
led_pin = Pin(4, Pin.OUT)
with open("input.txt", "r") as f:
line = f.readline().strip()
T_LIST = list(map(int, line.split()))
T_index = 0
T = T_LIST[T_index]
print("T_LIST =", T_LIST)
print("초기 주기 T =", T)
prev_b1 = 1
prev_b2 = 1
is_blinking = False
while True:
b1 = button1.value()
b2 = button2.value()
if b1 == 0 and prev_b1 == 1 and not is_blinking:
T_index = (T_index + 1) % len(T_LIST)
T = T_LIST[T_index]
print("주기 전환됨 -> T =", T)
time.sleep(0.5)
if b2 == 0 and prev_b2 == 1 and not is_blinking:
print("버튼2 눌림 -> 거리 측정 시작")
is_blinking = True
TRIG.low()
utime.sleep(0.0002)
TRIG.high()
utime.sleep(0.0005)
TRIG.low()
while ECHO.value() == 0:
start = utime.ticks_us()
while ECHO.value() == 1:
stop = utime.ticks_us()
distance = (stop - start) * 0.034 / 2
print("측정된 거리:", distance)
C = int(distance // 100) + 1
print(T, C)
for k in range(C):
led_pin.value(1)
time.sleep(T/2)
led_pin.value(0)
time.sleep(T/2)
is_blinking = False
print("제어 종료")
time.sleep(1)