I'm trying to use proximity sensors on a Raspberry Pi2 and I want each sensor to run on a different thread, so I'm using threading module. If I only use 2 threads, everything works fine but when I try to run 3 threads, I get this error:
Exception in thread Thread-3: Traceback (most recent call last): File "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner self.run() File "/usr/lib/python2.7/threading.py", line 505, in run self.__target(*self.__args, **self.__kwargs) File "range_sensor.py", line 52, in measure pulse_duration = pulse_end - pulse_start UnboundLocalError: local variable 'pulse_start' referenced before assignment
Here is the code, I don't understand what is wrong
tuples = [(1, 'Bagno Cla', 'Toilet paper', 23, 24),
(1, 'Bagno Ladispe', 'Trash', 25, 8),
(2,'Bagno inventato', 'Soap', 16,20)]
def measure(bathroomFloor, bathroomId, item, TRIG, ECHO):
# getting raspberry and sensors ready
GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)
GPIO.output(TRIG, False)
print "Waiting For Sensor To Settle"
time.sleep(2)
T=60
while True:
print "Distance Measurement In Progress"
time.sleep(5) #sampling period
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)
while GPIO.input(ECHO)==0:
pulse_start = time.time()
while GPIO.input(ECHO)==1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17150
distance = round(distance, 2)
print "Measured distance in "+bathroomId+":",distance,"cm"
print "Time of measure in "+bathroomId+":",time.strftime("%H:%M")
GPIO.cleanup()
return
# this is the part of code that launches each thread
try: #launching threads
i = 0
while i < len(tuples):
t = threading.Thread(target=measure, args=tuples[i])
t.start();
i+=1
except:
print "Error: unable to start thread"