Advertisement
Guest User

Untitled

a guest
Jun 30th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. import RPi.GPIO as GPIO
  2. import time
  3.  
  4. GPIO.setwarnings(False)
  5. red_walk_LED = 16
  6. green_traf_LED = 15
  7. Btn_one = 22 # pin12 --- button
  8. Btn_two = 29 # pin29 --- 2nd button
  9. # GLOBAL VARIABLES
  10. red_Led_status = 1
  11. Green_Led_status = 1
  12. flag_btn_one_pushed = 0
  13. flag_btn_two_pushed = 0
  14.  
  15. def all_leds_off():
  16. GPIO.output(green_traf_LED, GPIO.HIGH)
  17. GPIO.output(yellow_traf_LED, GPIO.HIGH)
  18. GPIO.output(red_traf_LED, GPIO.HIGH)
  19. GPIO.output(red_walk_LED, GPIO.HIGH)
  20. GPIO.output(white_walk_LED, GPIO.HIGH)
  21. def setup():
  22. global green_LED_frequence
  23. global red_LED_frequence
  24. GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
  25. #set LEDs as outputs
  26. GPIO.setup(green_traf_LED, GPIO.OUT)
  27. GPIO.setup(red_traf_LED, GPIO.OUT)
  28. GPIO.setup(Btn_one, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set BtnPin's mode as input, and pull up to high level(3.3V)
  29. GPIO.setup(Btn_two, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  30. red_LED_frequence = GPIO.PWM(red_traf_LED, 1000) # set Frequece to 1KHz
  31. green_LED_frequence = GPIO.PWM(green_traf_LED, 1000)
  32. red_LED_frequence.start(0) # Duty Cycle = 0
  33. green_LED_frequence.start(0)
  34.  
  35. def Btn_one_push(ev=None):
  36. print('OK, the 1st button was pushed')
  37. global red_Led_status #we are allowed to change these variables in this function
  38. global my_counter
  39. global flag_btn_one_pushed
  40. global red_LED_frequence
  41.  
  42. red_Led_status = not red_Led_status #change LED status 0-1 or 1-0
  43. flag_btn_one_pushed = 1
  44.  
  45. my_delay = 0.2
  46. GPIO.output(green_traf_LED, GPIO.HIGH)
  47. if red_Led_status == 1: #1-on
  48. print('ok, reds on')
  49. for dc in range(0, 101, 4): # Increase duty cycle: 0~100
  50. red_LED_frequence.ChangeDutyCycle(dc) # Change duty cycle
  51. time.sleep(0.02)
  52. for dc in range(100, -1, -4): # Decrease duty cycle: 100~0
  53. red_LED_frequence.ChangeDutyCycle(dc)
  54. time.sleep(0.02)
  55.  
  56.  
  57. all_leds_off() #turn all LEDs off!!
  58. flag_btn_pushed = 0
  59.  
  60. def Btn_two_push(ev=None):
  61. print('OK, the 2nd button was pushed')
  62. global Green_Led_status
  63. global flag_btn_two_pushed
  64. global green_LED_frequence
  65.  
  66. Green_Led_status = not Green_Led_status #change LED status 0-1 or 1-0
  67. flag_btn_two_pushed = 1
  68.  
  69. my_delay = 0.2
  70. GPIO.output(red_traf_LED, GPIO.HIGH)
  71. if Green_Led_status == 1: #1-on
  72. print('This is supposed to work!')
  73. for dc in range(0, 101, 4): # Increase duty cycle: 0~100
  74. print(green_LED_frequence)
  75. green_LED_frequence.ChangeDutyCycle(dc) # Change duty cycle
  76. time.sleep(0.08)
  77. for dc in range(100, -1, -4): # Decrease duty cycle: 100~0
  78. green_LED_frequence.ChangeDutyCycle(dc)
  79. time.sleep(0.08)
  80. all_leds_off() #turn all LEDs off!!
  81. flag_btn_two_pushed = 0
  82.  
  83.  
  84.  
  85.  
  86.  
  87. def loop():
  88. global flag_btn_one_pushed
  89. global flag_btn_two_pushed
  90. GPIO.add_event_detect(Btn_one, GPIO.FALLING, callback=Btn_one_push) # wait for change in GPIO 0-1 or 1-0
  91. GPIO.add_event_detect(Btn_two, GPIO.FALLING, callback=Btn_two_push)
  92. while True: #when the button isn't pushed, do this
  93. all_leds_off()
  94. else:
  95. pass
  96.  
  97. def destroy():
  98. all_leds_off()
  99. GPIO.cleanup() # Release resource
  100.  
  101. if __name__ == '__main__': # Program start from here
  102. setup()
  103. try:
  104. loop()
  105. except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
  106. destroy()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement