def readCompoundEye(): global irUpValue, irDownValue, irLeftValue, irRightValue, distance # <4> ledPin = 25 gpio.mode(ledPin, "out") # <5> gpio.write(ledPin, gpio.HIGH) #Wait for sensors to get ready time.sleep(0.05) # <6> irUpValue = mcp.readAnalog(0, 0) # <7> irDownValue = mcp.readAnalog(1, 0) irLeftValue = mcp.readAnalog(0, 1) irRightValue = mcp.readAnalog(1, 1) ambientLight = 0 gpio.write(ledPin, gpio.LOW) # <8> time.sleep(0.05) ambientLight = mcp.readAnalog(0, 0) # <8> irUpValue = irUpValue - ambientLight # <9> ambientLight = mcp.readAnalog(1, 0) # <10> irDownValue = irDownValue - ambientLight ambientLight = mcp.readAnalog(0, 1) irLeftValue = irLeftValue - ambientLight ambientLight = mcp.readAnalog(1, 1) irRightValue = irRightValue - ambientLight distance = (irUpValue + irDownValue + irLeftValue + irRightValue) / 4 # <11>
def readCompoundEye(): global irUpValue,irDownValue,irLeftValue,irRightValue,distance # <4> ledPin = 25 gpio.mode(ledPin, "out") # <5> gpio.write(ledPin, gpio.HIGH) #Wait for sensors to get ready time.sleep(0.05) # <6> irUpValue = mcp.readAnalog(0, 0) # <7> irDownValue = mcp.readAnalog(1, 0) irLeftValue = mcp.readAnalog(0, 1) irRightValue = mcp.readAnalog(1, 1) ambientLight = 0 gpio.write(ledPin, gpio.LOW) # <8> time.sleep(0.05) ambientLight = mcp.readAnalog(0, 0) # <8> irUpValue = irUpValue - ambientLight # <9> ambientLight = mcp.readAnalog(1, 0) # <10> irDownValue = irDownValue - ambientLight ambientLight = mcp.readAnalog(0, 1) irLeftValue = irLeftValue - ambientLight ambientLight = mcp.readAnalog(1, 1) irRightValue = irRightValue - ambientLight distance = (irUpValue+irDownValue+irLeftValue+irRightValue)/4 # <11>
def measureDistance(): trigPin = 22 # указываем номер контакта Raspberry Pi, к которому подключен Trig контакт датчика HC-SR04 echoPin = 27 # указываем номер контакта Raspberry Pi, к которому подключен Echo контакт датчика HC-SR04 v = ( 331.5 + 0.6 * 20 ) # скорость звука при температуре 20 градусов Цельсия(вы можете указать свое значение вместо 20) в м/с gpio.mode(trigPin, "out") # устанавливаем контакт как выход gpio.mode(echoPin, "in") # устанавливаем контакт как вход gpio.interruptMode( echoPin, "both" ) # режим прерывания, чтобы функция pulseInHigh вычислила длительность перехода сигнала с 0 до 1 и от 1 до 0 gpio.write(trigPin, gpio.LOW) # устанавливаем низкий уровень сигнала time.sleep(0.5) # задержка в пол секунда gpio.write(trigPin, gpio.HIGH) # устанавливаем высокий уровень сигнала time.sleep(1 / 1000000.0) # задержка в 1 мкс gpio.write(trigPin, gpio.LOW) # устанавливаем низкий уровень сигнала t = gpio.pulseInHigh(echoPin) # вычисляем длительность сигнала d = t * v * 50 # вычисляем пройденное расстояние return d # возвращаем значение
def main(): triggerPin = 25 gpio.mode(triggerPin, "in") # <2> flame = gpio.read(triggerPin) # <3> if(flame == gpio.HIGH): # <4> print "Flame detected" else: print "No flame detected" time.sleep(0.5)
def main(): triggerPin = 25 gpio.mode(triggerPin, "in") # <2> flame = gpio.read(triggerPin) # <3> if (flame == gpio.HIGH): # <4> print "Flame detected" else: print "No flame detected" time.sleep(0.5)
def main(): switchPin = 27 gpio.mode(switchPin, "in") # <2> x = gpio.read(switchPin) # <3> if( x == gpio.LOW ): # <4> print "Something is inside detection range" else: print "There is nothing inside detection range" time.sleep(0.1)
def main(): vibPin = 3 gpio.mode(vibPin, "in") while (True): vibrationInput = gpio.read(vibPin) # <2> if(vibrationInput == gpio.LOW): print "Vibration" time.sleep(5) # <3> time.sleep(0.01) # seconds # <4>
def main(): vibPin = 3 gpio.mode(vibPin, "in") while (True): vibrationInput = gpio.read(vibPin) # <2> if (vibrationInput == gpio.LOW): print "Vibration" time.sleep(5) # <3> time.sleep(0.01) # seconds # <4>
def main(): switchPin = 27 gpio.mode(switchPin, "in") # <2> x = gpio.read(switchPin) # <3> if (x == gpio.LOW): # <4> print "Something is inside detection range" else: print "There is nothing inside detection range" time.sleep(0.1)
def main(): linePin = 23 gpio.mode(linePin, "in") while True: lineState = gpio.read(linePin) # <2> if (lineState == gpio.HIGH): print "Sensor is on the line" else: print "Sensor is off the line" time.sleep(0.5)
def main(): linePin = 23 gpio.mode(linePin, "in") while True: lineState = gpio.read(linePin) # <2> if( lineState == gpio.HIGH ): print "Sensor is on the line" else: print "Sensor is off the line" time.sleep(0.5)
def main(): switchPin = 3 # has internal pull-up # <2> gpio.mode(switchPin, "in") while (True): switchState = gpio.read(switchPin) # <3> if (switchState == gpio.LOW): print "switch triggered" time.sleep(0.3)
def main(): touch_pin = 25 gpio.mode(touch_pin, "in") while True: touchState = gpio.read(touch_pin) if (touchState == gpio.HIGH): print("Touch detected") else: print("No touch detected") time.sleep(0.5)
def main(): switchPin = 3 # has internal pull-up # <2> gpio.mode(switchPin, "in") while (True): switchState = gpio.read(switchPin) # <3> if(switchState == gpio.LOW): print "switch triggered" time.sleep(0.3)
def main(): touch_pin = 25 gpio.mode(touch_pin, "in") while True: touchState = gpio.read(touch_pin) if( touchState == gpio.HIGH ): print("Touch detected") else: print("No touch detected") time.sleep(0.5)
def main(): tiltpin = 3 # has internal pull-up # <1> gpio.mode(tiltpin, "in") while (True): isNotTilted = gpio.read(tiltpin) # <2> if(isNotTilted == gpio.LOW): print "Sensor is tilted" else: print "Sensor is not tilted" time.sleep(0.3) # seconds
def main(): switchpin = 3 # has internal pull-up # <2> gpio.mode(switchpin, "in") while (True): switchState = gpio.read(switchpin) # <3> if (switchState == gpio.LOW): print "Switch is pressed" else: print "Switch is up" time.sleep(0.3) # seconds
def main(): buttonpin = 3 # has internal pull-up # <2> gpio.mode(buttonpin, "in") # <3> while (True): # <4> buttonUp = gpio.read(buttonpin) # <5> if(buttonUp == gpio.HIGH): print "Button is up" else: print "Button is pressed" time.sleep(0.3) # seconds # <6>
def main(): switchpin = 3 # has internal pull-up # <2> gpio.mode(switchpin, "in") while (True): switchState = gpio.read(switchpin) # <3> if(switchState == gpio.LOW): print "Switch is pressed" else: print "Switch is up" time.sleep(0.3) # seconds
def main(): tiltpin = 3 # has internal pull-up # <1> gpio.mode(tiltpin, "in") while (True): isNotTilted = gpio.read(tiltpin) # <2> if (isNotTilted == gpio.LOW): print "Sensor is tilted" else: print "Sensor is not tilted" time.sleep(0.3) # seconds
def main(): buttonpin = 3 # has internal pull-up # <2> gpio.mode(buttonpin, "in") # <3> while (True): # <4> buttonUp = gpio.read(buttonpin) # <5> if (buttonUp == gpio.HIGH): print "Button is up" else: print "Button is pressed" time.sleep(0.3) # seconds # <6>
def main(): pirPin = 14 gpio.mode(pirPin, "in") #Learning period time.sleep(learningPeriod) # <1> while (True): movement = gpio.read(pirPin) # <2> if(movement == gpio.HIGH): print "Movement detected" else: print "No movement detected" time.sleep(0.3)
def main(): pirPin = 14 gpio.mode(pirPin, "in") #Learning period time.sleep(learningPeriod) # <1> while (True): movement = gpio.read(pirPin) # <2> if (movement == gpio.HIGH): print "Movement detected" else: print "No movement detected" time.sleep(0.3)
def main(): pirPin = 7 gpio.mode(pirPin, "in") #Learning period print("learning... " + str(learningPeriod) + " seconds") time.sleep(learningPeriod) # <1> while (True): movement = gpio.read(pirPin) # <2> if (movement == gpio.HIGH): print("Movement detected " + time.ctime()) else: print("No movement detected " + time.ctime()) time.sleep(2)
def sample(count): sendPin = 23 recievePin = 24 gpio.mode(sendPin,"out") gpio.mode(recievePin,"in") gpio.write(sendPin,0) total = 0 # set low for x in xrange(1,count): time.sleep(0.01) gpio.write(sendPin,gpio.HIGH) while(gpio.read(recievePin) == False): total += 1 gpio.write(sendPin,gpio.LOW) return total
def main(): flamePin = 8 gpio.mode(flamePin, "in") #small wait before starting print("starting flame sensor...") time.sleep(0.5) while (True): flame = gpio.read(flamePin) if (flame == gpio.LOW): print("Flame detected " + time.ctime()) else: print("NO flame detected " + time.ctime()) time.sleep(1)
def getKeyPress(): global lastReadTime foundKey = None if((time.time() - lastReadTime) > bounceTime): # <4> #pulse columns and read pins for c in range(len(columns)): gpio.mode(columns[c], 'out') gpio.write(columns[c], gpio.LOW) # <5> for r in range(len(rows)): if gpio.read(rows[r]) == gpio.LOW: # <6> foundKey = keymap[r][c] # <7> gpio.write(columns[c], gpio.HIGH) gpio.mode(columns[c], 'in') # <8> if not foundKey == None: break # <9> lastReadTime = time.time() return foundKey
def main(): broker_address = "139.59.140.158" client = mqtt.Client("P2") client.connect(broker_address) topic = "nuotiovahti/flame" flamePin = 8 gpio.mode(flamePin, "in") while (True): flame = gpio.read(flamePin) if (flame == gpio.LOW): client.publish(topic, "2") else: client.publish(topic, "NO flame detected") time.sleep(2)
def readDistanceCm(): sigPin=22 v=(331.5+0.6*20) gpio.interruptMode(sigPin, "both") # <3> gpio.mode(sigPin, "out") # <4> gpio.write(sigPin, gpio.LOW) # <5> time.sleep(0.5) # s gpio.write(sigPin, gpio.HIGH) # <6> time.sleep(1/1000.0/1000.0) # <7> gpio.mode(sigPin, "in") # <8> #Read high pulse width t = gpio.pulseInHigh(sigPin) # s # <9> d = t*v d = d/2 # <10> return d*100 # cm
def getKeyPress(): global lastReadTime foundKey = None if ((time.time() - lastReadTime) > bounceTime): # <4> #pulse columns and read pins for c in range(len(columns)): gpio.mode(columns[c], 'out') gpio.write(columns[c], gpio.LOW) # <5> for r in range(len(rows)): if gpio.read(rows[r]) == gpio.LOW: # <6> foundKey = keymap[r][c] # <7> gpio.write(columns[c], gpio.HIGH) gpio.mode(columns[c], 'in') # <8> if not foundKey == None: break # <9> lastReadTime = time.time() return foundKey
def readDistanceCm(): sigPin = 22 v = (331.5 + 0.6 * 20) gpio.interruptMode(sigPin, "both") # <3> gpio.mode(sigPin, "out") # <4> gpio.write(sigPin, gpio.LOW) # <5> time.sleep(0.5) # s gpio.write(sigPin, gpio.HIGH) # <6> time.sleep(1 / 1000.0 / 1000.0) # <7> gpio.mode(sigPin, "in") # <8> #Read high pulse width t = gpio.pulseInHigh(sigPin) # s # <9> d = t * v d = d / 2 # <10> return d * 100 # cm
def main(): encoderClk = 3 encoderDt = 2 gpio.mode(encoderClk, "in") gpio.mode(encoderDt, "in") encoderLast = gpio.LOW encoderPos = 0 lastEncoderPos = 0 while True: clk = gpio.read(encoderClk) if encoderLast == gpio.LOW and clk == gpio.HIGH: # <2> if(gpio.read(encoderDt) == gpio.LOW): # <3> encoderPos += 1 else: encoderPos -= 1 encoderLast = clk if encoderPos != lastEncoderPos: print("Encoder position %d" % encoderPos) lastEncoderPos = encoderPos time.sleep(0.001) # s # <4>
def main(): broker_address = "139.59.140.158" client = mqtt.Client("P1") #create new instance client.connect(broker_address) #connect to broker pirPin = 7 gpio.mode(pirPin, "in") #Learning period # client.publish("nuotiovahti","learning... " + str(learningPeriod) + " seconds") time.sleep(learningPeriod) # <1> while (True): movement = gpio.read(pirPin) # <2> if (movement == gpio.HIGH): client.publish("nuotiovahti/pir", "1") else: client.publish("nuotiovahti/pir", "no movement detected") time.sleep(2)
def main(): encoderClk = 3 encoderDt = 2 gpio.mode(encoderClk, "in") gpio.mode(encoderDt, "in") encoderLast = gpio.LOW encoderPos = 0 lastEncoderPos = 0 while True: clk = gpio.read(encoderClk) if encoderLast == gpio.LOW and clk == gpio.HIGH: # <2> if (gpio.read(encoderDt) == gpio.LOW): # <3> encoderPos += 1 else: encoderPos -= 1 encoderLast = clk if encoderPos != lastEncoderPos: print("Encoder position %d" % encoderPos) lastEncoderPos = encoderPos time.sleep(0.001) # s # <4>
def main(): broker_address="139.59.140.158" client = mqtt.Client("P2") client.connect(broker_address) topic = "nuotiovahti" flamePin = 8 gpio.mode(flamePin, "in") #Learning period #print ("starting flame sensor...") while(True): flame = gpio.read(flamePin) if(flame == gpio.LOW): client.publish(topic,"Flame detected" + time.ctime()) #print ("Flame detected " + time.ctime()) else: client.publish(topic,"NO flame detected" + time.ctime()) #print ("NO flame detected " + time.ctime()) time.sleep(3)
def readDistanceCm(): triggerPin = 22 # <1> echoPin = 27 v = (331.5 + 0.6 * 19) # m/s gpio.mode(triggerPin, "out") gpio.mode(echoPin, "in") gpio.interruptMode(echoPin, "both") gpio.write(triggerPin, gpio.LOW) time.sleep(0.5) gpio.write(triggerPin, gpio.HIGH) time.sleep(1 / 1000.0 / 1000.0) gpio.write(triggerPin, gpio.LOW) t = gpio.pulseInHigh(echoPin) # s d = t * v d = d / 2 return d * 100 # cm
def readDistanceCm(): triggerPin = 22 # <1> echoPin = 27 v=(331.5+0.6*20) # m/s gpio.mode(triggerPin, "out") gpio.mode(echoPin, "in") gpio.interruptMode(echoPin, "both") gpio.write(triggerPin, gpio.LOW) time.sleep(0.5) gpio.write(triggerPin, gpio.HIGH) time.sleep(1/1000.0/1000.0) gpio.write(triggerPin, gpio.LOW) t = gpio.pulseInHigh(echoPin) # s d = t*v d = d/2 return d*100 # cm
def readDistanceCm(): # <3> triggerPin = 22 # <4> echoPin = 27 # <5> v = (331.5 + 0.6 * 20) # m/s # <6> gpio.mode(triggerPin, "out") # <7> gpio.mode(echoPin, "in") # <8> gpio.interruptMode(echoPin, "both") # <9> gpio.write(triggerPin, 0) # <10> time.sleep(0.5) # <11> gpio.write(triggerPin, 1) # <12> time.sleep(1 / 1000.0 / 1000.0) # <13> gpio.write(triggerPin, 0) # <14> t = gpio.pulseInHigh(echoPin) # s # <15> d = t * v # <16> d = d / 2 # <17> return d * 100 # cm # <18>
def initializeColorSensor(): ledPin = 25 gpio.mode(2, "out") # <2> gpio.mode(3, "out") gpio.mode(14, "out") gpio.mode(17, "out") gpio.mode(22, "out") gpio.mode(27, "out") gpio.write(2, gpio.LOW) gpio.write(3, gpio.LOW) gpio.write(14, gpio.LOW) gpio.write(17, gpio.LOW) gpio.write(22, gpio.LOW) gpio.write(27, gpio.LOW) gpio.mode(ledPin, "out") gpio.write(ledPin, gpio.HIGH) # <3>
def readButton(): buttonPin = 25 gpio.mode(buttonPin, "in") return gpio.read(buttonPin)
def initializeKeyPad(): for x in range(len(rows)): gpio.mode(rows[x], 'in') gpio.mode(columns[x], 'in')
# Example 4-2. Adjustable infrared switch code # adjustable-infrared-sensor-switch.py - is object within predefined distance? # (c) BotBook.com - Karvinen, Karvinen, Valtokari import botbook_gpio as gpio gpio.mode(27, "in") x = gpio.read(27) if (x == 0): print "Something is inside detection range" else: print "There is nothing inside detection range"
size = width, height # <3> background = 0, 0, 0 # <4> screen = pygame.display.set_mode(size, pygame.FULLSCREEN) # <5> normalSpeed = 512 ballrect = Rect(width / 2, height / 2, 16, 16) # <6> computerrect = Rect(width - 20, 0, 20, 120) # <7> playerrect = Rect(0, 0, 20, 120) # <8> #movement is diff in x and y. ball can only move in 45 degree angles. speed = [normalSpeed, normalSpeed] # <9> clock = pygame.time.Clock() # <10> pygame.mouse.set_visible(False) mainloop = True uppin = 2 downpin = 3 gpio.mode(uppin, "in") gpio.mode(downpin, "in") while mainloop: # <11> seconds = clock.tick(30) / 1000.0 # seconds since last frame # <12> # User input for event in pygame.event.get(): # <13> if event.type == pygame.QUIT: mainloop = False # <14> if (event.type == KEYUP) or (event.type == KEYDOWN): if event.key == K_ESCAPE: mainloop = False # Movement and collisions playerspeed = 0 if gpio.read(uppin) == gpio.LOW:
# adjustable-infrared-sensor-switch.py - is object within predefined distance? # (c) BotBook.com - Karvinen, Karvinen, Valtokari import botbook_gpio as gpio # <1> gpio.mode(27, "in") # <2> x = gpio.read(27) # <3> if (x == 0): # <4> print "Something is inside detection range" # <5> else: # <6> print "There is nothing inside detection range" # <7>
def initializeColorSensor(): ledPin = 25 gpio.mode(2,"out") # <2> gpio.mode(3,"out") gpio.mode(14,"out") gpio.mode(17,"out") gpio.mode(22,"out") gpio.mode(27,"out") gpio.write(2,gpio.LOW) gpio.write(3,gpio.LOW) gpio.write(14,gpio.LOW) gpio.write(17,gpio.LOW) gpio.write(22,gpio.LOW) gpio.write(27,gpio.LOW) gpio.mode(ledPin,"out") gpio.write(ledPin, gpio.HIGH) # <3>
def readAxel(pin): gpio.mode(pin, "in") gpio.interruptMode(pin, "both") return gpio.pulseInHigh(pin) # <1>
size = width, height # <3> background = 0, 0, 0 # <4> screen = pygame.display.set_mode(size,pygame.FULLSCREEN) # <5> normalSpeed = 512 ballrect = Rect(width/2, height/2, 16, 16) # <6> computerrect = Rect(width-20, 0, 20, 120) # <7> playerrect = Rect(0, 0, 20, 120) # <8> #movement is diff in x and y. ball can only move in 45 degree angles. speed = [normalSpeed, normalSpeed] # <9> clock = pygame.time.Clock() # <10> pygame.mouse.set_visible(False) mainloop = True uppin = 2 downpin = 3 gpio.mode(uppin, "in") gpio.mode(downpin, "in") while mainloop: # <11> seconds = clock.tick(30) / 1000.0 # seconds since last frame # <12> # User input for event in pygame.event.get(): # <13> if event.type == pygame.QUIT: mainloop = False # <14> if (event.type == KEYUP) or (event.type == KEYDOWN): if event.key == K_ESCAPE: mainloop = False # Movement and collisions playerspeed = 0 if gpio.read(uppin) == gpio.LOW: