RPI.GPIO Help

hakugin.gin at gmail.com hakugin.gin at gmail.com
Fri Aug 28 22:56:21 CEST 2015


On Friday, August 28, 2015 at 1:40:41 PM UTC-4, John McKenzie wrote:
> Thanks for the replies, everyone.
> 
>  Two of you suggested I ask in comp.sys.raspberry-pi. We leave in a world 
> where people cannot tell you the difference between the world wide web 
> and the Internet and tech support for my ISP once told me in response to 
> mentioning that thei news server was not functioning properly that I 
> should "try turning on cookies" and further suggested I speak to the 
> "owner of usenet" for help. Because of this new world it never occured to 
> me that a new newsgroup would have been created in the last five years. I 
> expected to be laughed at if I asked about Raspberry Pi having a presence 
> on usenet. So thank you both for suggesting it. I will absolutely move my 
> question there then come back here for more straight forward pure Python 
> stuff.
> 
> 
> Dennis replied in detail and asked a question of me.
> 
> >	I don't see you doing anything with the LED...
> 
>  As I mentioned in my original post I intend to turn LED lights a 
> different colour but for developmented purposes I anm replace the code 
> for that with a print statement. LED variable was leftover from the 
> original script and I forgot to delete that line when sharing the 
> development version.
> 
> >What is "channel"?
> 
>  Channel is something that is in every GPIO library example, and I pretty 
> sure it is literal, not a placeholder. If I replace it with channel 
> numbers it gives more errors. Channel is in place in working script 
> examples you can download.
> 
> 
> >	timered is not declared "global", so will be considered local 
> to><> the
> 
>  Rereading global variables info. Never understood why all variables 
> aren't always global at all times in any language. Why would you not want 
> the variable accessible whenever you wanted it? Maybe it is related to 
> performance or something. Anyway, will look at the scopes of variables 
> again and look at the code again.
> 
> 
> >	You initialize "colour" to 1, and then loop until "colour" is NOT 
> 1 --
> 
>  I thought the existance of the other callbacks would be able to interupt 
> it. Thanks for another point for me to look into and learn about.
> 
> 
> >	You start an infinite loop, nothing below this point will be 
> executed
> 
>  The start of the infinite sleep loop is nessecary to have the script go 
> reiterate instead of run once in under a second and stop. Not saying this 
> has to be the way to do it, just explaining why I did it. What I used was 
> the example used in dozens and dozens of answers given online to solve 
> that same problem when using RPI.GPIO.
> 
> 
> >	The exit handler will not be defined...
> >	Ugh... Please read up on Python string interpolation (or format 
> method,
> >depending on Python version)
> 
>  Not sure what you are saying about that part of the code because it is 
> the one part of the code that works perfectly. I copied from a generic 
> example to start with and added my own text and ANSI codes and in every 
> test it does what it is supposed exactly as it is supposed to.
> 
> 
>  In addition to wanting to say thanks for your comments, I would like to 
> say thanks for your example pseudo-code. I appreciate it and will, like 
> you warned, keep in mind you do not have a Pi.
> 
> 
>  Thanks everyone who replied to my questions for that matter.
> 
>  Now that I finnally have some time to work on this weekend I will take 
> all your comments in and look at the code suggestions provided. Thank you.

This issue is not directly related to the PI itself, it is from not having much experience with Python. While I do not have my RPI handy to test with give the following a try...

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_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"

def yellow_button(channel):
    colour = 2
    print "Yellow Button Pressed"

def blue_button(channel):
    colour = 3
    print "Blue Button Pressed"

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)
    if colour == 1:
        time_red += 1
    elif colour == 2:
        time_yellow += 1
    elif colour == 3:
        time_blue += 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('flag1log.text', '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()
atexit.register(exit_handler)
GPIO.cleanup()


I largely left your code intact. First I changed the name of your variables to make them easier to read, obviously this can be changed back. Second, I moved your "timer code" from the functions and into the while loop. I admit, this could cause your "timer" variables to get fairly large, pretty quickly.

I strongly recommend looking up how to create a stop watch script and have each button start a timer when pressed, making sure to total your results for each colour when the next button is pressed. I apologize that I do not have the time to provide example code.


More information about the Python-list mailing list