Monday, 17 April 2017

Blog 7: What can "UCreate" with a Micro:bit and a Rasperry Pi 3 (Minecraft random radio bounce)

Last summer I created some resources to use with Micro:bit/s for CPC and over the next few months and I will try and share the best bits with the kind permission of CPC. 

You can go and buy a Micro:bit project kit from here:

This one uses the Micro:bit and Raspberry Pi. The Micro:bit uses a serial connection to the Pi to send data. The credit for this goes to Martin O'Hanlon and David Whale.

This builds on the previous blog:

Which created random bouncing Minecraft character triggered by a Micro:bit. This extends this by using two Micro:bits one remains connected to the Pi and listens for messages from the second Micro:bit which runs off batteries. If you press "a" on the battery run Micro:bit then it sends a message via the radio module. The Pi connected Micro:bit then receives the message and then sends the message to the Python script through the serial connection. In a nutshell its a radio controlled bouncing character. 

Here is a picture of the set up

Here is the updated version of the code the two Micro:bits:

from microbit import*
import random
import radio



JumpStatus = ["Jump","Donotjump"]

def get_data():
    if button_a.is_pressed():
        a = random.choice(JumpStatus)
        radio.send(random.choice(JumpStatus))  # a-ha

def run():
    while True:
        # Read any incoming messages.
        incoming = radio.receive()
        if incoming == 'Jump':

        elif incoming == 'Donotjump':
            display.scroll("Do not jump")


Code to go on Python on the Pi
Python 2.7 needs to be used

Code written by Martin O'Hanlon in the following blog post:
edited by @warksraspijam

import time
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

def read_microbit_data():
    #read a line from the microbit,
    data = s.readline()
    data_s = data.rstrip().split(" ")
    JumpStatus = data_s[0]
    print JumpStatus
    return JumpStatus

mc = Minecraft.create()
    playerPos = mc.player.getTilePos()
    while True:
        JumpStatus = read_microbit_data()
        if JumpStatus == "Jump":
            pos = mc.player.getPos()
            #change this to teleport
            mc.player.setPos(random.randint(-100,100), random.randint(1,45),random.randint(-100,100))
            msg = JumpStatus

No comments:

Post a Comment