Exemplo n.º 1
0
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()
Exemplo n.º 2
0
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()
Exemplo n.º 3
0
def lcd_toggle_enable():
  # Toggle enable
  time.sleep(E_DELAY)
  GPIO.output(LCD_E, True)
  time.sleep(E_PULSE)
  GPIO.output(LCD_E, False)
  time.sleep(E_DELAY)
Exemplo n.º 4
0
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)
Exemplo n.º 5
0
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)
Exemplo n.º 6
0
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')
Exemplo n.º 7
0
def loop():
  old_first  = False
  old_left   = False
  old_right  = False
  old_last   = False
  index = 0
  
  while True:
    # POLL BUTTONS
    # remember they are inverted
    first  = not GPIO.input(BUTTON_FIRST)
    left   = not GPIO.input(BUTTON_LEFT)
    right  = not GPIO.input(BUTTON_RIGHT)
    last   = not GPIO.input(BUTTON_LAST)
    
    # REPORT ANY CHANGED BUTTONS
    if first != old_first:
      print("FIRST=" + str(first))
      old_first = first
    if left != old_left:
      print("LEFT=" + str(left))
      old_left = left
    if right != old_right:
      print("RIGHT=" + str(right))
      old_right = right
    if last != old_last:
      print("LAST=" + str(last))
      old_last = last
      
      
    # PROCESS HELD BUTTONS IN PRIORITY ORDER
    if first:
      index = 0
      
    elif last:
      index = len(LED_GPIO)-1
      
    elif left:
      if index > 0:
        index -= 1
      
    elif right:
      if index < len(LED_GPIO)-1:
        index += 1

        
    # FLASH PRESENTLY SELECTED LED
    GPIO.output(LED_GPIO[index], True)
    time.sleep(FLASH_TIME/2)
    GPIO.output(LED_GPIO[index], False)
    time.sleep(FLASH_TIME/2)
Exemplo n.º 8
0
def loop():
    old_first = False
    old_left = False
    old_right = False
    old_last = False
    index = 0

    while True:
        # POLL BUTTONS
        # remember they are inverted
        first = not GPIO.input(BUTTON_FIRST)
        left = not GPIO.input(BUTTON_LEFT)
        right = not GPIO.input(BUTTON_RIGHT)
        last = not GPIO.input(BUTTON_LAST)

        # REPORT ANY CHANGED BUTTONS
        if first != old_first:
            print("FIRST=" + str(first))
            old_first = first
        if left != old_left:
            print("LEFT=" + str(left))
            old_left = left
        if right != old_right:
            print("RIGHT=" + str(right))
            old_right = right
        if last != old_last:
            print("LAST=" + str(last))
            old_last = last

        # PROCESS HELD BUTTONS IN PRIORITY ORDER
        if first:
            index = 0

        elif last:
            index = len(LED_GPIO) - 1

        elif left:
            if index > 0:
                index -= 1

        elif right:
            if index < len(LED_GPIO) - 1:
                index += 1

        # FLASH PRESENTLY SELECTED LED
        GPIO.output(LED_GPIO[index], True)
        time.sleep(FLASH_TIME / 2)
        GPIO.output(LED_GPIO[index], False)
        time.sleep(FLASH_TIME / 2)
Exemplo n.º 9
0
def step():
    global running, StepCounter, stepsToDo
    while running and stepsToDo > 0:
        for pin in range(0, 4):
            xpin = StepPins[pin]  # Get GPIO
            if Seq[StepCounter][pin] != 0:
                GPIO.output(xpin, True)
            else:
                GPIO.output(xpin, False)
        StepCounter += StepDir
        if (StepCounter >= StepCount):
            StepCounter = 0
        if (StepCounter < 0):
            StepCounter = StepCount + StepDir
        stepsToDo -= 1
        #print stepsToDo
        time.sleep(WaitTime)
    # clear the output pins
    for pin in StepPins:
        GPIO.output(pin, False)
    running = False
Exemplo n.º 10
0
def step():
  global running, StepCounter, stepsToDo
  while running and stepsToDo>0:
    for pin in range(0,4):
      xpin = StepPins[pin]# Get GPIO
      if Seq[StepCounter][pin]!= 0:
        GPIO.output(xpin, True)
      else:
        GPIO.output(xpin, False)   
    StepCounter += StepDir
    if (StepCounter>=StepCount):
      StepCounter = 0
    if (StepCounter<0):
      StepCounter = StepCount + StepDir
    stepsToDo -= 1
    #print stepsToDo
    time.sleep(WaitTime)
  # clear the output pins
  for pin in StepPins:
    GPIO.output(pin, False)
  running = False
Exemplo n.º 11
0
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()
Exemplo n.º 12
0
def flash(t):
    GPIO.output(LED, True)
    time.sleep(t)
    GPIO.output(LED, False)
    time.sleep(t)
