python - Global name XXX is not defined on RPI interrupt -


i new python raspberry pi. below example program use gpio. problem in conditional cannot see have variables span other statements in block

the entire program included example of problem code follows:

turn on gpio pin 23

            print uptime 

nameerror: global name 'uptime' not defined

any suggestions?

thank in advance

**************************** python code *****************************************

!/usr/bin/env python2.7

import time;

import rpi.gpio gpio time import sleep # lets have time delay (see line 12)

gpio.setwarnings(false)

gpio.setmode(gpio.bcm) # set bcm gpio numbering gpio.setup(17, gpio.in) # set gpio17 input (button)

gpio.setup(23, gpio.out) ## setup gpio pin 23 out

gpio.output(23,false) ## turn off gpio pin 23

define threaded callback function run in thread when events detected

def my_callback(channel):

global start_time, uptime         if gpio.input(17):     # if port 17 == 1             end_time = time.time()             #print (end_time)         print "rising edge detected on 17"             uptime = end_time - start_time                         #uptime = int(uptime)             print uptime             gpio.output(23,false)              ## turn off gpio pin 2         elif (gpio.input(17)== 0):             start_time = time.time()             #print (start_time)         print "falling edge detected on 17"             gpio.output(23,true)              ## turn on gpio pin 23             print uptime 

when changing edge detected on port 17, regardless of whatever

else happening in program, function my_callback run

gpio.add_event_detect(17, gpio.both, callback=my_callback)

raw_input("press enter when ready\n>")

try: print "when pressed, you'll see: rising edge detected on 17" print "when released, you'll see: falling edge detected on 17" sleep(10) # wait 30 seconds print "time's up. finished!" print uptime finally: # block run no matter how try block exits gpio.cleanup() # clean after yourself

if, first time my_callback gets called, gpio.input(17) 1, you'll set uptime , fine.

but happens if first time it's called, gpio.input(17) 0? won't set uptime. and, because first time you've ever called function, , don't have other code sets uptime, hasn't been set yet. try print anyway. nameerror.

you fix by, e.g., setting initial value @ start of problem—e.g.:

def my_callback(channel):     # existing code  uptime = 0 

of course i'm assuming reasonable indentation code. pasted, code mess of indentationerror exceptions , won't anything, , it's quite possible real code has other bugs caused code not being indented under right flow control statement.

and, since looks may mixing tabs , spaces, making indentation errors hard spot: better text editor, automatically converts tabs spaces, auto-indents you, and/or shows tabs in visible way. or @ least run code -tt flag catch tabs errors can fix them.


as side note, elif wasteful, , possibly broken.

if if gpio.input(17): part didn't trigger, know result 0 (or else falsey, since input returns numbers, can 0). so, why test again 0 when can't else?

but meanwhile, you're not re-testing value here unnecessarily, you're going out , reading value again off hardware. so, if edge changes 1 0 , 1 enough, first read may return 0, second read may return 1, meaning neither of branches fires. (plus, reading, may have absorbed edge trigger don't callback, although i'm not sure that.)

you can solve of these problems using else: instead of trying write elif captures opposite sense of if.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -