raspberry pi trafic light


/ Published in: Python
Save to your folder(s)

raspberry pi trafic light with basic menu


Copy this code and paste it in your HTML
  1. #! /usr/bin/python
  2.  
  3. import time
  4. import RPi.GPIO as GPIO
  5.  
  6. # North set of traffic light
  7. # Define LED colour and their GPIO pin
  8. RED_NORTH = 17
  9. YEL_NORTH = 21
  10. GRN_NORTH = 22
  11.  
  12. # East set of traffic light
  13. RED_EAST = 25
  14. YEL_EAST = 8
  15. GRN_EAST = 7
  16.  
  17. GPIO.setmode(GPIO.BCM)
  18.  
  19. ans=True
  20.  
  21. while ans:
  22. print("""
  23. 1.Run Lights
  24. 2.clear GPIO
  25. 3.say hi
  26. 4.Exit/Quit
  27. """)
  28. ans=raw_input("What would you like to do? ")
  29. if ans=="1":
  30. print("\nLights Running")
  31. GPIO.setwarnings(False)
  32. GPIO.setup(RED_NORTH, GPIO.OUT)
  33. GPIO.setup(YEL_NORTH, GPIO.OUT)
  34. GPIO.setup(GRN_NORTH, GPIO.OUT)
  35. GPIO.setup(RED_EAST, GPIO.OUT)
  36. GPIO.setup(YEL_EAST, GPIO.OUT)
  37. GPIO.setup(GRN_EAST, GPIO.OUT)
  38. delay = 1
  39. GPIO.output(RED_NORTH, True) #ALL red
  40. GPIO.output(RED_EAST, True)
  41. time.sleep (delay)
  42. time.sleep (delay)
  43.  
  44. GPIO.output(YEL_EAST, True)
  45. time.sleep (delay)
  46. GPIO.output(YEL_EAST, False)
  47. GPIO.output(RED_EAST, False)
  48.  
  49. GPIO.output(GRN_EAST, True) # East green
  50. time.sleep (delay)
  51. time.sleep (delay)
  52. GPIO.output(GRN_EAST, False)
  53.  
  54. GPIO.output(YEL_EAST, True)
  55. time.sleep (delay)
  56. GPIO.output(YEL_EAST, False)
  57.  
  58. GPIO.output(RED_EAST, True) # All red
  59. time.sleep (delay)
  60.  
  61. GPIO.output(YEL_NORTH, True)
  62. time.sleep (delay)
  63. GPIO.output(YEL_NORTH, False)
  64. GPIO.output(RED_NORTH, False)
  65.  
  66. GPIO.output(GRN_NORTH, True) # North green
  67. time.sleep (delay)
  68. time.sleep (delay)
  69. GPIO.output(GRN_NORTH, False)
  70.  
  71. GPIO.output(YEL_NORTH, True)
  72. time.sleep (delay)
  73. GPIO.output(YEL_NORTH, False)
  74. GPIO.cleanup()
  75.  
  76. elif ans=="2":
  77. print("\n GPIO Cleard")
  78. GPIO.cleanup()
  79. elif ans=="3":
  80. print("\n hi")
  81. elif ans=="4":
  82. print("\n Goodbye")
  83. ans = None
  84. else:
  85. print("\n Not Valid Choice Try again")

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.