Flash_Stang
Posts: 6
Joined: Sun Nov 17, 2019 8:12 pm

Is my circuit or program wrong?

Tue Nov 19, 2019 10:10 pm

So I have this traffic light circuit on a breadboard connected to the raspberry pi. It consists of a 3 LED's (Red, Yellow and Green) for the light, another two's LEDs (Red and Green) and these are for pedistrians, and then two seperate buttons(one button is to allow the user to change the traffic light to red and the pedistrian light to green. The other is to cause the lights to go out and only have the red traffic light and pedistrian green light constantly blink). When the program is run, only the green traffic light and the red pesitrian light are supposed to stay on. Then when the pedistrian button is held, the green traffic light stays on for 5 more seconds, then goes to yellow, then to red and once the traffic light is red, the pedistrian should stay on at green. Then when the button is released, the pedistrian light goes back to red and the traffic light back to green, like how they initally were.

However when I run the program, it seems to cycle through all these possiblities: the 2 two lights initally on, the button being held and changing the lights, and the blinking button being held to cause the lights to go out and blink red for traffic and green for pedistrians. Except none of the buttons are being pressed. Any idea why this is? My code and circuit are down below(Note: All the wires colored wires in this circuit are connected to GPIO's and refered to by their pin numbers. Only the brown is being used as ground.)

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)

GPIO.setup(7, GPIO.OUT) #Green LED
GPIO.setup(11, GPIO.OUT)#Yellow LED
GPIO.setup(13, GPIO.OUT)#Red LED
GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_UP)# Pedistrian Button
GPIO.setup(16, GPIO.OUT)#Pedestrian Red LED
GPIO.setup(18, GPIO.OUT)#Pedestrian Green LED
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP)# Blinking Button causes Red LED and Pedristrian
                                                 # Green Constantly, while all other lights are off

def turn_on(pin,seconds):
    GPIO.output(pin,GPIO.HIGH)
    time.sleep(seconds)

def turn_off(pin,seconds):
    GPIO.output(pin,GPIO.LOW)
    time.sleep(seconds)

try:
    while True:
        button_state = GPIO.input(15)
        blinking_state = GPIO.input(22)
        turn_on(7,1)
        turn_on(16,1)
        if button_state == True:
            turn_on(7,5)
            turn_off(7,.1)
            turn_on(11,1)
            turn_off(11,.1)
            turn_on(13,1)
            turn_on(18,1)
        if blinking_state == True:
            turn_on(13,1)
            turn_on(18,1)
            turn_off(13,1)
            turn_off(18,1)
        
except KeyboardInterrupt:
    GPIO.output(7,GPIO.LOW)
    GPIO.output(11,GPIO.LOW)
    GPIO.output(13,GPIO.LOW)
    GPIO.output(16,GPIO.LOW)
    GPIO.output(18,GPIO.LOW)
    GPIO.cleanup()
    print("Traffic Light Sequence Done")

Attachments
small_circuit.jpg
small_circuit.jpg (126.56 KiB) Viewed 271 times

User avatar
PeterO
Posts: 5456
Joined: Sun Jul 22, 2012 4:14 pm

Re: Is my circuit or program wrong?

Tue Nov 19, 2019 10:46 pm

It looks like you have the sense of your buttons the wrong way round.

You've configured them with pull ups, so without the button pressed they will be True, and will change to False when you press the button.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Emma_Jir
Posts: 23
Joined: Wed Nov 29, 2017 11:13 am

Re: Is my circuit or program wrong?

Thu Nov 21, 2019 12:19 pm

wouldn't wait_for_edge be a more suitable solution for this? Something like this:

Code: Select all


{setup idle-state without time-arguments}

interrupt = False

while not interrupt
	pedestrian = GPIO.wait_for_edge(15, GPIO.FALLING)				#falling, like Peter0 said
	not_serious = GPIO.wait_for_edge(15, GPIO.RISING, timeout=5000)			#wait 5 seconds for releasing the button
		if pedestrian == not_serious
			{do nothing}
		else
			{change trafficlights}
			GPIO.wait_for_edge(15, GPIO.RISING)
			{change traffic lights back}
	interrupt=GPIO.event_detected(22)

{blinking_state}	

Edit: not sure about the blinking state though.
Last edited by Emma_Jir on Thu Nov 21, 2019 12:23 pm, edited 2 times in total.

Return to “Advanced users”