papous
Posts: 80
Joined: Fri Jan 05, 2018 5:50 am

Help with code weirdness

Mon Jan 08, 2018 8:44 pm

Why does this works (with the distance sensor)?

Code: Select all

import RPi.GPIO as GPIO 
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

TRIG = 17 
ECHO = 18
tdist=[]
dist=0
#print ("Distance Measurement In Progress")

GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)

GPIO.output(TRIG, False)
#print ("Waiting For Sensor To Settle")
time.sleep(1)


#def distance():
for cntr in range(1, 10):

    try: # Repeat the next indented block forever

        GPIO.output(TRIG, True)
        time.sleep(0.00001)
        GPIO.output(TRIG, False)

        while GPIO.input(ECHO)==0:
          pulse_start = time.time()
          #print (pulse_start)

        while GPIO.input(ECHO)==1:
            pulse_end = time.time()

        pulse_duration = pulse_end - pulse_start
        tdist.append(pulse_duration * 34300/2)
        #return tdist
    
    except KeyboardInterrupt:
        GPIO.cleanup() 

def median(thelist):
    sorted_list = sorted(thelist)
    length = len(sorted_list)
    center = length // 2
    print (length)
    if length == 1:
        return sorted_list[0]
    elif length % 2 == 0:
        return sum(sorted_list[center - 1: center + 1]) / 2.0
    else:
        return sorted_list[center]


print('Dist',median(tdist))
       
and this does not?

Code: Select all

import RPi.GPIO as GPIO 
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

TRIG = 17 
ECHO = 18
tdist=[]
dist=0
#print ("Distance Measurement In Progress")

GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)

GPIO.output(TRIG, False)
#print ("Waiting For Sensor To Settle")
time.sleep(1)


def distance():
    for cntr in range(1, 10):

        try: # Repeat the next indented block forever

            GPIO.output(TRIG, True)
            time.sleep(0.00001)
            GPIO.output(TRIG, False)

            while GPIO.input(ECHO)==0:
              pulse_start = time.time()
              #print (pulse_start)

            while GPIO.input(ECHO)==1:
                pulse_end = time.time()

            pulse_duration = pulse_end - pulse_start
            tdist.append(pulse_duration * 34300/2)
        
            return median(tdist)
        
        except KeyboardInterrupt:
            GPIO.cleanup() 


def median(thelist):
    sorted_list = sorted(thelist)
    length = len(sorted_list)
    center = length // 2
    print ('length',length)
    if length == 1:
        return sorted_list[0]
    elif length % 2 == 0:
        return sum(sorted_list[center - 1: center + 1]) / 2.0
    else:
        return sorted_list[center]

print('Dist',distance())


pcmanbob
Posts: 9897
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Help with code weirdness

Mon Jan 08, 2018 9:17 pm

Saying the second code listing does not work is not giving us any information.
What happens when you run it. Do you get an error message if so try posting it in its entirety.

Remember people may not have the same hardware available to test your code.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

papous
Posts: 80
Joined: Fri Jan 05, 2018 5:50 am

Re: Help with code weirdness

Mon Jan 08, 2018 9:26 pm

You are right.
The first code will return the median of 10 values from the distance sensor.
I tried to 'convert' it to a function and I only get 1 reading. trying to unindent the RETURN keyword produces the syntax error: invalid syntax
thank you

Raspberry3B

papous
Posts: 80
Joined: Fri Jan 05, 2018 5:50 am

Re: Help with code weirdness

Mon Jan 08, 2018 9:30 pm

solved it
removing try: loop makes the thing work

Thanks

Return to “Python”