Exemplo n.º 13
0
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)
        GPIO.output(OUT1, True)
        sleep(t)
        GPIO.output(OUT2, True)
        sleep(t)
        GPIO.output(OUT3, True)
        sleep(t)
        GPIO.output(OUT4, True)
        sleep(t)
        GPIO.output(RED, False)
        sleep(t)
Exemplo n.º 14
0
def flash(t):
  GPIO.output(LED, True)
  time.sleep(t)
  GPIO.output(LED, False)
  time.sleep(t)
Exemplo n.º 15
0
#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)
Exemplo n.º 16
0
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)
		sleep(2)
		GPIO.output(BUZZ, True)
		sleep(t)
		GPIO.output(BUZZ, False)
	
Exemplo n.º 17
0
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()
Exemplo n.º 18
0
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)
	time.sleep(t)
	GPIO.output(GRN, False)
	time.sleep(2)   
finally:
  GPIO.cleanup()
  
# END
Exemplo n.º 19
0
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)

	finally:
		GPIO.cleanup()
Exemplo n.º 20
0
def lcd_byte(bits, mode):
  # Send byte to data pins
  # bits = data
  # mode = True  for character
  #        False for command

  GPIO.output(LCD_RS, mode) # RS

  # High bits
  GPIO.output(LCD_D4, False)
  GPIO.output(LCD_D5, False)
  GPIO.output(LCD_D6, False)
  GPIO.output(LCD_D7, False)
  if bits&0x10==0x10:
    GPIO.output(LCD_D4, True)
  if bits&0x20==0x20:
    GPIO.output(LCD_D5, True)
  if bits&0x40==0x40:
    GPIO.output(LCD_D6, True)
  if bits&0x80==0x80:
    GPIO.output(LCD_D7, True)

  # Toggle 'Enable' pin
  lcd_toggle_enable()

  # Low bits
  GPIO.output(LCD_D4, False)
  GPIO.output(LCD_D5, False)
  GPIO.output(LCD_D6, False)
  GPIO.output(LCD_D7, False)
  if bits&0x01==0x01:
    GPIO.output(LCD_D4, True)
  if bits&0x02==0x02:
    GPIO.output(LCD_D5, True)
  if bits&0x04==0x04:
    GPIO.output(LCD_D6, True)
  if bits&0x08==0x08:
    GPIO.output(LCD_D7, True)

  # Toggle 'Enable' pin
  lcd_toggle_enable()
Exemplo n.º 21
0
#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)

	print("Turning on")
	RTKGPIO.output(gpio,True)
	sleep(0.1)
	print("Reading input")
	input2 = RPIGPIO.input(gpio)	
	if(input1 == 0 and input2 ==1):
		print("GPIO Pin Passed")
	else:
		errorPins.append(gpio)
		print("GPIO Pin Failed")
		print(input1)
Exemplo n.º 22
0
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
Exemplo n.º 23
0
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)
        bang = (b == block.GOLD_BLOCK.id)

    # Output Control
    if bang:
        for v in range(5,0,-1):
            mc.postToChat(str(v))
            time.sleep(1)
            
        mc.postToChat("BANG!")
        GPIO.output(BANG, True)
        mc.setBlock(pos.x-10, pos.y-5, pos.z-10, pos.x+10, pos.y+10, pos.z+10, block.AIR.id)
        time.sleep(3)
        GPIO.output(BANG, False)
        
# END
Exemplo n.º 24
0
#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()
Exemplo n.º 25
0
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)
		GPIO.output(OUT1, True)
		sleep(t)
		GPIO.output(OUT2, True)
		sleep(t)
		GPIO.output(OUT3, True)
		sleep(t)
		GPIO.output(OUT4, True)
		sleep(t)
		GPIO.output(RED, False)
		sleep(t)
Exemplo n.º 26
0
    self.delayMicroseconds(1)		# commands need > 37us to settle


  def message(self, text):
    """ Send string to LCD. Newline wraps to second line"""

    for char in text:
      if char == '\n':
        self.write4bits(0xC0) # next line
      else:
        self.write4bits(ord(char),True)


if __name__ == '__main__':
  
  gpio.output(2, True)
  
  lcd = CharLCD()

  lcd.clear()
  lcd.message(("\\"*16) + "\n" + ("\\"*16))
  
  sleep(5)
  
  lcd.clear()
  lcd.message("       :)       \n       :)       ")
  
  sleep(3)
  
  lcd.clear()
  lcd.message(("\\"*16) + "\n" + ("\\"*16))
Exemplo n.º 27
0
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
Exemplo n.º 28
0
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)
        bang = (b == block.GOLD_BLOCK.id)

    # Output Control
    if bang:
        for v in range(5, 0, -1):
            mc.postToChat(str(v))
            time.sleep(1)

        mc.postToChat("BANG!")
        GPIO.output(BANG, True)
        mc.setBlock(pos.x - 10, pos.y - 5, pos.z - 10, pos.x + 10, pos.y + 10,
                    pos.z + 10, block.AIR.id)
        time.sleep(3)
        GPIO.output(BANG, False)

# END
Exemplo n.º 29
0
#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)