RPI.GPIO Help

MRAB python at mrabarnett.plus.com
Sun Aug 16 22:15:30 CEST 2015


On 2015-08-16 20:40, John McKenzie wrote:
>
>   Hello, all. I am hoping some people here are familiar with the RPi.GPIO
> python module for the Raspberry Pi.
>
>   Very new to Python and electronics. Not to computing in general though.
> I posted for help about accepting key presses and then discovered that
> wiring up buttons directly to the Pi was 1/50th as difficult as I thought
> it would be so I am going a different route than keyboard emulation and
> needing GUI toolkits, etc.
>
>   However, I am getting error messages with RPi.GPIO.
>
>   I have three buttons, Red, Yellow and Blue in colour, attached to the
> Pi. The eventual goal is to have pressing one button result in changing
> the colour of an LED lightstrip to that colour and the Pi record how long
> the strip spent as each colour.
>
>   For development purposes I have the controls near me and my desktop
> computer, and the Pi networked to this computer. For now I have each
> button press result in a print statement. When I get this working I will
> replace the print statements with the code to change colours on the LED
> strip using the Blinkstick module.
>
>   This is the basic test code.
>
>
> 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
>      print "Red Button Pressed"
>      while colour == 1:
>          timered += 1
>
> def yellow_button(channel):
>      colour = 2
>      print "Yellow Button Pressed"
>      while colour == 2:
>          timeyellow += 1
>
> def blue_button(channel):
>      colour = 3
>      print "Blue Button Pressed"
>      while colour == 3:
>          timeblue += 1
>
> while True:
>      GPIO.add_event_detect(22, GPIO.RISING, callback=red_button,
> bouncetime=200)
>      GPIO.add_event_detect(23, GPIO.RISING, callback=yellow_button,
> bouncetime=200)
>      GPIO.add_event_detect(24, GPIO.RISING, callback=blue_button,
> bouncetime=200)
>
>
> def exit_handler():
>      print '\033[0;41;37mRed Team:\033[0m ', timered
>      print '\033[0;43;30mYellow Time:\033[0m ', timeyellow
>      print '\033[0;44;37mBlue Time:\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()
>
>
>
>   This results in the error message "RuntimeError: Conflicting edge
> detection already enabled for this GPIO channel".  Running GPIO.cleanup()
> in the interpreter results in a message stating the GPIO pins are not
> assigned and there is nothing to cleanup.
>
>    Removing line 40, the while True: line, removes the error, but the
> program does not sit and wait waiting for a button press, it just runs
> and ends a second later.
>
>     There are other things this script will need, but this is the core
> function  that I need to get working -pressing a button does what I want
> and the script keeps running so I can press another button if I want. If
> are familiar with the RPi.GPIO or see a more general Python mistake that
> could be affecting everything know I would appreciate your help. Thanks.
>
I'm looking at this:

     http://sourceforge.net/p/raspberry-gpio-python/wiki/Inputs/

As far as I can tell, the 'add_event_detect' method adds a callback.

However, you have it in a loop, so you're trying to add more than one
callback (actually, the same one more than once!), which it doesn't
like.

You should add the callbacks only once and then have some kind of sleep
or do-nothing loop, perhaps waiting for the signal to quit (I don't
have a Raspberry Pi, so I don't know what the usual practice is).



More information about the Python-list mailing list