2017-11-16 1 views
1

여기 모듈 라디오 : 비트 (파이썬)

그래서 내가 두 번째 microbit에 microbit 1에서 target_x과 target_y을 보내려면 어떻게합니까 방법을 알고 싶어요 파이썬

내 microbit 내 코드?

microbit 1 :

radio.on() 
target_x = random.randint(0,4) 
target_y = random.randint(0,4) 
if button_a.was.pressed(): 
    radio.send() 

microbit 2 :

radio.on() 
order = radio.receive() 
microbit.display.set_pixel(target_x,target_y,7) 

그래서 내가 두 번째 microbit에 microbit 1에서 target_x과 target_y을 보내려면 어떻게합니까 방법을 알고 싶어요

?

답해 주셔서 감사합니다.

답변

1

두 개의 마이크로 비트를 사용하여 아래 코드를 테스트했습니다. 메시지가 손상된 경우 수신자에게 'except, try'절을 넣습니다. 신뢰할 수있는 무선 인터페이스를 만들기 위해 더 많은 오류 검사가 구현되어야하지만 이는 질문에 대한 대답입니다.

radio_send_randints.py

''' transmit random x and y on button push ''' 
import random 
from microbit import * 
import radio 

radio.config(group=0) 
radio.on() 

while True: 
    if button_a.was_pressed(): 
     target_x = random.randint(0,4) 
     target_y = random.randint(4) 
     message = "{},{}".format(target_x, target_y) 
     radio.send(message) 
    sleep(100) 

radio_receive_randints.py

from microbit import * 
import radio 

radio.config(group=0) 
radio.on() 

while True: 
    incoming = radio.receive() 
    if incoming: 
     try: 
      target_x, target_y = incoming.split(',') 
     except: 
      continue 
     display.set_pixel(int(target_x), int(target_y), 7)