def main(): limitPin = 23 gpio.setMode(limitPin, "in") while True: if gpio.read(limitPin) == gpio.LOW: # <1> print("Touch 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(): 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(): 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(): 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(): 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(): 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(): 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(): 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(): 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(): 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 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(): 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(): 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(): 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(): 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(): 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(): 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 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 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 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("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(): 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 readButton(): buttonPin = 25 gpio.mode(buttonPin, "in") return gpio.read(buttonPin)
# 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 readLimit(): limitPin = 23 gpio.setMode(limitPin, "in") return gpio.read(limitPin)
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: playerspeed = -normalSpeed if gpio.read(downpin) == gpio.LOW: playerspeed = normalSpeed ballrect.x += speed[0] * seconds # <15> ballrect.y += speed[1] * seconds if ballrect.left < 0 or ballrect.right > width: # <16> ballrect.x = width/2; if ballrect.top < 0 or ballrect.bottom > height: speed[1] = -speed[1] computerrect.y = round(ballrect.y) # <17> playerrect.y += playerspeed * seconds # <18> if playerrect.top < 0: playerrect.top = 0 # <19> if playerrect.bottom > height: playerrect.bottom = height # <20> if computerrect.colliderect(ballrect): # <21>
# 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"
def readLimit(): limitPin = 23 gpio.setMode(limitPin,"in") return gpio.read(limitPin)
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: playerspeed = -normalSpeed if gpio.read(downpin) == gpio.LOW: playerspeed = normalSpeed ballrect.x += speed[0] * seconds # <15> ballrect.y += speed[1] * seconds if ballrect.left < 0 or ballrect.right > width: # <16> ballrect.x = width / 2 if ballrect.top < 0 or ballrect.bottom > height: speed[1] = -speed[1] computerrect.y = round(ballrect.y) # <17> playerrect.y += playerspeed * seconds # <18> if playerrect.top < 0: playerrect.top = 0 # <19> if playerrect.bottom > height: playerrect.bottom = height # <20> if computerrect.colliderect(ballrect): # <21>