Path: csiph.com!news.swapon.de!news.roellig-ltd.de!open-news-network.org!border2.nntp.ams1.giganews.com!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed7.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'elif': 0.04; 'handler': 0.04; '#if': 0.05; 'initialize': 0.05; '"__main__":': 0.07; '__name__': 0.07; 'escape': 0.07; 'executed': 0.07; 'exit': 0.07; 'granted,': 0.07; 'method,': 0.07; '"b":': 0.09; '"r")': 0.09; "'\\n')": 0.09; 'behave': 0.09; 'callback': 0.09; 'loop.': 0.09; 'message-id:@4ax.com': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'replied': 0.09; 'statements': 0.09; 'wrong,': 0.09; 'subject:Help': 0.10; 'python': 0.10; 'def': 0.13; 'thu,': 0.15; '"a")': 0.16; '"b")': 0.16; '"blue': 0.16; '#create': 0.16; '#this': 0.16; "'a')": 0.16; "'blue": 0.16; '>to': 0.16; 'atexit': 0.16; 'loops': 0.16; 'pressed': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'skip:> 20': 0.16; 'team:': 0.16; 'time.time()': 0.16; 'true:': 0.16; 'string': 0.17; 'element': 0.18; 'laura': 0.18; 'skip': 0.18; 'url:home': 0.18; 'runs': 0.18; 'library': 0.20; 'changes': 0.20; '2015': 0.20; 'aug': 0.20; 'issue.': 0.20; 'ok.': 0.22; 'rid': 0.22; '(or': 0.23; 'tried': 0.24; 'import': 0.24; 'testing': 0.25; "doesn't": 0.26; 'header:X-Complaints-To:1': 0.26; "skip:' 10": 0.28; 'argue': 0.29; 'declared': 0.29; 'does,': 0.29; 'loop,': 0.29; 'sleep': 0.29; 'objects': 0.29; "i'm": 0.30; 'print': 0.30; 'comments': 0.30; 'code': 0.30; 'skip:g 30': 0.30; 'error.': 0.31; 'anyone': 0.32; 'skip:_ 10': 0.32; 'getting': 0.33; 'run': 0.33; 'point': 0.33; 'class': 0.33; 'problem': 0.33; 'raising': 0.33; 'skip:- 10': 0.34; 'channel': 0.34; "skip:' 20": 0.34; 'could': 0.35; 'done': 0.35; 'false': 0.35; 'skip:> 10': 0.35; 'asking': 0.35; "isn't": 0.35; 'reply.': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'assigned': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'turn': 0.37; 'thanks': 0.37; 'received:org': 0.37; 'charset:us-ascii': 0.37; 'seem': 0.37; 'things': 0.38; 'doing': 0.38; 'version': 0.38; 'button': 0.38; 'anything': 0.38; 'skip:p 20': 0.38; 'skip:o 20': 0.38; 'means': 0.39; 'test': 0.39; 'format': 0.39; 'does': 0.39; 'to:addr:python.org': 0.40; 'your': 0.60; 'determine': 0.61; 'john': 0.61; 'skip:u 10': 0.61; 'total': 0.62; 'more': 0.63; 'pin': 0.66; 'color': 0.67; 'news': 0.68; 'led': 0.72; '>def': 0.84; '>with': 0.84; 'regain': 0.84; 'rgb': 0.84; 'colour': 0.91; 'dennis': 0.91; 'received:108': 0.93; 'instant': 0.98 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dennis Lee Bieber Subject: Re: RPI.GPIO Help Date: Thu, 20 Aug 2015 21:27:19 -0400 Organization: IISS Elusive Unicorn References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: adsl-108-68-178-61.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 6.00/32.1186 X-No-Archive: YES 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: 176 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1440120462 news.xs4all.nl 23730 [2001:888:2000:d::a6]:34356 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:95514 On Thu, 20 Aug 2015 15:12:37 GMT, John McKenzie declaimed the following: > > 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. Just curious, but have you considered asking in the news group comp.sys.raspberry-pi? Granted, most of them seem to argue more about getting the OS to behave than in programming... > >led = blinkstick.find_first() I don't see you doing anything with the LED... >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): What is "channel"? Does it map to the ID of the pin that triggered? If it does, you could get rid of the three callback functions and just use on callback with a set of IF statements testing "channel" to determine the button. > colour = 1 > while colour == 1: > print "Red Button pressed" > timered += 1 timered is not declared "global", so will be considered local to the function... And since it isn't initialized, should be raising an error. You initialize "colour" to 1, and then loop until "colour" is NOT 1 -- but there is nothing inside the loop that changes the value of colour, so the loop will never exit, which means the callback never exits, which means the GPIO process will never regain control to test for other button presses. > >while True: > time.sleep(0.1) > You start an infinite loop, nothing below this point will be executed meaning... >def exit_handler(): The exit handler will not be defined... > 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') Ugh... Please read up on Python string interpolation (or format method, depending on Python version) I don't have an R-Pi, nor your colorful LED, so this is just pseudo-code... -=-=-=-=-=-=- import atexit import time import blinkstick as bs import RPi.GPIO as GPIO class Button(object): modeSet = False def __init__(self, buttonPin, LED, colorCode): self.totalTime = 0 self.downTime = 0 self.LED = LED if colorCode.lower() in "rgb": self.colorCode = colorCode else: self.colorCode = None if not Button.modeSet: #classwide (shared) value GPIO.setmode(GPIO.BCN) #so only performed once Button.modeSet = True GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.add_event_detect(buttonPin, GPIO.RISING, callback=self.buttonDown, bouncetime=200) GPIO.add_event_detect(buttonPin, GPIO.FALLING, #guessing callback=self.buttonUp, bouncetime=200) def buttonDown(self, buttonPin): #this design doesn't use the pin self.downTime = time.time() #record when button pressed if self.colorCode: #if a valid RGB assigned (r, g, b) = LED.get_color() #modify LED color if self.colorCode == "r": LED.set_color(red=0xFF, green=g, blue=b) elif self.colorCode == "g": LED.set_color(red=r, green=0xFF, blue=b) elif self.colorCode == "b": LED.set_color(red=r, green=g, blue=0xFF) def buttonUp(self, buttonPin): self.totalTime += (time.time() - self.downTimp) #increment total time self.downTime = 0 #just paranoia if self.colorCode: #if valid RGB, turn the element off if self.colorCode == "r": LED.set_color(red=0x00, green=g, blue=b) elif self.colorCode == "g": LED.set_color(red=r, green=0x00, blue=b) elif self.colorCode == "b": LED.set_color(red=r, green=g, blue=0x00) def exit_handler(): # I don't have a reference to terminal escape codes, so # I'm going to skip that part print "Red Team :\t%s" % redButton.totalTime print "Green Team:\t%s" % greenButton.totalTime print "Blue Team :\t%s" % blueButton.totalTime flog = open("flag1.log", "a") flog.write("%s\n" % time.strftime("%H:%M:%S")) flog.write("Red Team :\t%s\n" % redButton.totalTime) flog.write("Green Team:\t%s\n" % greenButton.totalTime) flog.write("Blue Team :\t%s\n\n" % blueButton.totalTime) flog.close() GPIO.cleanup() if __name__ == "__main__": LED = bs.blinkstick().find_first() #get LED object redButton = Button(22, LED, "r") #create button objects greenButton = Button(23, LED, "g") blueButton = Button(24, LED, "b") atexit.register(exit_handler) #register exit handler while True: time.sleep(0) #loop forever -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/