def forward(tf): init() gpio.output (7, True) gpio.output (11, True) time.sleep(tf) ' gpio.cleanup()
def reverse(tf): init() gpio.output (7, False) gpio.output (11, False) time.sleep(tf) gpio.cleanup()
def turn_right(tf): init() gpio.output (7, False) gpio.output (11, True) time.sleep(tf) gpio.cleanup()
def changeStateMotion(self, pn, state): GPIO.output(pn, state) row = db.executeQuery("update appliances set status = " + state + " where pin = " + pn) if row.rowcount is not None: # update success return 1 else: # update failed return 0
def left(): GPIO.output(lf,GPIO.LOW) GPIO.output(lb,GPIO.HIGH) GPIO.output(en1,GPIO.HIGH) GPIO.output(rf,GPIO.HIGH) GPIO.output(rb,GPIO.LOW) GPIO.outpu(en2,GPIO.HIGH)
def __init__(self, pinDir, pinPWM): self.dirPin = pinDir self.pwmPin = pinPWM GPIO.setup(self.dirPin, GPIO.OUT) GPIO.output(self.dirPin, GPIO.LOW) GPIO.setup(self.pwmPin, GPIO.OUT) self.pwmControl = GPIO.PWM(self.pwmPin, 10000) self.pwmControl.start(0)
def write(self, value): if value is True: GPIO.output(self.ch_port, GPIO.HIGH) elif value is False: GPIO.output(self.ch_port, GPIO.LOW) else: return super(GPIOOutput, self).write(value)
def __intit__(self): gpio.setmode(gpio.Board) TRIG = 1 ECHO = 2 HIGH_VOL = 3 LOW_VOL = 4 gpio.setup(HIGH_VOL,gpio.OUT) gpio.setup(LOW_VOL,gpio.OUT) gpio.output(HIGH_VOL, gpio.HIGH) gpio.output(LOW_VOL, gpio.LOW) gpio.setup(TRIG, gpio.OUT) gpio.setup(ECHO, gpio.IN) self.dist = 0
def pivot_right(tf): init() gpio.output (7, False) gpio.output (11, True) gpio.output (13, False) gpio.output (15, True) time.sleep(tf) gpio.cleanup()
def pivot_left(tf): init() gpio.output (7, True) gpio.output (11, False) gpio.output (13, True) gpio.output (15, False) time.sleep(tf) gpio.cleanup()
def turn_left(tf): init() 0 gpio.output (7, True) gpio.output (11, True) gpio.output (13, True) gpio.output (15, False) time.sleep(tf) gpio.cleanup()
def forward(tf): init() gpio.output (7, True) gpio.output (11, False) gpio.output (13, False) gpio.output (15, True) time.sleep(tf) gpio.cleanup()
def right : print('RIGHT') p1.ChangeDutyCycle(50) p2.ChangeDutyCycle(50) GPIO.output(m11,1) GPIO.output(m12,0) GPIO.output(m21,0) GPIO.output(m22,0)
def left : print('LEFT') p2.ChangeDutyCycle(50) p2.ChangeDutyCycle(50) GPIO.output(m11,0) GPIO.output(m12,0) GPIO.output(m21,1) GPIO.output(m22,0)
def distance(): GPIO.output(trig, True) time.sleep(0.00001) GPIO.output(trig, False) while GPIO.input(echo) == 0: pulse_start = time.time() while GPIO.input(echo) == 1: pulse_end = time.time() pulse_duration = pulse_end - pulse_start d = pulse_duration * 17150 d = round(distance, 2) return d
def shoot(self,wait=0.5): ##focus... GPIO.output(self.pin1,True) sleep(wait) ## shutter release GPIO.output(self.pin2, True) sleep(wait) ##clean up GPIO.output(self.pin1, False) GPIO.output(self.pin2, False)
def lit(self,prob): i=0 GPIO.setmode(GPIO.BOARD) GPIO.setup(7,GPIO.OUT) GPIO.setwarnings(False) while(i>0): if prob>0.5: GPIO.output(7,GPIO.HIGH) time.sleep(1) GPIO.output(7,GPIO.LOW) GPIO.output(7,GPIO.LOW) time.sleep(1) i-=1 else: GPIO.output(7,GPIO.LOW) time.sleep(1) i-=1 GPIO.output(7,GPIO.LOW) GPIO.cleanup()
def FindDistance2(): GPIO.output(trigS2, False) time.sleep(1) GPIO.output(trigS2, True) time.sleep(0.00001) GPIO.output(trigS2, False) while GPIO.input(echoS1) == 0: pulse_start = time.time() while GPIO.input(echoS2) == 1: pulse_end = time.time() pulse = pulse_end - pulse_start distance2 = pulse * 17150 distance2 = round(distance, 2) if distance2 < 10: distance2 = 1 status2 = "slot 2 is full" return distance2 else: distance2 = 0 status2 = "slot 2 is empty" return distance2 return status2
def FindDistance1(): GPIO.output(trigS1, False) time.sleep(1) GPIO.output(trigS1, True) time.sleep(0.00001) GPIO.output(trigS1, False) while GPIO.input(echoS1) == 0: pulse_start = time.time() while GPIO.input(echoS1) == 1: pulse_end = time.time() pulse = pulse_end - pulse_start distance1 = pulse * 17150 distance1 = round(distance, 2) if distance1 < 10: distance1 = 1 status1 = "slot 1 is full" return distance1 else: distance1 = 0 status1 = "slot 1 is empty" return distance1 return status1
def FindDistance5(): GPIO.output(trigS5, False) time.sleep(1) GPIO.output(trigS5, True) time.sleep(0.00001) GPIO.output(trigS5, False) while GPIO.input(echoS5) == 0: pulse_start = time.time() while GPIO.input(echoS5) == 1: pulse_end = time.time() pulse = pulse_end - pulse_start distance5 = pulse * 17150 distance5 = round(distance, 2) if distance5 < 10: distance5 = 1 status5 = "slot 5 is full" return distance5 else: distance5 = 0 status5 = "slot 5 is empty" return distance5 return status5
def FindDistance4(): GPIO.output(trigS4, False) time.sleep(1) GPIO.output(trigS4, True) time.sleep(0.00001) GPIO.output(trigS4, False) while GPIO.input(echoS4) == 0: pulse_start = time.time() while GPIO.input(echoS4) == 1: pulse_end = time.time() pulse = pulse_end - pulse_start distance4 = pulse * 17150 distance4 = round(distance, 2) if distance4 < 10: distance4 = 1 status4 = "slot 4 is full" return distanc4 else: distance4 = 0 status4 = "slot 4 is empty" return distance4 return status4
def FindDistance6(): GPIO.output(trigS6, False) time.sleep(1) GPIO.output(trigS6, True) time.sleep(0.00001) GPIO.output(trigS6, False) while GPIO.input(echoS6) == 0: pulse_start = time.time() while GPIO.input(echoS6) == 1: pulse_end = time.time() pulse = pulse_end - pulse_start distance6 = pulse * 17150 distance6 = round(distance, 2) if distance6 < 10: distance6 = 1 status6 = "slot 6 is full" return distance6 else: distance6 = 0 status6 = "slot 6 is empty" return distance6 return status6
def FindDistance3(): GPIO.output(trigS3, False) time.sleep(1) GPIO.output(trigS3, True) time.sleep(0.00001) GPIO.output(trigS3, False) while GPIO.input(echoS3) == 0: pulse_start = time.time() while GPIO.input(echoS3) == 1: pulse_end = time.time() pulse = pulse_end - pulse_start distance3 = pulse * 17150 distance3 = round(distance, 2) if distance3 < 10: distance3 = 1 status3 = "slot 3 is full" return distance3 else: distance3 = 0 status3 = "slot 3 is empty" return distance3 return status3
def receive(interface): while True: message = interface.recv(1024) if not(len(message)): break # Se a mensagem recebida for que o botão foi pressionado o led acende elif message == 1: gpio.output(13, gpio.HIGH) # Se a mensagem recebida for que o botão não está pressionado o led permanece apagado elif message == 0: gpio.output(13, gpio.LOW) else: gpio.output(13, gpio.LOW) print("Conexão com o servidor encerrada!")
def run(self): gpio.output(TRIG, False) print "Waiting For Sensor To Settle" time.sleep(2) gpio.output(TRIG, True) time.sleep(0.00001) gpio.output(TRIG, False) while gpio.input(ECHO) == 0: pulse_start = time.time() while gpio.input(ECHO) == 1: pulse_end = time.time() pulse_duration = pulse_end - pulse_start distance = pulse_duration * 17150 self.dist = round(distance, 2) return self.dist
import Rpi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO_TRIGGER=18 GPIO_ECHO24 GPIO.setup(GPIO_TRIGGER,GPIO.OUT) GPIO.setup(GPIO_-ECHO,GPIO.IN) Def distance(): GPIO.output(GPIO_TRIGGER,True) Time.sleep(0.0001) GPIO.output(GPIO_TRIGGER,False) StartTime=time.time() StopTime=time.time() while GPIO.input(GPIO_ECHO) == 0: StartTime = time.time() while GPIO.inut(GPIO_ECHO) ==1: StopTime=time.time() TimeElapsed=StopTime-StartTime distance=(TimeElapsed * 34300)/2 Return distance if__name__ == ‘__main__: try: while True:
import Rpi.GPIO as GPIO, time GPIO.setmode(GPIO.BCM) GREEN_LED = 22 RED_LED = 21 GPIO.setup(GREEN_LED, GPIO.OUT) GPIO.setup(RED_LED, GPIO.OUT) GPIO.output(GREEN_LED, True) time.sleep(5) GPIO.output(GREEN_LED, False)
''' Build a circuit using your Raspberry Pi that causes an LED to blink when a push button is NOT pressed. However, the LED should stay on continually when the push button IS pressed. Video should show the LED blinking when the push button is not pressed, and it should show that the LED is constantly on while the button is pressed. The code can be tested without Raspberry Pi board by using print statements ''' import Rpi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) #setup or initialize the gpio board GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_UP) #setup pin 10 for this project, button up while True: input_state = GPIO.output(10) if input_state == True: #if button up, it blinks print("Button Pressed") GPIO.output(10, True) time.sleep(0.5) #sleep half second GPIO.output(10, False) time.sleep(0.5) else: #if button down, it stays constantly print "Button Not Pressed" GPIO.output(10, True)
while True: # bucle infinito gpio . out(led1,True) # encendemos el led sleep(1) # paus de un segundo gpio . output(led1 , False) # apagamos el led1 sleep(1) # pausa de un segundo ########################################################################################### while true: condicion = gpio.input(entrada) sleep(0.3) if condicion==True: # si se preciona es true . entra en esa linea contador = contador + 1 #aumentamos la variable contador # como solo tenemos un led , contador debe tomar los valores 0 , 1 if contador = 2: contador=0 # pero usamos el 0 para apagar el led if contador ==0: gpio.output(led1,False) # apagamos el led1 if contador==1: gpio.output(led1,True) # encendemos el led # ejercicio: con la primera pulsacion , encender solo el primer led # con la segunda pulsacion , ensencder solo el segundo led # con la tercera pulsacion , enceder solo el tercer led # conla cuarta pulsacion , apagar todo...................
#!/usr/bin/python3 # A simple Raspberry pi 3 GPIO test # Written by # (except lines 5-13, these are from the RPi site raspberrypi.org/documentation/usage/python/more.md) # plscks import Rpi.GPIO as GPIO GPIO.setmode(GPIO.BCM) # set board mode to Broadcom GPIO.setup(17, GPIO.OUT) # set up pin 17 GPIO.setup(18, GPIO.OUT) # set up pin 18 GPIO.output(17, 1) # turn on pin 17 GPIO.output(18, 1) # turn on pin 18
def press_button(): log('Debug: press_button') import Rpi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) GPIO.setup(SERVER_POWER, GPIO.OUT) GPIO.setup(SERVER_RESET, GPIO.OUT) GPIO.setup(DESKTOP_POWER, GPIO.OUT) GPIO.setup(DESKTOP_RESET, GPIO.OUT) log('Debug: set up GPIO') try: if machine_to_control == "S": if button_to_press == "P": # Server Power GPIO.output(SERVER_POWER, True) log('Info: pressing server power button') time.sleep(BUTTON_PRESS_TIME) GPIO.output(SERVER_POWER, False) log('Info: button released') elif button_to_press == "R": # Server Reset GPIO.output(SERVER_RESET, True) log('Info: pressing server reset button') time.sleep(BUTTON_PRESS_TIME) GPIO.output(SERVER_RESET, False) log('Info: button released') elif button_to_press == "PH": # Server Power Hold GPIO.output(SERVER_POWER, True) log('Info: holding server power button') time.sleep(BUTTON_HOLD_TIME) GPIO.output(SERVER_POWER, False) log('Info: button released') else: log('Error: machine variables not set correctly') GPIO.cleanup() return if machine_to_control == "D": if button_to_press == "P": # Desktop Power GPIO.output(DESKTOP_POWER, True) log('Info: pressing desktop power button') time.sleep(BUTTON_PRESS_TIME) GPIO.output(DESKTOP_POWER, False) log('Info: button released') elif button_to_press == "R": # Desktop Reset GPIO.output(DESKTOP_RESET, True) log('Info: pressing desktop reset button') time.sleep(BUTTON_PRESS_TIME) GPIO.output(DESKTOP_RESET, False) log('Info: button released') elif button_to_press == "PH": # Desktop Power Hold GPIO.output(DESKTOP_POWER, True) log('Info: holding desktop power button') time.sleep(BUTTON_HOLD_TIME) GPIO.output(DESKTOP_POWER, False) log('Info: button released') else: log('Error: desktop variables not set correctly') GPIO.cleanup() return # TODO: catch and log the error. finally: GPIO.cleanup()
import Rpi.GPIO as GPIO GPIO.setup(18, GPIO.OUT) GPIO.setup(11, GPIO.IN) while True: if GPIO.input(11): GPIO.output(18, True) else: GPIO.output(18, False)
def blink(pin): GPIO.output(pin,GPIO.HIGH) time.sleep(1) GPIO.output(pin,GPIO.LOW) time.sleep(1)
import Rpi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(18, GPIO.OUT) while TRUE: try: GPIO.output(18, GPIO.HIGH) time.sleep(1) GPIO.output(18, GPIO.LOW) time.sleep(1) except KeyboardInterrupt: break GPIO.cleanup()
import socket import Rpi.GPIO as GPIO import sys import os mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: mysock.bind(("192.168.1.111", 12345)) except socket.error: print("Failed to bind") sys.exit() mysock.listen(5) while True: conn, addr = mysock.accept() print ('Connected by ', addr) data = conn.recv(1024) if not data: break if data =="1": print ('ONE') GPIO.output(7, True) if data == "2": GPIO.output(7, False) conn.sendall(data) os.system(str(data)) conn.sendall(data) conn.close() mysock.close()
from Rpi import GPIO #import GPIO Module GPIO.setmode(GPIO.BOARD) # Board Pin GPIO.setwarnings(False) # set Warnings GPIO.setup(40, GPIO.IN) #declare input pin GPIO.setup(5, GPIO.OUT) #declare output pin while True: #use to While loop a = GPIO.input(40) #input pin if a == 1: #condition GPIO.output(5, HIGH) #Led On else: GPIO.output(5, LOW) #Led Off
import Rpi.GPIO as GPIO import time GPIO.setwarnings(Fale) GPIO.setmode(GPIO.BOARD) GPIO.setup(3, GPIO.OUT) while True: GPIO.output(3, 1) time.sleep(1) GPIO.output(3, 0) time.sleep(1)
def reverse(self, speed): GPIO.output(self.dirPin, GPIO.HIGH) self.pwmControl.ChangeDutyCycle(speed) print("Motor Reverse at " + speed + "% duty cycle")
import Rpi.GPIO as gpio import time gpio.setmode(gpio,BCM) gpio.setup(21,gpio.OUT) while(1): x=input('Enter the state') if x == 'ON': gpio.output(21,1) print('LED ON') if x =='OFF' gpio.output(21,0) print('LED OFF') gpio.cleanup()
def forward(self, speed): GPIO.output(self.dirPin, GPIO.LOW) self.pwmControl.ChangeDutyCycle(speed) print("Motor Forward at " + speed + "% duty cycle")