python - Single Ultrasonic Sensor for Raspberry pi 2B+ not functioning from Pi Terminal -
i have been working 4-pin hc-sro4 ultrasonic sensors, 4 @ time. have been developing code make 4 of these sensors work simultaneously , after reorganizing wires installation on project , using basic code run one, cannot make sensor function. code follows:
import rpi.gpio gpio import time trig1 = 15 echo1 = 13 start1 = 0 stop1 = 0 gpio.setmode(gpio.board) gpio.setwarnings(false) gpio.setup(trig1, gpio.out) gpio.output(trig1, 0) gpio.setup(echo1, gpio.in) while true: time.sleep(0.1) gpio.output(trig1, 1) time.sleep(0.00001) gpio.output(trig1, 0) while gpio.input(echo1) == 0: start1 = time.time() print("here") while gpio.input(echo1) == 1: stop1 = time.time() print("also here") print("sensor 1:") print (stop1-start1) * 17000 gpio.cleanup()
after changing wires, sensors, , other components within circuit (including gpio pins) have looked @ code, , added print statements terminal see parts of code running. 1st print statement print("here")
executes consistently, second print statementprint("also here")
not, , @ loss explanation. in other words, why second while loop not being executed? other questions asked here have not worked problem. appreciated.
thanks, h.
here tutorial gaven macdonald might : https://www.youtube.com/watch?v=xacy8l3lsxi
first of all, while block echo1 == 0
loop forever until echo1 becomes 1. in period of time, code inside executed again , again. wouldn't want set time again , again, can this:
while gpio.input(echo1) == 0: pass #this here make while loop nothing , check again. start = time.time() #here set start time. while gpio.input(echo1) == 1: pass #doing same thing, looping until condition true. stop = time.time() print (stop - start) * 170 #note since both values integers, python multiply value 170. if our values string, python write same string again , again: 170 times.
also, best practice, should use try except blocks safely exit code. such as:
try: while true: #code code code... except keyboardinterrupt: #this check if have pressed ctrl+c gpio.cleanup()
Comments
Post a Comment