I'm trying to fix this code for a robot with multiple sensors. The values of TRIG and ECHO are input through another part of the code.
Set this at the beginning of the code
def init():
GPIO.setmode(GPIO.BOARD)
This is the function where I'm getting the error
def getSensor(TRIG,ECHO):
try:
init()
#Set the pins for TRIG, trig is the output pin
GPIO.setup(TRIG,GPIO.OUT) #Error right here
#Set the pins for ECHO, echo is the input pin
GPIO.setup(ECHO,GPIO.IN)
#3.3v sent to trig
GPIO.output(TRIG,1)
#turn off the trig pin
GPIO.output(TRIG,0)
while GPIO.input(ECHO) == 0:
pass
start = time.time()
while GPIO.input(ECHO) == 1:
pass
stop = time.time()
reading = (stop - start) * 17000
return reading
except KeyboardInterrupt:
print ("Interrupted by keyboard")
except:
print ("Other error or exception occured")
finally:
GPIO.cleanup()
How can I fix this? I can't figure out where I'm going wrong