Re: RPI.GPIO Help

2015-09-14 Thread hakugin . gin
On Sunday, September 13, 2015 at 2:09:11 AM UTC-4, John McKenzie wrote:
> Hello, 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 realized the error with my example after I got home and forgot to update it. 
The issue was with:

(red=255, green=0, blue=0, repeats=1, duration=2000, steps=50)

I had copied and pasted that and should have removed the "red=", "green=", etc. 
I'm glad you were able to work it out.

>  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.
> 

Having the game load when the Pi starts is fairly easy, but I'd recommend 
searching the official Raspberry Pi forums. Last time I was there I saw quite a 
few posts on various ways to accomplish this.

As for allowing someone to enter a game time limit you can change your 
"gamelength" variable assignment to something along the lines of:

gamelength = None

while not isinstance(gamelength, int):
gamelength = raw_input("Please enter a game time limit:\n")
try:
gamelength = int(gamelength) # Converts to integer
except:
print("Invalid entry. Time limit must be a number.\n")
pass # ignore error and allow the loop to start over

I was able to test this code, and you will want to add it before your main game 
loop.

>  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.
> 



> 
> while time.time() < gamestart + gamelength:
>

This is actually how I personally would have accomplished the task of having it 
end at a specific time. There may be other ways though.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: RPI.GPIO Help

2015-09-13 Thread John McKenzie

 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 = "#FF"

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 = "#FF"


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()




-- 
https://mail.python.org/mailman/listinfo/python-list


Re: RPI.GPIO Help

2015-09-11 Thread John McKenzie

 Hello.

 Thanks to the help of people here and in other newsgroups I seem to have 
something working doing the basics. (Buttons work, colours light up 
appropriately.)

 When I followed MRAB's instructions and read about scopes of variables 
that solved my most recent problem, but it introduced a bug. I think I 
fixed the bug but after all my stupid mistakes and forgetfulness that 
seems too good to be true. I expect there is a better, more elegant, or 
more Pythonic way to do what I did so please feel free to share on the 
subject.

 I had a problem where if I pressed a button while the LEDs were already 
flashing the colour of that button it would block a new colour from 
starting when I pressed a new button. So if the LED strip was red and I 
pressed the red button again nothing would happen when I pressed the blue 
or yellow button. Similar problem for the other two buttons.

 So inside my callbacks I added this code:

   if colour == 1:
pass
elif colour == 2 or 3:
colour = 1


 Now it seems OK from my limited testing.


 Here is the code that has buttons and colours working and includes my 
bug fix:


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):
global colour
if colour == 1:
pass
elif colour == 2 or 3:
colour = 1
while colour == 1:
led.pulse(red=255, green=0, blue=0, repeats=1, duration=2000, 
steps=50)
def yellow_button(channel):
global colour
if colour == 2:
pass
elif colour == 1 or 3:
colour = 2
while colour == 2:
led.pulse(red=255, green=96, blue=0, repeats=1, 
duration=2000, steps=50)
def blue_button(channel):
global colour
if colour == 3:
pass
elif colour == 1 or 2:
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()
led.set_color(name="black")
atexit.register(exit_handler)
GPIO.cleanup()



 I think I am OK GPIO wise now, although always happy to improve the code 
and in the long term I want to do so.

 Will start new threads for more straight forward Python questions like 
help with saving a log of the results, timing, etc.

 Thanks for your help, everyone.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: RPI.GPIO Help

2015-09-11 Thread MRAB

On 2015-09-11 19:24, John McKenzie wrote:


  Hello.

  Thanks to the help of people here and in other newsgroups I seem to have
something working doing the basics. (Buttons work, colours light up
appropriately.)

  When I followed MRAB's instructions and read about scopes of variables
that solved my most recent problem, but it introduced a bug. I think I
fixed the bug but after all my stupid mistakes and forgetfulness that
seems too good to be true. I expect there is a better, more elegant, or
more Pythonic way to do what I did so please feel free to share on the
subject.

  I had a problem where if I pressed a button while the LEDs were already
flashing the colour of that button it would block a new colour from
starting when I pressed a new button. So if the LED strip was red and I
pressed the red button again nothing would happen when I pressed the blue
or yellow button. Similar problem for the other two buttons.

  So inside my callbacks I added this code:

