sebra9988
Posts: 1
Joined: Fri Sep 28, 2018 7:45 pm

Traffic lights

Fri Sep 28, 2018 9:38 pm

I am trying to make a traffic program with red, yellow, green, and red and green for pedestrians. I see that the simple traffic program on the raspberry side contains only three inputs. Can anybody help me?

User avatar
topguy
Posts: 5626
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Traffic lights

Sat Sep 29, 2018 11:22 pm

Help with what exactly ? You are not giving us much to help with, maybe add some links that shows us what you are working with.

gordon77
Posts: 4079
Joined: Sun Aug 05, 2012 3:12 pm

Re: Traffic lights

Sun Sep 30, 2018 8:46 am

Is this the code you used?

https://projects.raspberrypi.org/en/pro ... hts-python

Try adding leds for the pedestrians.

eg. Controls 2 sets of traffic lights. Pedestrians need to press a button and wait for a WALK indication.

Code: Select all

from gpiozero import LED
from gpiozero import Button
from time import sleep

red1     = LED(23) # RED set 1
amber1   = LED(24) # AMBER set 1
green1   = LED(25) # GREEN set 1
red2     = LED(5)  # RED set 2
amber2   = LED(6)  # AMBER set 2
green2   = LED(13) # GREEN set 2
p_red1   = LED(8)  # pedestrian RED (DON'T WALK) light, set 1
p_green1 = LED(27) # pedestrian GREEN (WALK) light, set 1
p_red2   = LED(19) # pedestrian RED (DON'T WALK)light, set 2
p_green2 = LED(26) # pedestrian GREEN (WALK) light, set 1
p_set1   = LED(12) # Pedestrian WAIT LED 1, acknowledges button pressed
p_set2   = LED(22) # Pedestrian WAIT LED 2, acknowledges button pressed
button1 = Button( 7,pull_up = True,bounce_time= None) # Pedestrian button set 1, switch to gnd
button2 = Button(16,pull_up = True,bounce_time= None) # Pedestrian button set 2, switch to gnd

# initialise
red2.on()
amber2.off()
green2.off()
red1.on()
amber1.off()
green1.off()
p_red1.on()
p_red2.on()
p_green1.off()
p_green2.off()
p_set1.off()
p_set2.off()

def Light_Sequence(red,amber,green):
   amber.on()
   sleep(2)
   red.off()
   amber.off()
   green.on()
   sleep(10)
   green.off()
   amber.on()
   sleep(2)
   amber.off()
   red.on()
   sleep(1)
   
def PED_button(x): # pedestrian button pressed
   global PED
   PED = 1
   p_set1.on() # WAIT LED 1
   p_set2.on() # WAIT LED 2

def PED_Crossing():
   p_set1.off()
   p_set2.off()
   p_green1.on()
   p_green2.on()
   p_red1.off()
   p_red2.off()
   sleep(3)
   for led in range (0, 10):
      p_green1.off()
      p_green2.off()
      sleep(.1)
      p_green1.on()
      p_green2.on()
      sleep(.1)
   p_green1.off()
   p_green2.off()
   p_red1.on()
   p_red2.on()
   sleep(1)
   
x = 0
PED = 0
button1.when_pressed = PED_button
button2.when_pressed = PED_button

while True:
   # traffic lights set 1 sequence
   Light_Sequence(red1,amber1,green1)
   # pedestrian crossing,if either button has been pressed
   if PED == 1:
      PED_Crossing()
      PED = 0
   # traffic lights set 2 sequence
   Light_Sequence(red2,amber2,green2)
   # pedestrian crossing,if either button has been pressed
   if PED == 1:
      PED_Crossing()
      PED = 0



Return to “General discussion”