Wednesday, 9 August 2017

BitIO Blog 6: 'Touching pins....takes you places'


Part 2: Touching pins.....takes you places.



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. This version builds on from the previous blog about 'touching pins'
(http://warksjammy.blogspot.co.uk/2017/08/bitio-blog-5-touch-pins.html)

In this version you will touch a Micro:bit pin 0-2 and either:

  • pin 0- build the world map in Minecraft.
  • pin 1- teleport to England.
  • pin 2- teleport to the USA.



To "touch a pin" you will need to touch pin 0 for example with one finger and complete the circuit by touching the 'gnd' pin and then the code will work. 

Here is a link to a video of what touching pins basic code does:
https://twitter.com/Warksraspijam/status/895355608404242436

Here is a photo of how it will look:

Here is the link to the simple version code:
Teleportation and pin reading code

Build world code:

Data file:

Credit Damien Mooney's world building code:
https://damianmooney.wordpress.com/2016/02/16/raspberry-pi-minecraft-iss-tracker/

Instructions:
1. Plug your Micro:bit into the Pi/PC.
2.   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

3. Go into the 'Bitio master' folder, find the 'src' folder 
4. Create a new Python 3 script.
5. Call it "touching pins takes you places.py"
6. 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
from datetime import datetime
import time
import serial
import random
import BuildWorldDM as bw

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

UsaX = 53.8
UsaZ = 39.7
 
mc = minecraft.Minecraft.create()
  
def Teleport(x,z,Country):
    mc.player.setPos(x,20,z)
    mc.camera.setFollow()
    mc.setBlock(x,1, z,35,random.choice(WoolList))
    mc.postToChat(Country)


while True:
    time.sleep(1)
    if microbit.pin0.is_touched():
        print("Pin 0 touched, build world")
        bw.Build()
    elif microbit.pin1.is_touched():
        print("Pin 1 touched, teleport to England")
        Teleport(EngX,EngZ,"England")
    elif microbit.pin2.is_touched():
        print("Pin 2 touched, teleport to the USA") 
        Teleport(UsaX,UsaZ,"USA")



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

Extension:
Try adding in extra countries.


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