Skip to content

Instantly share code, notes, and snippets.

@mmikhan
Created October 31, 2021 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmikhan/9015f9d8625ff25ee86786455a2c554c to your computer and use it in GitHub Desktop.
Save mmikhan/9015f9d8625ff25ee86786455a2c554c to your computer and use it in GitHub Desktop.
Control traffic LEDs through IR Sensors to dynamically turn green light on or off based on the number of vehicles using Python on Raspberry Pi
from time import sleep
from signal import pause
from gpiozero import LED, MotionSensor
ledNorthRed = LED(21)
ledNorthYellow = LED(20)
ledNorthGreen = LED(16)
ledSouthRed = LED(26)
ledSouthYellow = LED(19)
ledSouthGreen = LED(13)
ledEastRed = LED(22)
ledEastYellow = LED(27)
ledEastGreen = LED(17)
ledWestRed = LED(25)
ledWestYellow = LED(24)
ledWestGreen = LED(23)
sensorNorth = MotionSensor(12)
sensorSouth = MotionSensor(6)
sensorEast = MotionSensor(4)
sensorWest = MotionSensor(18)
trafficNorthCount = 0
trafficSouthCount = 0
trafficEastCount = 0
trafficWestCount = 0
def blinkAllLeds():
ledNorthRed.blink()
ledNorthYellow.blink()
ledNorthGreen.blink()
ledSouthRed.blink()
ledSouthYellow.blink()
ledSouthGreen.blink()
ledEastRed.blink()
ledEastYellow.blink()
ledEastGreen.blink()
ledWestRed.blink()
ledWestYellow.blink()
ledWestGreen.blink()
def onAllLeds():
ledNorthRed.on()
ledNorthYellow.on()
ledNorthGreen.on()
ledSouthRed.on()
ledSouthYellow.on()
ledSouthGreen.on()
ledEastRed.on()
ledEastYellow.on()
ledEastGreen.on()
ledWestRed.on()
ledWestYellow.on()
ledWestGreen.on()
def offAllLeds():
ledNorthRed.off()
ledNorthYellow.off()
ledNorthGreen.off()
ledSouthRed.off()
ledSouthYellow.off()
ledSouthGreen.off()
ledEastRed.off()
ledEastYellow.off()
ledEastGreen.off()
ledWestRed.off()
ledWestYellow.off()
ledWestGreen.off()
def turnOnNorthLeds():
ledNorthRed.off()
ledNorthGreen.on()
def turnOffNorthLeds():
ledNorthYellow.blink()
sleep(2)
ledNorthYellow.off()
ledNorthGreen.off()
ledNorthRed.on()
def turnOnSouthLeds():
ledSouthRed.off()
ledSouthGreen.on()
def turnOffSouthLeds():
ledSouthYellow.blink()
sleep(2)
ledSouthYellow.off()
ledSouthGreen.off()
ledSouthRed.on()
def turnOnEastLeds():
ledEastRed.off()
ledEastGreen.on()
def turnOffEastLeds():
ledEastYellow.blink()
sleep(2)
ledEastYellow.off()
ledEastGreen.off()
ledEastRed.on()
def turnOnWestLeds():
ledWestRed.off()
ledWestGreen.on()
def turnOffWestLeds():
ledWestYellow.blink()
sleep(2)
ledWestYellow.off()
ledWestGreen.off()
ledWestRed.on()
def turnOnAllRed():
ledNorthRed.on()
ledSouthRed.on()
ledEastRed.on()
ledWestRed.on()
def turnOffAllRed():
ledNorthRed.off()
ledSouthRed.off()
ledEastRed.off()
ledWestRed.off()
def blinkAllYellow():
ledNorthYellow.blink()
ledSouthYellow.blink()
ledEastYellow.blink()
ledWestYellow.blink()
def turnOffAllYellow():
ledNorthYellow.off()
ledSouthYellow.off()
ledEastYellow.off()
ledWestYellow.off()
def trafficNorthCounter():
global trafficNorthCount
trafficNorthCount += 1
def trafficSouthCounter():
global trafficSouthCount
trafficSouthCount += 1
def trafficEastCounter():
global trafficEastCount
trafficEastCount += 1
def trafficWestCounter():
global trafficWestCount
trafficWestCount += 1
def trafficNorthCounterReset():
global trafficNorthCount
trafficNorthCount = 0
def trafficSouthCounterReset():
global trafficSouthCount
trafficSouthCount = 0
def trafficEastCounterReset():
global trafficEastCount
trafficEastCount = 0
def trafficWestCounterReset():
global trafficWestCount
trafficWestCount = 0
#blinkAllLeds()
#offAllLeds()
#pause()
sensorNorth.when_no_motion = trafficNorthCounter
sensorSouth.when_no_motion = trafficSouthCounter
sensorEast.when_no_motion = trafficEastCounter
sensorWest.when_no_motion = trafficWestCounter
while True:
if trafficNorthCount > trafficSouthCount or trafficNorthCount > trafficEastCount or trafficNorthCount > trafficWestCount:
blinkAllYellow()
turnOnAllRed()
turnOffAllYellow()
turnOnNorthLeds()
sleep(5)
trafficNorthCounterReset()
turnOffNorthLeds()
elif trafficSouthCount > trafficNorthCount or trafficSouthCount > trafficEastCount or trafficSouthCount > trafficWestCount:
blinkAllYellow()
turnOnAllRed()
turnOffAllYellow()
turnOnSouthLeds()
sleep(5)
trafficSouthCounterReset()
turnOffSouthLeds()
elif trafficEastCount > trafficNorthCount or trafficEastCount > trafficSouthCount or trafficEastCount > trafficWestCount:
blinkAllYellow()
turnOnAllRed()
turnOffAllYellow()
turnOnEastLeds()
sleep(5)
trafficEastCounterReset()
turnOffEastLeds()
elif trafficWestCount > trafficNorthCount or trafficWestCount > trafficSouthCount or trafficWestCount > trafficEastCount:
blinkAllYellow()
turnOnAllRed()
turnOffAllYellow()
turnOnWestLeds()
sleep(5)
trafficWestCounterReset()
turnOffWestLeds()
else:
turnOnAllRed()
ledNorthYellow.blink()
sleep(2)
ledNorthYellow.off()
turnOnNorthLeds()
sleep(5)
turnOffNorthLeds()
ledSouthYellow.blink()
sleep(2)
ledSouthYellow.off()
turnOnSouthLeds()
sleep(5)
turnOffSouthLeds()
ledEastYellow.blink()
sleep(2)
ledEastYellow.off()
turnOnEastLeds()
sleep(5)
turnOffEastLeds()
ledWestYellow.blink()
sleep(2)
ledWestYellow.off()
turnOnWestLeds()
sleep(5)
turnOffWestLeds()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment