RPI.GPIO Help

John McKenzie davros at bellaliant.net
Wed Sep 9 21:03:04 CEST 2015


 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.


More information about the Python-list mailing list