Path: csiph.com!news.swapon.de!eternal-september.org!feeder.eternal-september.org!newsfeed1.swip.net!uio.no!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'variable,': 0.07; "'\\n')": 0.09; 'assigning': 0.09; 'callback': 0.09; 'global,': 0.09; 'loop.': 0.09; 'replied': 0.09; 'timestamp': 0.09; 'wrong,': 0.09; 'subject:Help': 0.10; 'python': 0.10; 'def': 0.13; '"blue': 0.16; "'a')": 0.16; "'blue": 0.16; 'atexit': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'loops': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'team:': 0.16; 'true:': 0.16; 'wrote:': 0.16; 'basically': 0.18; 'laura': 0.18; 'runs': 0.18; 'library': 0.20; 'issue.': 0.20; 'function,': 0.22; 'ok.': 0.22; 'trying': 0.22; 'tried': 0.24; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'function': 0.28; "skip:' 10": 0.28; 'looks': 0.29; 'loop,': 0.29; 'occurred': 0.29; 'sleep': 0.29; 'raise': 0.29; 'print': 0.30; 'comments': 0.30; 'code': 0.30; 'anyone': 0.32; 'received:84': 0.32; 'run': 0.33; 'problem': 0.33; 'similar': 0.33; 'changing': 0.34; 'channel': 0.34; "skip:' 20": 0.34; 'could': 0.35; 'done': 0.35; 'reply.': 0.35; 'but': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'thanks': 0.37; 'things': 0.38; 'busy': 0.38; 'version': 0.38; 'button': 0.38; 'skip:p 20': 0.38; 'skip:o 20': 0.38; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'called': 0.40; 'press': 0.61; 'john': 0.61; 'skip:u 10': 0.61; 'skip:y 20': 0.63; 'led': 0.72; 'detecting': 0.84; 'each,': 0.84; 'remarks': 0.84; 'rising': 0.84; 'colour': 0.91; 'instant': 0.98 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=MbeRwMLf c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=EBOSESyhAAAA:8 a=Yo2XpZhZnQAA:10 a=IkcTkHD0fZMA:10 a=GWd8tLBbcKknkk98wC4A:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett@:2500 Subject: Re: RPI.GPIO Help To: python-list@python.org References: From: MRAB Date: Thu, 20 Aug 2015 16:45:53 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 93 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1440085563 news.xs4all.nl 23797 [2001:888:2000:d::a6]:41660 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:95509 On 2015-08-20 16:12, John McKenzie wrote: > > Thanks for the reply. Also, thanks to Laura who replied via email. > > Tried a bunch of things based off these comments and I always ended up > with one of two situations, the channel conflict error, or an instant run > and quit issue. This new version of the code runs but is unresponsive. I > removed loops then put in a short sleep loop. while True: > time.sleep(0.1) It could be my hardware is done up wrong, but it > looks OK. Perhaps it is always sleeping. > > Anyone at all know about GPIO and the Pi under the Python library > RPi.GPIO please feel free to advise as to what the problem is most likely > to be. > > > import atexit > import time > from blinkstick import blinkstick > import RPi.GPIO as GPIO > > led = blinkstick.find_first() > colour = 0 > timered = 0 > timeyellow = 0 > timeblue = 0 > timestamp = time.strftime("%H:%M:%S") > > > > GPIO.setmode(GPIO.BCM) > GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) > GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) > GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) > > > > def red_button(channel): > colour = 1 > while colour == 1: > print "Red Button pressed" > timered += 1 > > > def yellow_button(channel): > colour = 2 > while colour == 2: > print "Yellow Button pressed" > timeyellow += 1 > > > def blue_button(channel): > colour = 3 > while colour == 3: > print "Blue Button pressed" > timeblue += 1 > > GPIO.add_event_detect(23, GPIO.RISING, callback=yellow_button, > bouncetime=200) > GPIO.add_event_detect(22, GPIO.RISING, callback=red_button, > bouncetime=200) > GPIO.add_event_detect(24, GPIO.RISING, callback=blue_button, > bouncetime=200) > > while True: > time.sleep(0.1) > > def exit_handler(): > print '\033[0;41;37mRed Team:\033[0m ', timered > print '\033[0;103;30mYellow Team:\033[0m ', timeyellow > print '\033[0;44;37mBlue Team:\033[0m ', timeblue > flog = open('flag1log.text', 'a') > flog.write(timestamp + '\n' + 'Red Team: ' + str(timered) + '\n' + > 'Yellow Team: ' + str(timeyellow) + '\n' + 'Blue Team: ' + str(timeblue) > + '\n') > flog.close() > atexit.register(exit_handler) > GPIO.cleanup() > > The function 'red_button' will be called when a rising edge is detected. In that function, you're assigning to 'colour' but not changing it in the loop, so it's basically just a busy loop. However, you're trying to change 'timered', which is a global variable, but you're not declaring it as global, so that will raise UnboundLocalError. Similar remarks apply to the other two callbacks. I think you'd be better off detecting both the rising and falling edges, with a callback for each, recording when each edge occurred (duration of press = time of falling edge - time of rising edge).