Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #95509
| Subject | Re: RPI.GPIO Help |
|---|---|
| References | <lG5Ax.73238$E26.47630@fx20.iad> <mailman.45.1439756138.4764.python-list@python.org> <F7mBx.11314$Md3.3869@fx01.iad> |
| From | MRAB <python@mrabarnett.plus.com> |
| Date | 2015-08-20 16:45 +0100 |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.24.1440085563.28100.python-list@python.org> (permalink) |
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).
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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