May-01-2018, 09:42 AM
Hi guys!
I bought a Raspberry Pi some time ago, and now I started a little project. First, I programmed a traffic light, which switches from green to yellow and then red, then it goes back to green and so on. It keeps going on forever. Now I want to add a light for pedestrian, which flashes when the traffic light is red and only if I pressed a button before. I read something about 2 threads which run at the same time, so one thread is always checking if the button was pressed or not. The code looks like this:
Can someone help me? Thanks a lot!
EDIT: Sry, I actually get an error message:
I bought a Raspberry Pi some time ago, and now I started a little project. First, I programmed a traffic light, which switches from green to yellow and then red, then it goes back to green and so on. It keeps going on forever. Now I want to add a light for pedestrian, which flashes when the traffic light is red and only if I pressed a button before. I read something about 2 threads which run at the same time, so one thread is always checking if the button was pressed or not. The code looks like this:
import RPi.GPIO as GPIO import time import _thread GPIO.setmode(GPIO.BCM) rot = 0; gelb = 1; gruen = 2; blau = 3; taster = 4 Ampel=[4,18,23,24,25] GPIO.setup(Ampel[rot], GPIO.OUT, initial = False) GPIO.setup(Ampel[gelb], GPIO.OUT, initial = False) GPIO.setup(Ampel[gruen], GPIO.OUT, initial = True) GPIO.setup(Ampel[blau], GPIO.OUT, initial = False) GPIO.setup(25, GPIO.IN) print("Taster drücken für Fussgängerblinklicht, Ctrl+C beendet das Programm") fussg = False def taste(): GPIO.setmode(GPIO.BCM) GPIO.setup(25, GPIO.IN) while True: if GPIO.input(25) == 1: fussg = True try: while True: if fussg == True: GPIO.output(Ampel[gruen], False) GPIO.output(Ampel[gelb], True) time.sleep(1) GPIO.output(Ampel[gelb], False) GPIO.output(Ampel[rot], True) time.sleep(0.6) for i in range(10): GPIO.output(Ampel[blau], True); time.sleep(0.05) GPIO.output(Ampel[blau], False); time.sleep(0.05) time.sleep(0.6) GPIO.output(Ampel[rot], False) GPIO.output(Ampel[gelb], False) GPIO.output(Ampel[gruen], True) time.sleep(3) fussg = False else: time.sleep(3) GPIO.output(Ampel[gruen], False) GPIO.output(Ampel[gelb], True) time.sleep(1) GPIO.output(Ampel[gelb], False) GPIO.output(Ampel[rot], True) time.sleep(2) GPIO.output(Ampel[gelb], True) time.sleep(0.6) GPIO.output(Ampel[rot], False) GPIO.output(Ampel[gelb], False) GPIO.output(Ampel[gruen], True) except KeyboardInterrupt: GPIO.cleanup() _thread.start_new_thread(taste, ())I don't get an error message, but the program do not work. It has no effect if I press the button or not, the blue (pedestrian) light do not flash.
Can someone help me? Thanks a lot!
EDIT: Sry, I actually get an error message:
Exception in thread Thread-1: Traceback (most recent call last): File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner self.run() File "/usr/lib/python3.5/threading.py", line 862, in run self._target(*self._args, **self._kwargs) File "/home/pi/Desktop/ampel02.py", line 34, in schleife if fussg == True: UnboundLocalError: local variable 'fussg' referenced before assignment