Activate User Name

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jrrobb
    New Member
    • Oct 2021
    • 1

    Activate User Name

    I wish to use the attached code to run my robot as an Obstacle Avoider. a run check on the code stops at CODEavgDistance =0/CODE and I am unable to find the solution.
    This is on an Raspberry Pi 3 B with latest Rasbian O/S.
    CODEprint ("right")
    stop()
    count=0
    while True:
    i=0
    avgDistance=0
    for i in range(5):
    GPIO.output(TRI G, False) #Set TRIG as LOW
    time.sleep(0.1) #Delay
    GPIO.output(TRI G, True) #Set TRIG as HIGH
    time.sleep(0.00 001) #Delay of 0.00001 seconds
    GPIO.output(TRI G, False) #Set TRIG as LOW
    while GPIO.input(ECHO )==0: #Check whether the ECHO is LOW
    pulse_start = time.time()
    while GPIO.input(ECHO )==1: #Check whether the ECHO is HIGH
    pulse_end = time.time()
    pulse_duration = pulse_end - pulse_start #time to get back the pulse to sensor
    distance = pulse_duration * 17150 #Multiply pulse duration by 17150 (34300/2) to get distance
    distance = round(distance, 2) #Round to two decimal points
    avgDistance=avg Distance+distan ce
    avgDistance=avg Distance/5
    print ("avgDistanc e")
    flag=0
    if avgDistance < 15: #Check whether the distance is within 15 cm range
    count=count+1
    stop()
    time.sleep(1)
    back()
    time.sleep(1.5)
    if (count%3 ==1) & (flag==0):
    right()
    flag=1
    else:
    left()
    flag=0
    time.sleep(1.5)
    stop()
    time.sleep(1)
    forward()
    flag=0/CODE
Working...