if colour == 1:
 pass
 elif colour == 2 or 3:
 colour = 1


  Now it seems OK from my limited testing.


[snip]

"colour == 2 or 3" means the same as "(colour == 2) or 3", where 3 is a
true value (zero is a false value; any non-zero number is a true value).

What you mean is "colour == 2 or colour == 3".

A shorter alternative is "colour in (2, 3)".

--
https://mail.python.org/mailman/listinfo/python-list


Re: RPI.GPIO Help

2015-09-11 Thread hakugin . gin
On Friday, September 11, 2015 at 2:25:15 PM UTC-4, John McKenzie wrote:
> Hello.
> 
>  Thanks to the help of people here and in other newsgroups I seem to have 
> something working doing the basics. (Buttons work, colours light up 
> appropriately.)



> def red_button(channel):
> global colour
> if colour == 1:
> pass
> elif colour == 2 or 3:
> colour = 1
> while colour == 1:
> led.pulse(red=255, green=0, blue=0, repeats=1, duration=2000, 
> steps=50)



Another option for this would be:

def red_button(channel):
global colour
if colour != 1:
colour = 1
# Rest of your original code goes below here


I am currently checking some other options, unfortunately I am not at home or 
have access to a RPi right now so I am unable to fully test.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: RPI.GPIO Help

2015-09-11 Thread hakugin . gin
On Friday, September 11, 2015 at 2:25:15 PM UTC-4, John McKenzie wrote:
> Hello.
> 
>  Thanks to the help of people here and in other newsgroups I seem to have 
> something working doing the basics. (Buttons work, colours light up 
> appropriately.)
> 
>  When I followed MRAB's instructions and read about scopes of variables 
> that solved my most recent problem, but it introduced a bug. I think I 
> fixed the bug but after all my stupid mistakes and forgetfulness that 
> seems too good to be true. I expect there is a better, more elegant, or 
> more Pythonic way to do what I did so please feel free to share on the 
> subject.
> 
>  I had a problem where if I pressed a button while the LEDs were already 
> flashing the colour of that button it would block a new colour from 
> starting when I pressed a new button. So if the LED strip was red and I 
> pressed the red button again nothing would happen when I pressed the blue 
> or yellow button. Similar problem for the other two buttons.
> 
>  So inside my callbacks I added this code:
> 
>if colour == 1:
> pass
> elif colour == 2 or 3:
> colour = 1
> 
> 
>  Now it seems OK from my limited testing.
> 
> 
>  Here is the code that has buttons and colours working and includes my 
> bug fix:
> 
> 
> 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):
> global colour
> if colour == 1:
> pass
> elif colour == 2 or 3:
> colour = 1
> while colour == 1:
> led.pulse(red=255, green=0, blue=0, repeats=1, duration=2000, 
> steps=50)
> def yellow_button(channel):
> global colour
> if colour == 2:
> pass
> elif colour == 1 or 3:
> colour = 2
> while colour == 2:
> led.pulse(red=255, green=96, blue=0, repeats=1, 
> duration=2000, steps=50)
> def blue_button(channel):
> global colour
> if colour == 3:
> pass
> elif colour == 1 or 2:
> 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()
> led.set_color(name="black")
> atexit.register(exit_handler)
> GPIO.cleanup()
> 
> 
> 
>  I think I am OK GPIO wise now, although always happy to improve the code 
> and in the long term I want to do so.
> 
>  Will start new threads for more straight forward Python questions like 
> help with saving a log of the results, timing, etc.
> 
>  Thanks for your help, everyone.

I just noticed another potential issue with your code. You added "while" loops 
to your button functions, you should adjust the code so that the "led.pulse" 
call is in the main loop.

For example, at the beginning add something like:

pulse_settings = []


Change your button functions to change this new variable when the button is 
pressed:


def red_button(channel):
global colour, pulse_settings
if colour != 1:
colour = 1
pulse_settings = (red=255, green=0, blue=0, repeats=1, duration=2000, 
steps=50)


Then change your main "while" loop:

