Skip to content

Instantly share code, notes, and snippets.

@deveworld
Last active November 4, 2025 01:26
Show Gist options
  • Select an option

  • Save deveworld/d5f98758e545c3f1dc0f5abf37cc2cf1 to your computer and use it in GitHub Desktop.

Select an option

Save deveworld/d5f98758e545c3f1dc0f5abf37cc2cf1 to your computer and use it in GitHub Desktop.
Micro:bit tag/finder
# 분실방지 태그 시스템 - 파인더/휴대용
# micro:bit에 업로드할 코드
from microbit import *
import radio
import music
CHANNEL = 73
POWER = 7
# 라디오 설정
radio.on()
radio.config(channel=CHANNEL, power=POWER)
display.clear()
last_signal_time = running_time()
lost_signal = False
lost_signal_threshold = 10000 # 10초
find_mode = False
last_beep_time = 0
current_beep_interval = 1000
beep_times = [100, 300, 500, 1000, 2000] # 매우 가까움 -> 매우 멀음
rssi_images = [
Image('99999:99999:99999:99999:99999'), # 매우 가까움
Image('00000:99999:99999:99999:00000'), # 가까움
Image('00000:00000:99999:00000:00000'), # 보통
Image('00000:00000:09990:00000:00000'), # 멀음
Image('00000:00000:00900:00000:00000') # 매우 멀음
]
def rssi_to_level(rssi):
if rssi > -50:
return 0
elif rssi > -60:
return 1
elif rssi > -70:
return 2
elif rssi > -80:
return 3
else:
return 4
def handle_event():
global find_mode, last_beep_time
# A버튼: 소리 내기
if button_a.was_pressed():
display.show(Image.TARGET)
radio.send('RING')
sleep(500)
display.clear()
# B버튼: 찾기 모드 토글
if button_b.was_pressed():
find_mode = not find_mode
if find_mode:
display.show(Image.TARGET)
last_beep_time = running_time()
else:
music.stop()
display.clear()
sleep(500)
while True:
handle_event()
# 라디오 수신
incoming = radio.receive_full()
if incoming:
msg, rssi, timestamp = incoming
if msg == b'\x01\x00\x01PING':
last_signal_time = running_time()
level = rssi_to_level(rssi)
display.show(rssi_images[level])
current_beep_interval = beep_times[level]
radio.send("ACK")
if find_mode and running_time() - last_signal_time < lost_signal_threshold:
if running_time() - last_beep_time >= current_beep_interval:
music.play('C4:1', wait=False)
last_beep_time = running_time()
# 신호 손실 감지
if running_time() - last_signal_time > lost_signal_threshold:
if not lost_signal:
lost_signal = True
# 신호 손실 시 해골 이미지 표시
display.show(Image.SKULL)
else:
if lost_signal:
display.clear()
lost_signal = False
sleep(100)
# 분실방지 태그 시스템 - 태그용
# micro:bit에 업로드할 코드
from microbit import *
import radio
import music
# 설정 상수
CHANNEL = 73
POWER = 7
RING_DURATION = 3000
PING_INTERVAL = [1000, 3000] # 일반 모드: 1초, 저전력 모드: 3초
SLEEP_DURATION = [200, 1000] # 일반 모드: 0.2초, 저전력 모드: 1초
last_ack_time = running_time()
lost_signal = False
lost_signal_threshold = 10000 # 10초
last_ping_time = -10000
ring_signal = -10000
low_power_mode = False
ring_started = False
# 라디오 설정
radio.on()
radio.config(channel=CHANNEL, power=POWER)
# 초기화
display.clear()
def handle_event():
global low_power_mode
# A버튼 + B버튼: 저전력 모드 토글
if button_a.is_pressed() and button_b.is_pressed():
low_power_mode = not low_power_mode
if low_power_mode:
display.show(Image.ASLEEP)
else:
display.show(Image.HAPPY)
sleep(500)
display.clear()
while True:
display.clear()
# 주기적으로 PING 신호 전송
if running_time() - last_ping_time >= PING_INTERVAL[low_power_mode]:
last_ping_time = running_time()
radio.send('PING')
handle_event()
# 수신된 라디오 신호 처리
incoming = radio.receive()
if incoming:
data = incoming.strip()
if data == 'RING':
ring_signal = running_time()
elif data == 'ACK':
last_ack_time = running_time()
if not low_power_mode:
display.set_pixel(2, 2, 5) # ACK 신호 수신 표시
# 링 신호 수신 시 소리 재생
if running_time() - ring_signal < RING_DURATION:
display.show(Image.TARGET)
if not ring_started:
music.play(['C5:1', 'E5:1'], wait=False, loop=True)
ring_started = True
else:
if ring_started:
music.stop()
ring_started = False
if low_power_mode:
display.clear() # 저전력 모드에서는 화면 끄기
# 신호 손실 감지
if not low_power_mode and running_time() - last_ack_time > lost_signal_threshold:
if not lost_signal:
lost_signal = True
# 신호 손실 시 해골 이미지 표시
display.show(Image.SKULL)
else:
if lost_signal:
display.clear()
lost_signal = False
# 저전력 모드에 따른 대기 시간 조절
sleep(SLEEP_DURATION[low_power_mode])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment