Wednesday, 9 August 2017

BitIO blog 5: Touch the pins......

Part 1: Touching pins.....dropping blocks



This takes the basic 'touched.py' demo from David Whales BitIO master available here:

As ever if you have not quite noticed may playground is Minecraft. So in this simple version you will drop different wool blocks according to the pin that you touch. To "touch a pin" you will need to touch the metal end of the cable attached to the pin and then touch the ground pin to complete the circuit and for the code to work. It took me ten minutes to figure this out.


Here is a link to a video of what touching pins basic code does:

Here is a photo of how the pins were set up during the video:

Here is the link to the simple version code:

Instructions:
1. Set up the 3 cables as above on the Micro:bit
2. Plug your Micro:bit into the Pi/PC.
3.   Ensure that you have read blog 1:
http://warksjammy.blogspot.co.uk/2017/07/blog-1-getting-started-with-bitio.html
and downloaded the BitIO master files from David Whales github

4. Go into the 'Bitio master' folder, find the 'src' folder 
5. Create a new Python 3 script.6. Call it "touched_simple.py"
7. Type in the following code:

Code
# touched.py - demonstrates using pin touch

import microbit
from mcpi import minecraft as minecraft
from mcpi import block as block
import time
import random

WoolList = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]

 
mc = minecraft.Minecraft.create()
 

while True:
    
    time.sleep(1)
    if microbit.pin0.is_touched():
        msg = "Pin 0 touched, Wool block colour 0"
        print(msg)
        mc.postToChat(msg)
        pos = mc.player.getTilePos()
        mc.setBlock(pos.x, pos.y-1, pos.z,35,0)
    if microbit.pin1.is_touched():
        msg = "Pin 1 touched, Wool block colour 1"
        print(msg)
        mc.postToChat(msg)
        pos = mc.player.getTilePos()
        mc.setBlock(pos.x, pos.y-1, pos.z,35,1)
    if microbit.pin2.is_touched():
        msg = "Pin 2 touched, Wool block colour 2"
        print(msg)
        mc.postToChat(msg)
        pos = mc.player.getTilePos() 
mc.setBlock(pos.x, pos.y-1, pos.z,35,2)

8. Open Minecraft, create a new world.
9. Run the code by pressing f5.
10. Try touching pin 0, what happens.

Extension:
Try changing the block ID's


You can now download a pdf version of these tutorials as part of the updated #hackpack resource booklet here:
https://github.com/ncscomputing/HpAnthologyV2/raw/master/Hackpack%20Anthology%20V2%200.3.pdf



No comments:

Post a comment