Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #96216

Re: RPI.GPIO Help

Subject Re: RPI.GPIO Help
References <lG5Ax.73238$E26.47630@fx20.iad> <In%Hx.2854$6l6.96@fx08.iad>
From MRAB <python@mrabarnett.plus.com>
Date 2015-09-09 20:29 +0100
Newsgroups comp.lang.python
Message-ID <mailman.290.1441826971.8327.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

RPI.GPIO Help John McKenzie <davros@bellaliant.net> - 2015-08-16 19:40 +0000
  Re: RPI.GPIO Help MRAB <python@mrabarnett.plus.com> - 2015-08-16 21:15 +0100
    Re: RPI.GPIO Help John McKenzie <davros@bellaliant.net> - 2015-08-20 15:12 +0000
      Re: RPI.GPIO Help MRAB <python@mrabarnett.plus.com> - 2015-08-20 16:45 +0100
        Re: RPI.GPIO Help alister <alister.nospam.ware@ntlworld.com> - 2015-08-20 15:54 +0000
      Re: RPI.GPIO Help Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-08-20 21:27 -0400
        Re: RPI.GPIO Help John McKenzie <davros@bellaliant.net> - 2015-08-28 17:40 +0000
          Re: RPI.GPIO Help hakugin.gin@gmail.com - 2015-08-28 13:56 -0700
          Re: RPI.GPIO Help Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-08-29 14:21 -0400
  Re: RPI.GPIO Help John McKenzie <davros@bellaliant.net> - 2015-08-31 17:41 +0000
    Re: RPI.GPIO Help hakugin.gin@gmail.com - 2015-08-31 11:25 -0700
    Re: RPI.GPIO Help MRAB <python@mrabarnett.plus.com> - 2015-08-31 19:34 +0100
    Re: RPI.GPIO Help Johannes Bauer <dfnsonfsduifb@gmx.de> - 2015-09-01 10:58 +0200
  Re: RPI.GPIO Help Tim Daneliuk <tundra@bogus-city.tundraware.com> - 2015-08-31 14:07 -0500
    Re: RPI.GPIO Help Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-09-01 02:08 -0400
  Re: RPI.GPIO Help John McKenzie <davros@bellaliant.net> - 2015-09-02 18:50 +0000
  Re: RPI.GPIO Help John McKenzie <davros@bellaliant.net> - 2015-09-09 19:03 +0000
    Re: RPI.GPIO Help MRAB <python@mrabarnett.plus.com> - 2015-09-09 20:29 +0100
  Re: RPI.GPIO Help John McKenzie <davros@bellaliant.net> - 2015-09-10 15:56 +0000
    Re: RPI.GPIO Help John McKenzie <davros@bellaliant.net> - 2015-09-11 18:24 +0000
      Re: RPI.GPIO Help MRAB <python@mrabarnett.plus.com> - 2015-09-11 19:49 +0100
      Re: RPI.GPIO Help hakugin.gin@gmail.com - 2015-09-11 12:00 -0700
      Re: RPI.GPIO Help hakugin.gin@gmail.com - 2015-09-11 12:24 -0700
      Re: RPI.GPIO Help Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-09-11 21:10 -0400
  Re: RPI.GPIO Help John McKenzie <davros@bellaliant.net> - 2015-09-13 06:08 +0000
    Re: RPI.GPIO Help Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-09-13 12:24 -0400
    Re: RPI.GPIO Help hakugin.gin@gmail.com - 2015-09-14 05:53 -0700

csiph-web