Guest User

Untitled

a guest
May 24th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. # import modules
  2. import RPi.GPIO as GPIO
  3. import time
  4.  
  5. # constantes
  6. BTN_PRESSED = False # pull-up: false | pull-down: true
  7. OBSTACLE_DETECTED = False # pull-up: false | pull-down: true
  8.  
  9. # setup pins
  10. GPIO.setmode(GPIO.BOARD)
  11. GPIO.setup(3, GPIO.OUT)
  12. #GPIO.setup(5, GPIO.IN)
  13. GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  14.  
  15. """ prender un led """
  16. def ledOn(pin):
  17. GPIO.output(pin, GPIO.HIGH)
  18.  
  19. """ apagar un led """
  20. def ledOff(pin):
  21. GPIO.output(pin, GPIO.LOW)
  22.  
  23. """ indica si un botón fue apretado y mantiene bloqueo mientras no se suelte """
  24. def isButtonPressed(pin):
  25. if GPIO.input(pin) == BTN_PRESSED:
  26. while GPIO.input(pin) == BTN_PRESSED:
  27. time.sleep(0.2)
  28. return True
  29. return False
  30.  
  31. """ indica si el sensor de obstáculos detectó algo """
  32. def obstacleDetected(pin):
  33. return GPIO.input(pin) == OBSTACLE_DETECTED
  34.  
  35. # loop 5 times
  36. for i in range(5):
  37.  
  38. #led 3
  39. ledOn(3)
  40. time.sleep(1)
  41. ledOff(3)
  42. time.sleep(1)
  43.  
  44. #botón
  45. if isButtonPressed(5):
  46. print("Botón ON")
  47. else:
  48. print("Botón OFF")
  49.  
  50. #botón
  51. if obstacleDetected(7):
  52. print("Perro detectado")
  53. else:
  54. print("Perro NO detectado")
Add Comment
Please, Sign In to add comment