Advertisement
cymplecy

threading

Oct 11th, 2016
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.00 KB | None | 0 0
  1. import time
  2.  
  3. TIME = 0.1
  4. import anyio.GPIO as GPIO
  5. from threading import Thread
  6. BUTTON = 23
  7.  
  8. RED =13
  9. YEL =11
  10. GRN=7
  11.  
  12. GPIO.setup(RED, GPIO.OUT)
  13. GPIO.setup(YEL, GPIO.OUT)
  14. GPIO.setup(GRN, GPIO.OUT)
  15. GPIO.setmode(GPIO.BOARD)
  16. GPIO.setup(BUTTON, GPIO.IN)
  17. GPIO.output(GRN, True)
  18.  
  19. def pinread():
  20.     while True:
  21.         time.sleep(TIME)
  22.         b = GPIO.input(BUTTON)
  23.         if not b:
  24.             print("pressed")
  25.         else:
  26.             print("released")
  27.  
  28. def pinwrite():
  29.     while True:
  30.         GPIO.output(GRN, False)
  31.         GPIO.output(YEL, True)
  32.         time.sleep(TIME * 10)
  33.         GPIO.output(YEL, False)
  34.         GPIO.output(RED, True)
  35.         time.sleep(TIME * 30)
  36.         GPIO.output(YEL, True)
  37.         time.sleep(TIME * 10)
  38.         GPIO.output(RED, False)
  39.         GPIO.output(YEL, False)
  40.         GPIO.output(GRN, True)
  41.         time.sleep(TIME * 30)
  42.  
  43. try:
  44.     i = Thread(target=pinread)
  45.     i.start()
  46.     o = Thread(target=pinwrite)
  47.     o.start()
  48. finally:
  49.     GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement