Я пытался использовать две utrasonics, но проблема в том, что каждый раз, когда эхосигнал первого ультразвука, т.е. ECHO1 низкий, и он всегда в цикле.Вот мой код:
import RPi.GPIO as GPIO #Import GPIO library
время импорта # Библиотека времени импорта
GPIO.setmode (GPIO.BCM) # Установить нумерацию выводов GPIO
TRIG1 = 23 #Associate pin 23 to TRIG
ECHO1 = 24
TRIG2 = 2
ECHO2 = 3 #Associate pin 24 to ECHO
print "Distance measurement in progress"
GPIO.setup(TRIG1,GPIO.OUT) #Set pin as GPIO out
GPIO.setup(ECHO1,GPIO.IN) #Set pin as GPIO in
GPIO.setup(TRIG2,GPIO.OUT) #Set pin as GPIO out
GPIO.setup(ECHO2,GPIO.IN)
while True:
#ultrasonic 1
GPIO.output(TRIG1, False) #Set TRIG as LOW
print "Waitng For Sensor To Settle"
time.sleep(2) #Delay of 2 seconds
GPIO.output(TRIG1, True) #Set TRIG as HIGH
time.sleep(0.00001) #Delay of 0.00001 seconds
GPIO.output(TRIG1, False) #Set TRIG as LOW
while GPIO.input(ECHO1)==0: #Check whether the ECHO is LOW
pulse_start1 = time.time() #Saves the last known time of LOW pulse
while GPIO.input(ECHO1)==1: #Check whether the ECHO is HIGH
pulse_end1 = time.time() #Saves the last known time of HIGH pulse
pulse_duration1 = pulse_end1 - pulse_start1 #Get pulse duration to a variable
distance1 = pulse_duration1 * 17150 #Multiply pulse duration by 17150 to get distance
distance1 = round(distance1, 2) #Round to two decimal points
if distance1 > 2 and distance1 < 400 : #Check whether the distance is within range
print "Distance1 :",distance1 - 0.5,"cm" #Print distance with 0.5 cm calibration
else:
print "Out Of Range" #display out of range
#ultrasonic2
GPIO.output(TRIG2, False) #Set TRIG as LOW
print "Waitng For Sensor To Settle"
time.sleep(2) #Delay of 2 seconds
GPIO.output(TRIG2, True) #Set TRIG as HIGH
time.sleep(0.00001) #Delay of 0.00001 seconds
GPIO.output(TRIG2, False) #Set TRIG as LOW
while GPIO.input(ECHO2)==0: #Check whether the ECHO is LOW
pulse_start2 = time.time() #Saves the last known time of LOW pulse
while GPIO.input(ECHO2)==1: #Check whether the ECHO is HIGH
pulse_end2 = time.time() #Saves the last known time of HIGH pulse
pulse_duration2 = pulse_end2 - pulse_start2 #Get pulse duration to a variable
distance2 = pulse_duration2 * 17150 #Multiply pulse duration by 17150 to get distance
distance2 = round(distance2, 2) #Round to two decimal points
if distance2 > 2 and distance2 < 400 : #Check whether the distance is within range
print "Distance2 :",distance2 - 0.5,"cm" #Print distance with 0.5 cm calibration
else:
print "Out Of Range" #display out of range