python - Galileo and ultrasonic error when distance less than 4cm -


below code ran on intel galileo gen2. i'm wondering why when object come close ultrasonic sensor program stops , complain variable sig "local variable 'sig' referenced before assignment"?

import mraa import time  trig = mraa.gpio(0) echo = mraa.gpio(1)  trig.dir(mraa.dir_out) echo.dir(mraa.dir_in)   def distance(measure='cm'):     trig.write(0)     time.sleep(0.2)      trig.write(1)     time.sleep(0.00001)     trig.write(0)      while echo.read() == 0:             nosig = time.time()      while echo.read() == 1:             sig = time.time()      # et = elapsed time     et = sig - nosig      if measure == 'cm':             distance =  et * 17150     elif measure == 'in':             distance = et / 0.000148     else:                print('improper choice of measurement!!')             distance = none      return distance   while true:     print(distance('cm')) 

your problem spike produced sensor short noticed, sampling-frequency of while echo.read() limited.

this never defines variable sig.

to overcome this, define sig = none when entering function, , later test being none - know can't use measurement.

if want sample higher frequency, need use language faster python, e.g. c++.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -