RPI.GPIO Help

John McKenzie davros at bellaliant.net
Sun Sep 13 08:08:54 CEST 2015


 Hello, there.

 MRAB, thank you for teaching me proper Python syntax for how I tried to 
use the or operator.

 Dennis, I must have learned allot recently as I believe I understood 99% 
of that code. I see how it is not just more advanced, but actually better 
than what I had. However, line 47 (cumTime[colour] += now - changeTime) 
had an error I could not figure out what to do about. Considering my time 
line I decided not to leave it to latter. Will be using these electronic 
flags at next year's game and for then I intend to just be better about 
everything. Will use your code to learn to get there.

 Hakugin, thank you you as well. I took the basic ideas you showed me for 
improvement and used them. The pulse settings variable was not liked by 
the interpreter, so I simplified it. I turned it into a hex value for 
each button press, then in the main loop I inserted the whole line for 
the LED pulse command, but put "pulse_settings" after "hex=" in the 
arguments. This worked.

 I added a few green blinks of the LED to indicate the starting and 
stopping of the script. Also, I got the log files score print outs upon 
exit working. Very important, and also importantly, I have it so it stops 
after a certain amount of time. For testing, I have it at 60 seconds, but 
games will be 3600 seconds on average when really being used.

 The stopping after a certain amount of time was done in a way that 
apparently technically works, but seems very weird and probably wrong to 
me. You may freak out when you see it. I used an else statement inside a 
while loop and it just feels so strange. At least it works.

 Hoping I might be able to make it so I boot the Pi, it loads the script, 
script waits for the user to tell it how long to make a game, game 
starts, scripts ends game at appropriate time, saves dated log file with 
scores, then waits for user to enter new game length to start new game. 
This is probably way to much to hope to accomplish in time. For next year 
for sure though. It would be better for the referees to operate that way.

 Next I will try and integrate wireless communications. If anyone here 
knows Synapse RF modules well, or at all, PLEASE contact me.


 Here is the code I did up most recently. Again, thanks for all the 
suggestions, code examples, and general help.

import atexit
import sys
import time 
from blinkstick import blinkstick 
import RPi.GPIO as GPIO   

gamestart = time.time()
gamelength = 60

led = blinkstick.find_first() 
colour = 0
time_red = 0
time_yellow = 0
time_blue = 0
timestamp = time.strftime("%H:%M:%S")
pulse_settings = []

led.blink(name="green", repeats=2)

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):
    global colour
    global pulse_settings
    if colour != 1:
        colour = 1
        pulse_settings = "#FF0000"

def yellow_button(channel):
    global colour
    global pulse_settings
    if colour != 2:
        colour = 2
        pulse_settings = "#FF8900" # Corrected yellow for cheap LED strip.

def blue_button(channel):
    global colour
    global pulse_settings
    if colour != 3:
        colour = 3
        pulse_settings = "#0000FF"


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)

def exit_handler():
    print "\033[0;41;37mRed Team:\033[0m ", time_red
    print "\033[0;103;30mYellow Team:\033[0m ", time_yellow
    print "\033[0;44;37mBlue Team:\033[0m ", time_blue
    flog = open("flag1.log", "a")
    flog.write("\n" + timestamp + "\n" + "Red Team: " + str(time_red) + 
"\n" + "Yellow Team: " + str(time_yellow) + "\n" + "Blue Team: " + str
(time_blue) + "\n")
    flog.close()
    led.blink(name="green", repeats=4)
    led.set_color(name="black")
atexit.register(exit_handler)


while time.time() < gamestart + gamelength:
    if colour == 1:
        time_red += 1
    elif colour == 2:
        time_yellow += 1
    elif colour == 3:
        time_blue += 1
    led.pulse(hex=pulse_settings, repeats=1, duration=2000, steps=50)
    time.sleep(0.1)
else:
    sys.exit()

GPIO.cleanup()






More information about the Python-list mailing list