Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # import modules
- import RPi.GPIO as GPIO
- import time
- # constantes
- BTN_PRESSED = False # pull-up: false | pull-down: true
- OBSTACLE_DETECTED = False # pull-up: false | pull-down: true
- # setup pins
- GPIO.setmode(GPIO.BOARD)
- GPIO.setup(3, GPIO.OUT)
- #GPIO.setup(5, GPIO.IN)
- GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)
- """ prender un led """
- def ledOn(pin):
- GPIO.output(pin, GPIO.HIGH)
- """ apagar un led """
- def ledOff(pin):
- GPIO.output(pin, GPIO.LOW)
- """ indica si un botón fue apretado y mantiene bloqueo mientras no se suelte """
- def isButtonPressed(pin):
- if GPIO.input(pin) == BTN_PRESSED:
- while GPIO.input(pin) == BTN_PRESSED:
- time.sleep(0.2)
- return True
- return False
- """ indica si el sensor de obstáculos detectó algo """
- def obstacleDetected(pin):
- return GPIO.input(pin) == OBSTACLE_DETECTED
- # loop 5 times
- for i in range(5):
- #led 3
- ledOn(3)
- time.sleep(1)
- ledOff(3)
- time.sleep(1)
- #botón
- if isButtonPressed(5):
- print("Botón ON")
- else:
- print("Botón OFF")
- #botón
- if obstacleDetected(7):
- print("Perro detectado")
- else:
- print("Perro NO detectado")
Add Comment
Please, Sign In to add comment