while True:
if colour == 1:
time_red += 1
elif colour == 2:
time_yellow += 1
elif colour == 3:
time_blue += 1
led.pulse(pulse_settings)
time.sleep(pulse_settings[4]) # sets the delay to the duration of the pulse 
effect


These are merely suggestions, again I do not have access to my RPi to test, 
your mileage may vary.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: RPI.GPIO Help

2015-09-10 Thread John McKenzie

 MRAB:

 Thanks for replying. I got so hyper focused on solving my hardware 
problems, and excited that I did, that I forgot details from previous 
comments. Thanks for your post.

 Off to make things global...
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: RPI.GPIO Help

2015-09-09 Thread John McKenzie

 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.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: RPI.GPIO Help

2015-09-09 Thread MRAB

On 2015-09-09 20:03, John McKenzie wrote:


  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.


It's the same problem as before, and it has the same answer.

In red_button, yellow_button and blue_button, 'colour' is a local
variable. It's set to a value and that value is never changed in the
loop, so it loops forever.

You should declare 'colour' to be global in all 3 functions, e.g.:

def red_button(channel):
global colour

--
https://mail.python.org/mailman/listinfo/python-list


Re: RPI.GPIO Help

2015-09-02 Thread John McKenzie

 Hakugin: Thanks for the correction. Someone elsewhere showed me example 
code that was very close to yours, with that being the main difference. 
His gave an error message that red_button was undefined so I moved the 
code block below the callbacks. After that it ran without producing 
errors but it was unresponsive. Will try your updated version tomorrow 
and will keep fiddling with his.

 Thank you all for warning me about debouncing. The RPi.GPIO library has 
built in debouncing. You can even adjust the timing of it.

 Turns out I got one thing right. Someone who knows better than I 
explained that GPIO.Cleanup() does go at the bottom and outside the def 
exit_handler() code block like I had it originally.

 Johannes: Thanks for sharing that. I will take a look at it when I have 
more time. Must head to the hospital for my treatment in a few minutes.

>Are you still calling GPIO.add_event_detect in a while loop?

 MRAB, apparently yes. Hakugin noticed and update his example code.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: RPI.GPIO Help

2015-09-01 Thread Johannes Bauer
On 31.08.2015 19:41, John McKenzie wrote:

>  Still checking here and am discussing all this in the Raspberry pi 
> newsgroup. Thanks to the several people who mentioned it.
> 
>  Again, still listening here if anyone has any more to add.

I've had the problem to use interrupt-driven GPIOs on the Pi about two
years back. Here's how I solved it:

http://pastebin.com/gdJaJByU

To explain the message you're getting: If you want to handle GPIOs in
the most resource-efficient way, you use interrupt-driven handling.
Interrupts for GPIOs can be configured to be off, level-triggered or
edge-triggered. For edge-triggering I'm also pretty sure that the type
of edge (rising, falling, both) can be specified.

IIRC (and I might not, been quite some time), these interrupts are
bundled together in GPIO ports ("channels"). All GPIOs in one channel
need to have the same configuration. You cannot have conflicing
configuration between two pins which belong to the same GPIO (and
apparently, your framework is trying to do it).

The code I posted does it all by hand (and it's not really hard, as you
can see). I used input and output functionality and do the interrupt
configuration myself (this works through the /proc filesystem on the Pi).

Hope this helps,
Cheers,
Johannes

-- 
>> Wo hattest Du das Beben nochmal GENAU vorhergesagt?
> Zumindest nicht öffentlich!
Ah, der neueste und bis heute genialste Streich unsere großen
Kosmologen: Die Geheim-Vorhersage.
 - Karl Kaos über Rüdiger Thomas in dsa 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: RPI.GPIO Help

2015-08-31 Thread John McKenzie

 Dennis, Hakugin, I tried your scripts and had to alter a typo here or 
there, but once the basic errors disappeared I had the same error 
message. "Conflicting edge detection already enabled for this GPIO 
channel".

 As much as I despise web based bulletin board systems I registered on 
the Raspberry Pi website to ask for help as well.

 Appreciate the effort you both put in those scripts. Will keep working 
with them and the general points you made.

 Still checking here and am discussing all this in the Raspberry pi 
newsgroup. Thanks to the several people who mentioned it.

 Again, still listening here if anyone has any more to add.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: RPI.GPIO Help

