Welcome - CBiS Education

Download Report

Transcript Welcome - CBiS Education

Proudly supported by
To
Open up putty
Enter the IP Address of your Raspberry Pi
Login with the details
Username – ninja
Password – dojo
Then type su pi it will ask for a password raspberry .
Why ?
sudo python minecraft/board/button1.py
button1led.txt
Let`s have a look at the code
import RPi.GPIO as GPIO ## Import GPIO library
import time ## Import 'time' library. Allows us to use 'sleep'
These 2 line import some important stuff ,
The Rpi.GPIO is the library of files that allow us to control the pins on the
raspberry pi .
Time it allows us to us the sleep function a bit further on
GPIO.setmode(GPIO.BOARD) ## Set up GPIO using BOARD numbering
GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_UP) ## Enable pull-up
resistor on pin 18
GPIO.setup(15, GPIO.OUT) ## Setup GPIO Pin 15 to OUT
Here we tell the Pi how to talk to the pins ,
We also setup 2 pins
12 – for our button
15 – for the LED
print " Code is running"
print " Press the red button and watch the led come on !!! "
Just a little info for the user so they know the code is running and what it does .
Pin Number 15 (GPIO 22) = Red
Pin Number 13 (GPIO 27)= Green
Pin Number 11 (GPIO 17) = Blue
while True:
input_state = GPIO.input(12)
if input_state == False:
print('Button Pressed')
GPIO.output(15,True) ## Turn on
GPIO pin 15
time.sleep(0.2)
else:
GPIO.output(15,False)
time.sleep(0.2)
Infinity loop
Detects if the button is pressed
if statement to test the input_state
If the button is pressed is classed as false
So we print a message and turn
Pin 15 on to light the LED
We wait for 0.2 seconds to retest
If the button is not pressed we run the
Else statement
We switch the LED off and wait 0.2 sec
Again.
sudo python minecraft/board/button1led.py
sudo python minecraft/board/button2.py
Red Button to teleport .
Black Button bring him home .
teleport.py
sudo python minecraft/board/teleport.py
from mcpi import minecraft ## import the minecraft library
mc = minecraft.Minecraft.create() ## create a minecraft instance
x, y, z = mc.player.getPos()
mc.player.setPos(x+100, y+100, z+100)
mc.player.setPos(0.5, 0.0, 0.5)
sudo python minecraft/board/teleport_random.py
x, y, z = mc.player.getPos()
mc.postToChat("Hello Steve you were at x = "+ str(x) + " y = " + str(y) + " z =
" + str(z) )
time.sleep(0.2)
xr = randint(0 , 200)
yr = randint(0 , 200)
zr = randint(0 , 200)
mc.player.setPos(xr, yr, zr)
print('Red Teleport] Pressed')
button1.py
-
press the red button
button1led.py
-
press the button and watch the LED come on .
button2.py
-
switch the led on and off
teleport.py
-
teleport Steve
Teleport_random.py -
teleport Steve randomly around the map