def init(delay=0.0015): global running, StepCount, StepDir, stepsToDo, StepPosition, StepPins global StepCounter, Seq, WaitTime # Use physical pin numbers GPIO.setmode(GPIO.BCM) # Define GPIO signals to use # StepPins = [35,36,32,33] # RoboHat StepPins = [4, 17, 27, 18] # ZeroPoint # Set all pins as output for pin in StepPins: GPIO.setup(pin, GPIO.OUT) GPIO.output(pin, False) # Define pin sequence Seq = [[1, 0, 0, 1], [1, 0, 1, 0], [0, 1, 1, 0], [0, 1, 0, 1]] StepCount = len(Seq) StepDir = 1 # 1 == clockwise, -1 = anticlockwise StepsToDo = 0 #number of steps to move StepPosition = 0 # current steps anti-clockwise from the zero position # Initialise variables StepCounter = 0 WaitTime = delay running = True # Move pointer to zero position StepDir = -1 stepsToDo = 700 step()
def init(delay = 0.0015): global running, StepCount, StepDir, stepsToDo, StepPosition, StepPins global StepCounter, Seq, WaitTime # Use physical pin numbers GPIO.setmode(GPIO.BCM) # Define GPIO signals to use # StepPins = [35,36,32,33] # RoboHat StepPins = [4, 17, 27, 18] # ZeroPoint # Set all pins as output for pin in StepPins: GPIO.setup(pin,GPIO.OUT) GPIO.output(pin, False) # Define pin sequence Seq = [[1,0,0,1], [1,0,1,0], [0,1,1,0], [0,1,0,1]] StepCount = len(Seq) StepDir = 1 # 1 == clockwise, -1 = anticlockwise StepsToDo = 0 #number of steps to move StepPosition = 0 # current steps anti-clockwise from the zero position # Initialise variables StepCounter = 0 WaitTime = delay running = True # Move pointer to zero position StepDir = -1 stepsToDo = 700 step()
def setup(): GPIO.setmode(GPIO.BCM) for l in LED_GPIO: GPIO.setup(l, GPIO.OUT) GPIO.output(l, False) for b in BUTTON_GPIO: GPIO.setup(b, GPIO.IN)
def setup(): GPIO.setmode(GPIO.BCM) for l in LED_GPIO: GPIO.setup(l, GPIO.OUT) GPIO.output(l, False) for b in BUTTON_GPIO: GPIO.setup(b, GPIO.IN)
#! /usr/bin/python import mcpi.minecraft as minecraft import mcpi.block as block import time import anyio.GPIO as GPIO blockId = block.CARPET_PINK.id blockData = block.CARPET_PINK.data LED_PIN = 9 GPIO.setmode(GPIO.BCM) GPIO.setup(LED_PIN, GPIO.OUT) def drawCarpet(locx, locy, locz, size, blockId, blockData): mc.setBlocks(locx + 1, locy, locz + 1, locx + size, locy, locz + size, blockId, blockData) if __name__ == "__main__": mc = minecraft.Minecraft.create() orig = mc.player.getTilePos() drawCarpet( orig.x, orig.y, orig.z, 3, blockId, blockData) try: while True: time.sleep(2) pos = mc.player.getTilePos() if pos.x > orig.x and pos.x < orig.x + 3 + 1 and pos.z > orig.z and pos.z < orig.z + 3 + 1: print("x="+str(pos.x) + " y="+str(pos.y) + " z="+str(pos.z)) mc.postToChat("Welcome to the pink carpet!") GPIO.output(LED_PIN, True) else: GPIO.output(LED_PIN, False)
#Pibrella Demo from time import sleep t = 0.2 import anyio.GPIO as GPIO RED = 27 YEL = 17 GRN = 4 BUZZ =18 OUT1 =22 OUT2 =23 OUT3 =24 OUT4 =25 GPIO.setmode(GPIO.BCM) GPIO.setup(RED, GPIO.OUT) GPIO.setup(YEL, GPIO.OUT) GPIO.setup(GRN, GPIO.OUT) GPIO.setup(OUT1,GPIO.OUT) GPIO.setup(OUT2,GPIO.OUT) GPIO.setup(OUT3,GPIO.OUT) GPIO.setup(OUT4,GPIO.OUT) try: while True: GPIO.output(RED, True) sleep(t) GPIO.output(YEL, True) sleep(t) GPIO.output(GRN, True)
import anyio.GPIO as GPIO #Import GPIO library import time #Import time library GPIO.setmode(GPIO.BCM) #Set GPIO pin numbering TRIG = 23 #Associate pin 23 to TRIG ECHO = 24 #Associate pin 24 to ECHO print "Distance measurement in progress" GPIO.setup(TRIG, GPIO.OUT) #Set pin as GPIO out GPIO.setup(ECHO, GPIO.IN) #Set pin as GPIO in while True: GPIO.output(TRIG, False) #Set TRIG as LOW print "Waitng For Sensor To Settle" time.sleep(2) #Delay of 2 seconds GPIO.output(TRIG, True) #Set TRIG as HIGH time.sleep(0.00001) #Delay of 0.00001 seconds GPIO.output(TRIG, False) #Set TRIG as LOW while GPIO.input(ECHO) == 0: #Check whether the ECHO is LOW pulse_start = time.time() #Saves the last known time of LOW pulse while GPIO.input(ECHO) == 1: #Check whether the ECHO is HIGH pulse_end = time.time() #Saves the last known time of HIGH pulse pulse_duration = pulse_end - pulse_start #Get pulse duration to a variable
#TrafficHAT KS Demo from time import sleep t = 0.2 import anyio.GPIO as GPIO RED = 22 YEL = 23 GRN = 24 BUZZ = 5 BUTT = 25 GPIO.setmode(GPIO.BCM) GPIO.setup(RED, GPIO.OUT) GPIO.setup(YEL, GPIO.OUT) GPIO.setup(GRN, GPIO.OUT) GPIO.setup(BUZZ,GPIO.OUT) GPIO.setup(BUTT,GPIO.IN) try: while True: GPIO.output(RED, True) sleep(t) GPIO.output(YEL, True) sleep(t) GPIO.output(GRN, True) sleep(2) GPIO.output(RED, False) sleep(t) GPIO.output(YEL, False) sleep(t) GPIO.output(GRN, False)
# BIG BANG - COUNTDOWN # Import modules import mcpi.minecraft as minecraft import mcpi.block as block import time #import RPi.GPIO as GPIO import anyio.GPIO as GPIO # GPIO pins on breadboard BANG = 15 # Setup mc = minecraft.Minecraft.create() GPIO.setmode(GPIO.BCM) # use broadcom GPIO numbers GPIO.setup(BANG, GPIO.OUT) pos = mc.player.getTilePos() mc.setBlock(pos.x + 1, pos.y, pos.z, block.GOLD_BLOCK.id) mc.postToChat("hit that gold block!") # Main loop while True: time.sleep(0.1) # Input Sensing bang = False events = mc.events.pollBlockHits() for e in events: pos = e.pos b = mc.getBlock(pos.x, pos.y, pos.z)
#TrafficHAT KS Demo from time import sleep t = 0.1 import anyio.GPIO as GPIO gpios = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27] GPIO.setmode(GPIO.BCM) while True: for cGPIO in gpios: print("Testing GPIO", cGPIO); GPIO.setup(cGPIO,GPIO.OUT) raw_input("Press enter to continue") GPIO.output(cGPIO,1) sleep(1) for cGPIO in gpios: #sleep(t) raw_input("Press enter to continue") GPIO.output(cGPIO,0) #sleep(t) sleep(1)
#Pibrella Demo from time import sleep t = 0.2 import anyio.GPIO as GPIO RED = 27 YEL = 17 GRN = 4 BUZZ = 18 OUT1 = 22 OUT2 = 23 OUT3 = 24 OUT4 = 25 GPIO.setmode(GPIO.BCM) GPIO.setup(RED, GPIO.OUT) GPIO.setup(YEL, GPIO.OUT) GPIO.setup(GRN, GPIO.OUT) GPIO.setup(OUT1, GPIO.OUT) GPIO.setup(OUT2, GPIO.OUT) GPIO.setup(OUT3, GPIO.OUT) GPIO.setup(OUT4, GPIO.OUT) try: while True: GPIO.output(RED, True) sleep(t) GPIO.output(YEL, True) sleep(t) GPIO.output(GRN, True) sleep(t)
import anyio.GPIO as GPIO #Import GPIO library import time #Import time library GPIO.setmode(GPIO.BCM) #Set GPIO pin numbering TRIG = 23 #Associate pin 23 to TRIG ECHO = 24 #Associate pin 24 to ECHO print "Distance measurement in progress" GPIO.setup(TRIG,GPIO.OUT) #Set pin as GPIO out GPIO.setup(ECHO,GPIO.IN) #Set pin as GPIO in while True: GPIO.output(TRIG, False) #Set TRIG as LOW print "Waitng For Sensor To Settle" time.sleep(2) #Delay of 2 seconds GPIO.output(TRIG, True) #Set TRIG as HIGH time.sleep(0.00001) #Delay of 0.00001 seconds GPIO.output(TRIG, False) #Set TRIG as LOW while GPIO.input(ECHO)==0: #Check whether the ECHO is LOW pulse_start = time.time() #Saves the last known time of LOW pulse while GPIO.input(ECHO)==1: #Check whether the ECHO is HIGH pulse_end = time.time() #Saves the last known time of HIGH pulse pulse_duration = pulse_end - pulse_start #Get pulse duration to a variable distance = pulse_duration * 17150 #Multiply pulse duration by 17150 to get distance
# # Test flashing an LED import time TIME = 0.5 # Raspberry PI #import RPi.GPIO as GPIO #LED = 15 # Arduino import anyio.GPIO as GPIO LED = 15 GPIO.setmode(GPIO.BCM) GPIO.setup(LED, GPIO.OUT) def flash(t): GPIO.output(LED, True) time.sleep(t) GPIO.output(LED, False) time.sleep(t) try: while True: flash(TIME) finally: GPIO.cleanup() # END
def setup(): GPIO.setmode(GPIO.BCM) GPIO.setup(BUTTON, GPIO.IN)
def main(): # Main program block #GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) # Use BCM GPIO numbers GPIO.setup(LCD_E, GPIO.OUT) # E GPIO.setup(LCD_RS, GPIO.OUT) # RS GPIO.setup(LCD_D4, GPIO.OUT) # DB4 GPIO.setup(LCD_D5, GPIO.OUT) # DB5 GPIO.setup(LCD_D6, GPIO.OUT) # DB6 GPIO.setup(LCD_D7, GPIO.OUT) # DB7 # Initialise display lcd_init() while True: # Send some test lcd_string("RTk.GPIO",LCD_LINE_1) lcd_string("16x2 LCD Test",LCD_LINE_2) time.sleep(3) # 3 second delay # Send some text lcd_string("1234567890123456",LCD_LINE_1) lcd_string("abcdefghijklmnop",LCD_LINE_2) time.sleep(3) # 3 second delay # Send some text lcd_string("Code by",LCD_LINE_1) lcd_string("RPiSPY",LCD_LINE_2) time.sleep(3) # Send some text lcd_string("Wow, Very LCD",LCD_LINE_1) lcd_string("Such Magic",LCD_LINE_2) time.sleep(3) lcd_string("All done on a", LCD_LINE_1) lcd_string("Desktop Computer", LCD_LINE_2) time.sleep(3)
from anyio import GPIO from time import sleep #Setup GPIO.setmode(GPIO.BCM) GPIO.setup(17,GPIO.OUT) GPIO.setup(18,GPIO.OUT) i = 0 try: while (i<10): print("Forward") GPIO.output(17,1) sleep(1) GPIO.output(17,0) print("Backward") GPIO.output(18,1) sleep(1) GPIO.output(18,0) i=i+1 finally: GPIO.cleanup()
#TrafficHAT KS Demo from time import sleep t = 0.1 import anyio.GPIO as GPIO gpios = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27 ] GPIO.setmode(GPIO.BCM) while True: for cGPIO in gpios: print("Testing GPIO", cGPIO) GPIO.setup(cGPIO, GPIO.OUT) raw_input("Press enter to continue") GPIO.output(cGPIO, 1) sleep(1) for cGPIO in gpios: #sleep(t) raw_input("Press enter to continue") GPIO.output(cGPIO, 0) #sleep(t) sleep(1)
import time t = 0.2 # RTk.GPIO import anyio.GPIO as GPIO RED = 22 YEL = 23 GRN = 24 BUZZ = 5 BTN = 25 GPIO.setmode(GPIO.BCM) GPIO.setup(RED, GPIO.OUT) GPIO.setup(YEL, GPIO.OUT) GPIO.setup(GRN, GPIO.OUT) try: while True: GPIO.output(RED, True) time.sleep(t) GPIO.output(YEL, True) time.sleep(t) GPIO.output(GRN, True) time.sleep(2) GPIO.output(RED, False) time.sleep(t) GPIO.output(YEL, False)
from anyio import GPIO from time import sleep #Setup GPIO.setmode(GPIO.BCM) GPIO.setup(17, GPIO.OUT) GPIO.setup(18, GPIO.OUT) i = 0 try: while (i < 10): print("Forward") GPIO.output(17, 1) sleep(1) GPIO.output(17, 0) print("Backward") GPIO.output(18, 1) sleep(1) GPIO.output(18, 0) i = i + 1 finally: GPIO.cleanup()
# clears blocks surrounding player for specified radius with button import mcpi.minecraft as minecraft import mcpi.block as block import time # import RPi.GPIO as GPIO import anyio.GPIO as GPIO clearButton = 5 GPIO.setmode(GPIO.BCM) GPIO.setup(clearButton, GPIO.IN) ON = False mc = minecraft.Minecraft.create() # clears blocks around player. Great for mining or getting through walls. def clearNear(x, y, z): radius = 20 mc.setBlocks(x-radius, y-radius, z-radius, x+radius, y+radius+1, z+radius, block.AIR.id) try: while True: time.sleep(0.1) if GPIO.input(clearButton) == False: pos = mc.player.getTilePos() clearNear(pos.x, pos.y, pos.z) finally: GPIO.cleanup()
#Set the modes RPIGPIO.setmode(RPIGPIO.BCM) RTKGPIO.setmode(RTKGPIO.BCM) # #Define GPIO pins gpios = [4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25] #2,3,26 and 27 removed for now errorPins = [] print("Setting up GPIO Outs on the RTK Board") #Setup the RPi for gpio in gpios: print(gpio) RTKGPIO.setup(gpio, RTKGPIO.IN) #sleep(0.1) print("Setting up GPIO Ins on the RPi Board") for gpio in gpios: print(gpio) RPIGPIO.setup(gpio, RPIGPIO.OUT) print("Now Testing") for gpio in gpios: print("Testing GPIO%s",str(gpio)) print("Turning off") RPIGPIO.output(gpio,0) sleep(0.1) print("Reading input") input1 = RTKGPIO.input(gpio)
RTKGPIO.setmode(RTKGPIO.BCM) # #Define GPIO pins gpios = [ 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 ] #2,3,26 and 27 removed for now errorPins = [] print("Setting up GPIO Outs on the RTK Board") #Setup the RPi for gpio in gpios: print(gpio) RTKGPIO.setup(gpio, RTKGPIO.IN) #sleep(0.1) print("Setting up GPIO Ins on the RPi Board") for gpio in gpios: print(gpio) RPIGPIO.setup(gpio, RPIGPIO.OUT) print("Now Testing") for gpio in gpios: print("Testing GPIO%s", str(gpio)) print("Turning off") RPIGPIO.output(gpio, 0) sleep(0.1) print("Reading input") input1 = RTKGPIO.input(gpio)
#import RPi.GPIO as GPIO import anyio.GPIO as GPIO #LED_PINS = [10,22,25,8,7,9,11,15] #Pi LED_PINS = [7,6,14,16,10,8,9,15] GPIO.setmode(GPIO.BCM) ON = False # False = common-anode for g in LED_PINS: GPIO.setup(g, GPIO.OUT) pattern = [True, True, True, True, True, True, True, False] #ABCDEFG (no decimal point) for g in range(8): if pattern[g]: GPIO.output(LED_PINS[g], ON) else: GPIO.output(LED_PINS[g], not ON) raw_input("finished?") GPIO.cleanup()
# Import necessary modules import time #import RPi.GPIO as GPIO # use this for Raspberry Pi import anyio.GPIO as GPIO # use this for Arduino on PC/Mac # This is the GPIO number that the LED is attached to # It's not the same as the pin number on the connector though! LED = 2 # Use Broadcom pin numbering (GPIO numbers, not connector pin numbers) GPIO.setmode(GPIO.BCM) # Configure the LED GPIO to be an output, so that you can # change the voltage on it and thus turn the LED on and off GPIO.setup(LED, GPIO.OUT) # Define a function that flashes the LED once # The 't' variable holds the time to flash for def flash(t): # True puts 3.3 Volts on the pin connected to the LED, the LED goes on GPIO.output(LED, True) time.sleep(t) # False puts 0 Volts on the pin connected to the LED, the LED goes off GPIO.output(LED, False) time.sleep(t) print('false') # Game loop try: # Try to run this code, jump to "finally" if it fails
# BIG BANG - COUNTDOWN # Import modules import mcpi.minecraft as minecraft import mcpi.block as block import time #import RPi.GPIO as GPIO import anyio.GPIO as GPIO # GPIO pins on breadboard BANG = 15 # Setup mc = minecraft.Minecraft.create() GPIO.setmode(GPIO.BCM) # use broadcom GPIO numbers GPIO.setup(BANG, GPIO.OUT) pos = mc.player.getTilePos() mc.setBlock(pos.x+1, pos.y, pos.z, block.GOLD_BLOCK.id) mc.postToChat("hit that gold block!") # Main loop while True: time.sleep(0.1) # Input Sensing bang = False events = mc.events.pollBlockHits() for e in events: pos = e.pos b = mc.getBlock(pos.x, pos.y, pos.z)
#Set the modes RPIGPIO.setmode(RPIGPIO.BCM) RTKGPIO.setmode(RTKGPIO.BCM) # #Define GPIO pins gpios = [4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25] #2,3,26 and 27 removed for now errorPins = [] print("Setting up GPIO Outs on the RTK Board") #Setup the RPi for gpio in gpios: print(gpio) RTKGPIO.setup(gpio, RTKGPIO.OUT) #sleep(0.1) print("Setting up GPIO Ins on the RPi Board") for gpio in gpios: print(gpio) RPIGPIO.setup(gpio, RPIGPIO.IN,pull_up_down=RPIGPIO.PUD_DOWN) print("Now Testing") for gpio in gpios: print("Testing GPIO%s",str(gpio)) print("Turning off") RTKGPIO.output(gpio,0) sleep(0.1) print("Reading input") input1 = RPIGPIO.input(gpio)
import mcpi.minecraft as minecraft import mcpi.block as block import time import countdown_mac as countdown #import RPi.GPIO as GPIO # Raspberry Pi import anyio.GPIO as GPIO # Arduino BUTTON = 4 GPIO.setmode(GPIO.BCM) GPIO.setup(BUTTON, GPIO.IN) mc = minecraft.Minecraft.create() mc.postToChat("PRESS THE BUTTON!") def bomb(x, y, z): mc.setBlock(x+1, y, z+1, block.TNT.id) for a in range(5, 0, -1): mc.postToChat(str(a)) time.sleep(1) ####countdown.count() mc.postToChat("BANG!") mc.setBlocks(x-10, y-5, z-10, x+10, y+10, z+10, block.AIR.id) ####mc.player.setTilePos(x, y+40, z) ####countdown.bang() try: while True: time.sleep(0.1)
import mcpi.minecraft as minecraft import mcpi.block as block import mcpi.minecraftstuff as minecraftstuff import time import random import thread import anyio.seg7 as display import anyio.GPIO as GPIO BUTTON = 4 LED_PINS = [7,6,14,16,10,8,9,15] GPIO.setmode(GPIO.BCM) GPIO.setup(BUTTON, GPIO.IN) ON = False # common-anode, set to True for common cathode display.setup(GPIO, LED_PINS, ON) #arena constants ARENAX = 20 ARENAZ = 30 ARENAY = 5 RIVERWIDTH = 6 def createArena(pos): mc = minecraft.Minecraft.create() pos.x mc.setBlocks(pos.x - 1, pos.y, pos.z - 1, pos.x + ARENAX + 1, pos.y - 3, pos.z + ARENAZ + 1, block.GRASS.id) mc.setBlocks(pos.x - 1, pos.y + 1, pos.z - 1, pos.x + ARENAX + 1, pos.y + ARENAY, pos.z + ARENAZ + 1,
#TrafficHAT KS Demo from time import sleep t = 0.2 import anyio.GPIO as GPIO RED = 22 YEL = 23 GRN = 24 BUZZ = 5 BUTT = 25 GPIO.setmode(GPIO.BCM) GPIO.setup(RED, GPIO.OUT) GPIO.setup(YEL, GPIO.OUT) GPIO.setup(GRN, GPIO.OUT) GPIO.setup(BUZZ, GPIO.OUT) GPIO.setup(BUTT, GPIO.IN) try: while True: GPIO.output(RED, True) sleep(t) GPIO.output(YEL, True) sleep(t) GPIO.output(GRN, True) sleep(2) GPIO.output(RED, False) sleep(t) GPIO.output(YEL, False) sleep(t) GPIO.output(GRN, False)