You can go and buy a Micro:bit project kit from here: http://cpc.farnell.com/bbc-micro-bit-kits
This project uses the Micro:bit serial connection with the Pi to randomly send 1 or 0 through the serial connection to tell the Pi based python script whether or not to lay an active TNT block or inactive TNT block. Thus it is called TNT roulette. :)
Picture of set up of Micro:bit plugged into the Pi
Instructions
You will need to have a Raspberry Pi and Micro:bit for this
to work. You will need to install MU on the Raspberry Pi to flash the micro python script. You will need to keep the Micro:bit plugged into the Pi usb port.
Here is the micro python script for the Micro:bit:
#Written by David Whale and Martin
O’Hanlon edited by Chris Penn
from microbit import*
import random
REFRESH = 500
BlockID = 46
ActiveValues = [0,1] # 0 = off
def get_data():
a, b = button_a.was_pressed(), button_b.was_pressed()
Active = random.choice(ActiveValues)
print(a, b, BlockID,Active)
def run():
while True:
sleep(REFRESH)
get_data()
display.show('M')
run()
Python code to go on the Raspberry Pi
N.b. This needs to be run in Python 2.7
"""
Code written by Martin O'Hanlon in
the following blog post:
http://www.stuffaboutcode.com/2016/03/microbit-get-data-from-usb.html
I have added a few bits on top
"""
import serial
from mcpi.minecraft import
Minecraft
from time import sleep
from mcpi import block as block
import random
PORT = "/dev/ttyACM0"
BAUD = 115200
s = serial.Serial(PORT)
s.baudrate = BAUD
s.parity = serial.PARITY_NONE
s.databits = serial.EIGHTBITS
s.stopbits = serial.STOPBITS_ONE
#read the first line and flush any
bad data
s.readline()
def read_microbit_data():
#read a line from the microbit,
data = s.readline()
#split the microbit data into x, y, z, a, b
data_s = data.rstrip().split(" ")
a = True if data_s[0] == "True" else False
b = True if data_s[1] == "True" else False
BlockID = int(data_s[2])
Active = int(data_s[3])
return a,b,BlockID,Active
mc = Minecraft.create()
try:
playerPos = mc.player.getTilePos()
while True:
a,b,BlockID, Active =
read_microbit_data()
if a == True:
pos = mc.player.getPos()
msg = "Button pressed =
",str(a),"+ Block ID = ",BlockID," + ",
"Active=",Active
mc.postToChat(msg)
mc.setBlock(pos.x,pos.y,pos.z,BlockID,Active)
finally:
sleep(1)
s.close()
|
Instructions
1. Having flashed your Micro:bit script and leaving your Micro:bit plugged into your Pi.
2. Open Minecraft, create a new world, ensure you can see the character like in the picture above, minimise the screen.
3. Run the Python code in 2.7 from above.
4. Press the A button on the Micro:bit, look at Minecraft and you should see a TNT block has been dropped. Whack it to see if it is active or not!
Enjoy!
No comments:
Post a Comment