Path: csiph.com!goblin2!goblin.stu.neva.ru!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'elif': 0.04; 'correct.': 0.07; '"\\n")': 0.09; 'buttons': 0.09; 'expected.': 0.09; 'timestamp': 0.09; 'subject:Help': 0.10; 'python': 0.10; 'def': 0.13; '"a")': 0.16; '"blue': 0.16; '"while': 0.16; 'acted': 0.16; 'atexit': 0.16; 'elsewhere.': 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; 'newsgroup.': 0.16; 'received:192.168.1.4': 0.16; 'red,': 0.16; 'team:': 0.16; 'true:': 0.16; 'variable.': 0.16; 'wrote:': 0.16; 'else,': 0.18; 'foundation': 0.19; 'together.': 0.20; 'suggested': 0.20; 'work,': 0.21; 'strip': 0.22; 'appears': 0.23; '(or': 0.23; 'seems': 0.23; 'tried': 0.24; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'command': 0.26; 'skip:" 20': 0.26; 'appreciated.': 0.27; 'correct': 0.28; 'went': 0.28; 'skip:( 20': 0.28; 'loop,': 0.29; 'once.': 0.29; 'str': 0.29; 'reset': 0.29; 'asked': 0.29; 'print': 0.30; 'work.': 0.30; 'code': 0.30; 'another': 0.32; 'posting': 0.32; 'maybe': 0.33; 'changed': 0.33; 'problem': 0.33; 'changing': 0.34; 'know.': 0.34; 'add': 0.34; 'advice': 0.35; 'asking': 0.35; 'but': 0.36; 'should': 0.36; 'instead': 0.36; 'needed': 0.36; 'there': 0.36; 'to:addr:python- list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'turn': 0.37; 'suggestion': 0.37; 'anything': 0.38; 'skip:p 20': 0.38; 'someone': 0.38; 'from:': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'close': 0.61; 'john': 0.61; 'black': 0.61; 'per': 0.62; 'making': 0.62; 'skip:y 20': 0.63; 'more': 0.63; 'between': 0.65; 'here': 0.66; 'repeat': 0.67; 'answer.': 0.72; 'led': 0.72; 'discovered': 0.83; 'wiring': 0.84; 'colour': 0.91; 'dennis': 0.91; 'forever.': 0.93 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=CvRCCSMD c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=EBOSESyhAAAA:8 a=IkcTkHD0fZMA:10 a=VDrA-ktzNwNi2nxWJ78A:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett@:2500 Subject: Re: RPI.GPIO Help To: python-list@python.org References: From: MRAB Date: Wed, 9 Sep 2015 20:29:26 +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: 116 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1441826971 news.xs4all.nl 23848 [2001:888:2000:d::a6]:42587 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:96216 On 2015-09-09 20:03, John McKenzie wrote: > > Hello. > > As per the suggestion of two of you I went to the Raspberry Pi > newsgroup. Dennis is also there and has been posting in response to my > problems. Between there and the Raspberry Foundation website I discovered > that my wiring did not match my code and changed all PUD_DOWN to PUD_UP > and all GPIO.RISING to GPIO.FALLING because I was wired to the ground pin. > > Someone suggested code very close to what hakugin suggested. > > Right now I have response buttons that work, but the colour of the LED > strip is not correct. It adds colours instead of changing to a new one. I > hit red, it pulses red, I hit blue, it pulses red and blue, making purple. > > It appears now my problem is more about Python usage than anything else, > so I am asking for help here as well as having already asked elsewhere. > > Tried using a go to black command (led.set_color(name="black")) and a > turn off command (led.turn_off()) before the pulse command to reset the > colour and these did not work. To see what would happen I removed the > "while colour == 1:" (or == 2: or ==3:) line and it acted as expected. > The LED pulsed once. However, it would pulse the correct colour, it would > not add colours together. > > So the while loop seems to be the cause of my problem, but it is needed > to keep the pulse repeating as far I can know. Maybe Python has another > way to repeat the function. > > Here is the code I was working from: > > import atexit > import time > from blinkstick import blinkstick > import RPi.GPIO as GPIO > > led = blinkstick.find_first() > colour = 0 > time_red = 0 > time_yellow = 0 > time_blue = 0 > timestamp = time.strftime("%H:%M:%S") > > GPIO.setmode(GPIO.BCM) > GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP) > GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP) > GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP) > > > def red_button(channel): > led.set_color(name="black") > colour = 1 > while colour == 1: > led.pulse(red=255, green=0, blue=0, repeats=1, duration=3000, > steps=50) > > def yellow_button(channel): > led.set_color(name="black") > colour = 2 > while colour == 2: > led.pulse(red=255, green=255, blue=0, repeats=1, duration=3000, > steps=50) > > def blue_button(channel): > led.set_color(name="black") > colour = 3 > while colour == 3: > led.pulse(red=0, green=0, blue=255, repeats=1, duration=2000, > steps=50) > > > GPIO.add_event_detect(22, GPIO.FALLING, callback=red_button, > bouncetime=200) > GPIO.add_event_detect(23, GPIO.FALLING, callback=yellow_button, > bouncetime=200) > GPIO.add_event_detect(24, GPIO.FALLING, callback=blue_button, > bouncetime=200) > > > while True: > if colour == 1: > time_red += 1 > elif colour == 2: > time_yellow += 1 > elif colour == 3: > time_blue += 1 > time.sleep(0.1) > > > def exit_handler(): > print "\033[0;41;37mRed Team:\033[0m ", time_red > print "\033[0;43;30mYellow Time:\033[0m ", time_yellow > print "\033[0;44;37mBlue Time:\033[0m ", time_blue > flog = open("flag1.log", "a") > flog.write(timestamp + "\n" + "Red Team: " + str(time_red) + "\n" + > "Yellow Team: " + str(time_yellow) + "\n" + "Blue Team: " + str > (time_blue) + "\n") > flog.close() > GPIO.cleanup() > atexit.register(exit_handler) > > > Any advice about the while loop for the colour pulsing is appreciated. > It's the same problem as before, and it has the same answer. In red_button, yellow_button and blue_button, 'colour' is a local variable. It's set to a value and that value is never changed in the loop, so it loops forever. You should declare 'colour' to be global in all 3 functions, e.g.: def red_button(channel): global colour