def run(self): self.on = True i = 0 j = 0 while (self.on): j = i i = (pz.readInput(2) + pz.readInput(3)) / 2 if i != j: self.value = self.value + 1 time.sleep(0.01)
def run(self): self.on = True i = 0 j = 0 while (self.on): j = i i = pz.readInput(2) if i != j: self.value = self.value + 1 print(self.value) time.sleep(0.01)
def speedrun(): pz.stop() mylcd.lcd_display_string("Speed Run ", 1) mylcd.lcd_display_string("Press E to End ", 2) time.sleep(2) speed = 100 GO = 1 PREP = 1 while PREP == 1: #get ready to go for event in get_key(): mylcd.lcd_display_string("Press G to GO ", 1) mylcd.lcd_display_string("Press E to End ", 2) if event.code == "KEY_G": PREP = 0 elif event.code == "KEY_E": pz.stop GO = 0 PREP = 0 while GO == 1: #new simple quit - works perfectly if button.is_pressed: pz.stop() GO = 0 else: RIGHTIR = pz.readInput(0) LEFTIR = pz.readInput(1) if RIGHTIR == 0: pz.spinLeft(75) mylcd.lcd_display_string("Right IR on ", 1) mylcd.lcd_display_string("Use switch=STOP ", 2) elif LEFTIR == 0: pz.spinRight(75) mylcd.lcd_display_string("left IR on ", 1) mylcd.lcd_display_string("Use switch=STOP ", 2) elif RIGHTIR == 1 and LEFTIR == 1: pz.forward(100) mylcd.lcd_display_string("No IR on ", 1) mylcd.lcd_display_string("Use switch=STOP ", 2)
def linefollower(): #works perfectly on a new set of batteries mylcd.lcd_display_string("Line Follower ", 1) mylcd.lcd_display_string("Press E to End ", 2) PREP = 1 GO = 1 LFSPEED = 75 #only changes cornering speed pz.stop() while PREP == 1: #setup ready for line following for event in get_key(): mylcd.lcd_display_string("Press G to GO ", 1) mylcd.lcd_display_string("Press E to End ", 2) if event.code == "KEY_G": PREP = 0 elif event.code == "KEY_E": GO = 0 PREP = 0 while GO == 1: #new simple quit - works perfectly if button.is_pressed: pz.stop() GO = 0 else: mylcd.lcd_display_string("GO!!!!!!!!!!! ", 1) mylcd.lcd_display_string("Switch = stop ", 2) LEFTLINE = pz.readInput(2) #assign right line sensor to a variable RIGHTLINE = pz.readInput(3) #assign left line sensor to a variable if RIGHTLINE == 1: pz.spinRight(LFSPEED) #time.sleep(0.5) elif LEFTLINE == 1: pz.spinLeft(LFSPEED) #time.sleep(0.5) elif LEFTLINE == 0 and RIGHTLINE == 0: pz.forward(15) #time.sleep(0.5) elif LEFTLINE == 1 and RIGHTLINE == 1: pz.reverse(15)
def run(self): tick = False tickp = False count = 0 self.continueLoop = False #encoder tick counter loop flag while self.encoderLoop: self.event.wait( ) #reduce threading cpu resource by only looping when necessary if not self.q.empty(): #Test for new commands in the queue #self.continueLoop = True data = self.q.get( ) #get command tuple from queue and parse data pwm = data[0] cmd = data[1] self.continueLoop = True motor(self.servo_port, pwm) else: self.event.clear() while self.continueLoop: #position control feedback loop #print(str(self.threshold)) val = pz.readInput(self.analog_port) if (val < self.threshold): tick = True else: tick = False if (tick != tickp): count = count + 1 tickp = tick if ((cmd - count) < 0 ): #get next command after completion of previous command count = 0 tick = False tickp = False self.continueLoop = False
def automaze(): pz.stop() mylcd.lcd_display_string("Auto Maze ", 1) mylcd.lcd_display_string("Press E to End ", 2) time.sleep(2) MSPEED = 20 MTURN = 90 GO = 1 PREP = 1 STEP = 0 # start step count while PREP == 1: #get ready to go for event in get_key(): mylcd.lcd_display_string("Press G to GO ", 1) mylcd.lcd_display_string("Press E to End ", 2) if event.code == "KEY_G": PREP = 0 elif event.code == "KEY_E": GO = 0 PREP = 0 while GO == 1: #new simple quit - works perfectly if button.is_pressed: pz.stop() GO = 0 else: RIGHTIR = pz.readInput(0) #assign right IR to a variable LEFTIR = pz.readInput(1) #assign left IR to a variable RANGE = hcsr04.getDistance() #assign HC-SR04 range to variable mylcd.lcd_display_string("Range = %d %%" % RANGE, 1) mylcd.lcd_display_string("Step = %d %%" % STEP, 2) pz.forward(MSPEED) #steps to follow to complete the maze if RANGE < 25 and STEP == 0: #first right turn pz.stop() pz.spinRight(MTURN) time.sleep(0.5) pz.stop() pz.forward(MSPEED) time.sleep(1) STEP = 1 mylcd.lcd_display_string("Range = %d %%" % RANGE, 1) mylcd.lcd_display_string("Step = %d %%" % STEP, 2) RANGE = hcsr04.getDistance() #assign HC-SR04 range to variable if RANGE < 25 and STEP == 1: #second right turn pz.stop() pz.spinRight(MTURN) time.sleep(0.5) pz.stop() pz.forward(MSPEED) time.sleep(1) STEP = 2 mylcd.lcd_display_string("Range = %d %%" % RANGE, 1) mylcd.lcd_display_string("Step = %d %%" % STEP, 2) RANGE = hcsr04.getDistance() #assign HC-SR04 range to variable if RANGE < 25 and STEP == 2: #third right pz.stop() pz.spinRight(MTURN) time.sleep(0.5) pz.stop() pz.forward(MSPEED) time.sleep(1) STEP = 3 mylcd.lcd_display_string("Range = %d %%" % RANGE, 1) mylcd.lcd_display_string("Step = %d %%" % STEP, 2) RANGE = hcsr04.getDistance() #assign HC-SR04 range to variable if RANGE < 25 and STEP == 3: #first left pz.stop() pz.spinLeft(MTURN) time.sleep(0.5) pz.stop() pz.forward(MSPEED) time.sleep(1) STEP = 4 mylcd.lcd_display_string("Range = %d %%" % RANGE, 1) mylcd.lcd_display_string("Step = %d %%" % STEP, 2) RANGE = hcsr04.getDistance() #assign HC-SR04 range to variable if RANGE < 25 and STEP == 4: #second left pz.stop() pz.spinLeft(MTURN) time.sleep(0.5) pz.stop() pz.forward(MSPEED) time.sleep(1) STEP = 5 mylcd.lcd_display_string("Range = %d %%" % RANGE, 1) mylcd.lcd_display_string("Step = %d %%" % STEP, 2) RANGE = hcsr04.getDistance() #assign HC-SR04 range to variable if RANGE < 25 and STEP == 5: #third left pz.stop() pz.spinLeft(MTURN) time.sleep(0.5) pz.stop() pz.forward(MSPEED) time.sleep(1) STEP = 6 mylcd.lcd_display_string("Range = %d %%" % RANGE, 1) mylcd.lcd_display_string("Step = %d %%" % STEP, 2) RANGE = hcsr04.getDistance() #assign HC-SR04 range to variable #emergency wall avoidance protocol if LEFTIR == 0: pz.spinRight(100) time.sleep(0.01) elif RIGHTIR == 0: pz.spinLeft(100) time.sleep(0.01)
import I2C_LCD_driver #I2C LCD driver from http://www.circuitbasics.com/raspberry-pi-i2c-lcd-set-up-and-programming/ #____________________________________________________________________________________ #hardware setup mylcd = I2C_LCD_driver.lcd() #assign LCD to variable for ease of use pz.init() #initiate hardware pz.setInputConfig(0, 0) #right IR sensor is input 0 and digital pz.setInputConfig(1, 0) #left IR sensor is input 1 and digital pz.setInputConfig(2, 0) #right line sensor is input 2 and digital pz.setInputConfig(3, 0) #left line is input 3 and digital RIGHTIR = pz.readInput(0) #assign right IR to a variable LEFTIR = pz.readInput(1) #assign left IR to a variable RIGHTLINE = pz.readInput(2) #assign right line sensor to a variable LEFTLINE = pz.readInput(3) #assign left line sensor to a variable hcsr04.init() #initiate hardware RANGE = hcsr04.getDistance() #assign HC-SR04 range to variable button = Button(22) #end of hardware setup #______________________________________________________________________________ #____________________________________________________________________________________ #functions for individual tasks
import piconzero as pz import time pz.setInputConfig(2, 0) pz.init() try: while True: if pz.readInput(2): print "IR droite : Black" pz.stop() time.sleep(1) break else: print "IR droite : White" time.sleep(1) pz.reverse(35) if pz.readInput(3): print "IR gauche : Black" pz.stop() time.sleep(1) break else: print "IR gauche : White" pz.reverse(35) time.sleep(1) except KeyboardInterrupt: print finally: pz.cleanup()
# GNU GPL V3 # Test code for 4tronix Picon Zero import piconzero as pz, time pz.init() pz.setInputConfig(0, 0, True) # request pullup on input vsn = pz.getRevision() if (vsn[1] == 2): print "Board Type:", "Picon Zero" else: print "Board Type:", vsn[1] print "Firmware version:", vsn[0] print try: while True: switch = pz.readInput(0) if (switch == 0): print "Switch Pressed", switch else: print "Switch Released", switch time.sleep(5) except KeyboardInterrupt: print finally: pz.cleanup()
# Test code for 4tronix Picon Zero from __future__ import absolute_import from __future__ import print_function import piconzero as pz, time pz.init() pz.setInputConfig(0, 0, True) # request pullup on input vsn = pz.getRevision() if (vsn[1] == 2): print("Board Type:", "Picon Zero") else: print("Board Type:", vsn[1]) print("Firmware version:", vsn[0]) print() try: while True: switch = pz.readInput(0) if (switch == 0): print("Switch Pressed", switch) else: print("Switch Released", switch) time.sleep(5) except KeyboardInterrupt: print() finally: pz.cleanup()
# requires picon zero hardware plus software library # see circuitry in article at rbnrpi.wordpress.com import piconzero as pz, time pz.init() pz.setInputConfig( 1, 0, True ) # used for push button digital input (normally high using internal resistor) pz.setOutputConfig(5, 3) # set output 5 to WS2812 pz.setInputConfig(0, 1) # set to Analogue used to sample audio try: pz.setAllPixels(128, 0, 128) #set an initial pixel state pz.setPixel(15, 0, 255, 0, False) #eye pz.setPixel(16, 0, 255, 0) #other eye pz.setPixel(14, 0, 0, 255) #nose while True: v = pz.readInput(0) # read adc value from audio input print(v) #print to terminal pz.setBrightness(v) #update brightness pz.updatePixels() #update all pixels time.sleep( 0.01 ) #short sleep to keep repsonse time good, just enough to allow ctrl-C to get a look in except KeyboardInterrupt: print finally: pz.cleanup() #reset picon zero board
import piconzero as pz import time pz.setInputConfig(2, 0) pz.init() try: while True: test = pz.readInput(2) if pz.readInput(2): print "IR droite : Black /test ", test time.sleep(1) else: print "IR droite : White /test", test time.sleep(1) if pz.readInput(3): print "IR gauche : Black" time.sleep(1) else: print "IR gauche : White" time.sleep(1) finally: pz.cleanup()
#! /usr/bin/env python # GNU GPL V3 # Test code for 4tronix Picon Zero import piconzero as pz, time pz.init() pz.setInputConfig(0, 2) # set input 0 to DS18B20 pz.setInputConfig(2, 2) # set input 2 to DS18B20 try: while True: ana0 = pz.readInput(0) if (ana0>32767): ana0 -= 65536 ana2 = pz.readInput(2) if (ana2>32767): ana2 -= 65536 print ana0*0.0625, ana2*0.0625 time.sleep(1) except KeyboardInterrupt: print finally: pz.cleanup()
from __future__ import absolute_import import piconzero as pz, time pz.setInputConfig(0, 1) # set input 0 to Analog pz.setOutputConfig(0, 1) # set output 0 to PWM pz.setOutputConfig(2, 2) # set output 2 to Servo pz.setOutputConfig(5, 3) # set output 5 to WS2812 while True: ana0 = pz.readInput(0) pz.setOutput(0, ana0/10) pz.setPixel(0,0,0,ana0/4) pz.setOutput(2, int(ana0/7)) time.sleep(0.1) # this makes 11 active lines, but can be removed
import piconzero as pz, time pz.init() pz.setInputConfig(0, 0) # request pullup on input while True: switch = pz.readInput(0) # 0 = pressed, 1 = not pressed if (switch == 0): print "Switch Pressed", switch else: print "Switch Released", switch time.sleep(1)
def isOnLine(): # 0 on true if pz.readInput(LineSensor.channel) == 0: return True else: return False
def sensor_activated(): return pz.readInput(0)
import piconzero as pz import time pz.setInputConfig(2, 0) pz.setInputConfig(3, 0) pz.init() try: while True: test = pz.readInput(2) if (pz.readInput(2) != 1 and pz.readInput(3) != 1): pz.reverse(100) else: pz.reverse(100) finally: pz.cleanup()
import piconzero as pz, time pz.setInputConfig(0, 1) # set input 0 to Analog pz.setOutputConfig(0, 1) # set output 0 to PWM pz.setOutputConfig(2, 2) # set output 2 to Servo pz.setOutputConfig(5, 3) # set output 5 to WS2812 while True: ana0 = pz.readInput(0) pz.setOutput(0, ana0/10) pz.setPixel(0,0,0,ana0/4) pz.setOutput(2, int(ana0/7)) time.sleep(0.1) # this makes 11 active lines, but can be removed
#! /usr/bin/env python # GNU GPL V3 # Test code for 4tronix Picon Zero from __future__ import absolute_import from __future__ import print_function import piconzero as pz, time pz.init() pz.setInputConfig(0, 2) # set input 0 to DS18B20 pz.setInputConfig(2, 2) # set input 2 to DS18B20 try: while True: ana0 = pz.readInput(0) if (ana0 > 32767): ana0 -= 65536 ana2 = pz.readInput(2) if (ana2 > 32767): ana2 -= 65536 print(ana0 * 0.0625, ana2 * 0.0625) time.sleep(1) except KeyboardInterrupt: print() finally: pz.cleanup()
openslot = int(b / 10) #set openslot cycles according to brightness openFlag = 1 #opening so set flag change(1) #open mouth else: if (opencount > openslot) and ( openFlag == 1 and b < threshold - 20 ): #ensures stays open for at least openslot cycles openFlag = 0 #closing so reset flag #shutslot=2 #doesn't change. set in intialisation shutcount = 0 #reset shutcount change(-1) #close mouth #now process brigthness level v = pz.readInput(0) # read adc value from audio input b = min(100, 5 * (abs(v - offset)) + 5) #calcuate next brigthness print(b) #print to terminal #check for offset calibration button trigger = pz.readInput(1) #check for button pressed if trigger == 0: #normally high: 0 if pushed offset = v #update offset value from current adc value #now adjust brightness if changed sufficiently if abs( blast - b ) > 10: #check if brightness has changed by at least 10 to reduce noise pz.setBrightness(b) #update brightness pz.updatePixels() #update all pixels blast = b #save brightness value
#! /usr/bin/env python # -*- coding: utf-8 -*- import time import piconzero as pz import hcsr04 pz.setInputConfig(2, 0) pz.setInputConfig(3, 0) pz.init() hcsr04.init() speed = 60 i = 0 try: while True: if ((pz.readInput(2) != 1) and (pz.readInput(3) != 1)): pz.spinLeft(100) time.sleep(0.25) elif ((pz.readInput(2) == 1) and (pz.readInput(3) != 1)): pz.spinRight(100) time.sleep(0.25) elif ((pz.readInput(2) == 1) and (pz.readInput(3) == 1)): pz.reverse(speed) time.sleep(0.1) else: pz.reverse(speed) except KeyboardInterrupt: print "Au revoir" finally: pz.cleanup() hcsr04.cleanup()
elif keyp == 'v': pz.stop() pts = [] row=2 scanWin.addstr(1,1,"Distance Map" ) scanWin.addstr(2,1, "Sonar : ir") for span in range( 39, 75, 3 ): irLine="" sonLine="" for stilt in range( 150, 30, -3 ): pz.setOutput (pan, span) pz.setOutput (tilt, stilt) time.sleep( 0.1) ir = pz.readInput(irSen) distance = int(hcsr04.getDistance()) #print "At pan,tilt: ",span,",",stilt,": ir Distance:", ir, " sonic distance: ", distance #volts=min(1,ir*0.0048828125); # // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3 volts=min(1,ir*0.002929688); # // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3 actdist=65*pow(volts, -1.10); # // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk #print "ir act distance: ",actdist # TODO: Save values and pos so build map # rescale reading to ascii value irLine = irLine + asciiSensor( 400, 400-min(400,ir) )
# # Basic test of HC-SR04 ultrasonic sensor on Picon Zero import hcsr04, time import piconzero as pz pz.setInputConfig(2, 0) pz.setInputConfig(3, 0) pz.init() hcsr04.init() try: while True: distance = int(hcsr04.getDistance()) print("Distance : "), distance if (((pz.readInput(2) != 1) or (pz.readInput(3) != 1)) and distance > 13): pz.reverse(60) else: pz.stop() time.sleep(0.2) pz.spinRight(50) time.sleep(0.2) pz.stop() break except KeyboardInterrupt: print finally: hcsr04.cleanup() pz.cleanup()
#! /usr/bin/env python import piconzero as pz, time pz.init() i = 0 j = 0 turns = 0 while True: j = i i = pz.readInput(2) if i != j: turns = turns + 1 print(turns) time.sleep(0.01)