2015-08-31 Thread MRAB

On 2015-08-31 18:41, John McKenzie wrote:


  Dennis, Hakugin, I tried your scripts and had to alter a typo here or
there, but once the basic errors disappeared I had the same error
message. "Conflicting edge detection already enabled for this GPIO
channel".


Are you still calling GPIO.add_event_detect in a while loop?


  As much as I despise web based bulletin board systems I registered on
the Raspberry Pi website to ask for help as well.

  Appreciate the effort you both put in those scripts. Will keep working
with them and the general points you made.

  Still checking here and am discussing all this in the Raspberry pi
newsgroup. Thanks to the several people who mentioned it.

  Again, still listening here if anyone has any more to add.



--
https://mail.python.org/mailman/listinfo/python-list


Re: RPI.GPIO Help

2015-08-31 Thread hakugin . gin
On Monday, August 31, 2015 at 1:42:16 PM UTC-4, John McKenzie wrote:
> Dennis, Hakugin, I tried your scripts and had to alter a typo here or 
> there, but once the basic errors disappeared I had the same error 
> message. "Conflicting edge detection already enabled for this GPIO 
> channel".
> 
>  As much as I despise web based bulletin board systems I registered on 
> the Raspberry Pi website to ask for help as well.
> 
>  Appreciate the effort you both put in those scripts. Will keep working 
> with them and the general points you made.
> 
>  Still checking here and am discussing all this in the Raspberry pi 
> newsgroup. Thanks to the several people who mentioned it.
> 
>  Again, still listening here if anyone has any more to add.

I apologize, I did make a mistake with the example I provided and I just 
realized it. Change the code block:

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 

to:

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)

while True: 
if colour == 1: 
time_red += 1 
elif colour == 2: 
time_yellow += 1 
elif colour == 3: 
time_blue += 1


You are getting the error because the script is attempting to perform a 
"add_event_detect" on the same pins each time the "while" loop iterates. By 
moving it above the loop the error should stop occuring.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: RPI.GPIO Help

2015-08-31 Thread Tim Daneliuk
On 08/16/2015 02:40 PM, John McKenzie wrote:
> 
>  Hello, all. I am hoping some people here are familiar with the RPi.GPIO 
> python module for the Raspberry Pi.


I am not familiar with the module, but I am quite familiar with dealing
with hardware interfacing, mostly in assembler.

One thing you need to ensure is that either the module or your
code implents software debounce.  When a button is pressed or switched moved,
there is mechanical bounce for a some period of time immediately thereafter.
In the time right after the switch/button is pressed, the mechanical closure
"bounces" between "on" and "off" for a little while, until it settles down
to the position you've selected.

The idea is to use the initial change of state to initiate the button handling 
logic but
wait until the switch/button settles down to it's final (and therefore, 
reliable) state. 

One algorithm to do this is to wait a fixed amount of time after the initial 
event - say,
100ms - before reading the switch.  The other way to do is to read the 
button/switch state
ever few milliseconds and only accept the input if it has not changed in, say,
5 measurements.

HTH,

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: RPI.GPIO Help

2015-08-28 Thread John McKenzie

 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.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: RPI.GPIO Help

2015-08-28 Thread hakugin . gin
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 

Re: RPI.GPIO Help

2015-08-20 Thread John McKenzie

 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()



 Thanks.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: RPI.GPIO Help

2015-08-20 Thread MRAB

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).

--
https://mail.python.org/mailman/listinfo/python-list


Re: RPI.GPIO Help

2015-08-20 Thread alister
On Thu, 20 Aug 2015 16:45:53 +0100, MRAB wrote:

 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).

you may also find it more useful to post this to Comp.sys.raspberry_pi
the python skills may not be quite as in depth as here but they will know 
more about the raspberry hardware



-- 
The easiest way to get the root password is to become system admin.
-- Unknown source
-- 
https://mail.python.org/mailman/listinfo/python-list


RPI.GPIO Help

2015-08-16 Thread John McKenzie

 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.
   
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: RPI.GPIO Help

2015-08-16 Thread MRAB

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).

--
https://mail.python.org/mailman/listinfo/python-list