/ Published in: Python
raspberry pi trafic light with basic menu
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#! /usr/bin/python import time import RPi.GPIO as GPIO # North set of traffic light # Define LED colour and their GPIO pin RED_NORTH = 17 YEL_NORTH = 21 GRN_NORTH = 22 # East set of traffic light RED_EAST = 25 YEL_EAST = 8 GRN_EAST = 7 GPIO.setmode(GPIO.BCM) ans=True while ans: print(""" 1.Run Lights 2.clear GPIO 3.say hi 4.Exit/Quit """) ans=raw_input("What would you like to do? ") if ans=="1": print("\nLights Running") GPIO.setwarnings(False) GPIO.setup(RED_NORTH, GPIO.OUT) GPIO.setup(YEL_NORTH, GPIO.OUT) GPIO.setup(GRN_NORTH, GPIO.OUT) GPIO.setup(RED_EAST, GPIO.OUT) GPIO.setup(YEL_EAST, GPIO.OUT) GPIO.setup(GRN_EAST, GPIO.OUT) delay = 1 GPIO.output(RED_NORTH, True) #ALL red GPIO.output(RED_EAST, True) time.sleep (delay) time.sleep (delay) GPIO.output(YEL_EAST, True) time.sleep (delay) GPIO.output(YEL_EAST, False) GPIO.output(RED_EAST, False) GPIO.output(GRN_EAST, True) # East green time.sleep (delay) time.sleep (delay) GPIO.output(GRN_EAST, False) GPIO.output(YEL_EAST, True) time.sleep (delay) GPIO.output(YEL_EAST, False) GPIO.output(RED_EAST, True) # All red time.sleep (delay) GPIO.output(YEL_NORTH, True) time.sleep (delay) GPIO.output(YEL_NORTH, False) GPIO.output(RED_NORTH, False) GPIO.output(GRN_NORTH, True) # North green time.sleep (delay) time.sleep (delay) GPIO.output(GRN_NORTH, False) GPIO.output(YEL_NORTH, True) time.sleep (delay) GPIO.output(YEL_NORTH, False) GPIO.cleanup() elif ans=="2": print("\n GPIO Cleard") GPIO.cleanup() elif ans=="3": print("\n hi") elif ans=="4": print("\n Goodbye") ans = None else: print("\n Not Valid Choice Try again")