Re: RPI.GPIO Help
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
Re: RPI.GPIO Help
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
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
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
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: GoPiGo script
On Monday, November 2, 2015 at 8:29:26 AM UTC-5, input/ld...@casema.nl wrote: > I tried to use def loop(): now for to restart the script. > but its only restart "fwd()" print ("forward 1x") and then stop. > It does not look further for the if function. > Is there another way to restart this script ? > I also tried with (while True:) but that does nothing. > > Thanks Try this: (hopefully the indentation is preserved) from gopigo import * from time import sleep # Boolean variable for the "while" loop KEEP_GOING = True enable_servo() mindist = 80 servo(90) set_right_speed(150) set_left_speed(105) def MainLoop(): # A check statement can be added to set KEEP_GOING to False # and allow the function to end while KEEP_GOING: mindist = 90 server(90) fwd() print("forward1x") if mindist > us_dist(15): bwd() print("backward1x",us_dist(15) sleep(2) left_rot() print("left rot",us_dist(15)) sleep(3) stop() if mindist < us_dist(15): fwd() print("forward2x",us_dist(15)) time.sleep(2) stop() # This is a simple check to determine if the script was run by itself # or if it was imported by another script. If it was imported it will # fail this check and not run the code but will allow access to the # function defined above. if __name__ == '__main__': MainLoop() - Mike -- https://mail.python.org/mailman/listinfo/python-list
Re: GoPiGo script
On Monday, November 2, 2015 at 8:45:35 AM UTC-5, hakug...@gmail.com wrote: > On Monday, November 2, 2015 at 8:29:26 AM UTC-5, input/ld...@casema.nl wrote: > > I tried to use def loop(): now for to restart the script. > > but its only restart "fwd()" print ("forward 1x") and then stop. > > It does not look further for the if function. > > Is there another way to restart this script ? > > I also tried with (while True:) but that does nothing. > > > > Thanks > > > Ignore that last suggestion... between auto correct and other things there are some issues with it. Try this instead: from gopigo import * from time import sleep # Boolean variable for the "while" loop KEEP_GOING = True enable_servo() mindist = 80 servo(90) set_right_speed(150) set_left_speed(105) def MainLoop(): # A check statement can be added to set KEEP_GOING to False # and allow the function to end while KEEP_GOING: fwd() print("forward1x") if mindist > us_dist(15): bwd() print("backward1x",us_dist(15) sleep(2) left_rot() print("left rot",us_dist(15)) sleep(3) stop() if mindist < us_dist(15): fwd() print("forward2x",us_dist(15)) time.sleep(2) stop() # This is a simple check to determine if the script was run by itself # or if it was imported by another script. If it was imported it will # fail this check and not run the code but will allow access to the # function defined above. if __name__ == '__main__': MainLoop() - Mike -- https://mail.python.org/mailman/listinfo/python-list
Re: GoPiGo script
On Monday, November 2, 2015 at 9:28:35 AM UTC-5, input/ld...@casema.nl wrote: > He mike, > > Thank you or making this script. > Only I get errors for sleep. > I also tried to change it to time.sleep() but that also gives errors. > > File "test05.py", line 23 > sleep(2) > ^ > SyntaxError: invalid syntax > --- > And this is why I shouldn't code while tired hahaha... I reviewed the code I submitted and found a couple of other errors. If you are changing the "sleep()" lines to "time.sleep()" you will need to change the line "from time import sleep" to "import time", otherwise you will most likely encounter something like: Traceback (most recent call last): File "", line 1, in NameError: name 'time' is not defined Since you are just beginning with Python I would suggest reading various entry level books such as Python 101 by Mike Driscoll, which I have read and own. I am not Mike Driscoll, but his book and blog, http://www.blog.pythonlibrary.org/ have been extremely helpful. There are MANY resources available for learning the Python programming language out there, including https://wiki.python.org/moin/BeginnersGuide -- https://mail.python.org/mailman/listinfo/python-list
Re: GoPiGo script
On Monday, November 2, 2015 at 10:21:46 AM UTC-5, MRAB wrote: > On 2015-11-02 14:28, input/ldom...@casema.nl wrote: > > He mike, > > > > Thank you or making this script. > > Only I get errors for sleep. > > I also tried to change it to time.sleep() but that also gives errors. > > > > File "test05.py", line 23 > > sleep(2) > > ^ > > SyntaxError: invalid syntax > > --- > > > The previous line is missing a ")"; it should end with two of them, not > just one. > Yep, I shouldn't code while tired... I also forgot to change line 31 from "time.sleep(2)" to "sleep(2)". > > In reply to "haku...@gmail.com" who wrote the following: > > > >> On Monday, November 2, 2015 at 8:45:35 AM UTC-5, hakug...@gmail.com wrote: > >> > On Monday, November 2, 2015 at 8:29:26 AM UTC-5, input/ld...@casema.nl > >> > wrote: > >> > > I tried to use def loop(): now for to restart the script. > >> > > but its only restart "fwd()" print ("forward 1x") and then stop. > >> > > It does not look further for the if function. > >> > > Is there another way to restart this script ? > >> > > I also tried with (while True:) but that does nothing. > >> > > > >> > > Thanks > >> > > >> > > >> > > >> > >> Ignore that last suggestion... between auto correct and other things there > > are > >> some issues with it. > >> > >> Try this instead: > >> > >> from gopigo import * > >> from time import sleep > >> > >> # Boolean variable for the "while" loop > >> KEEP_GOING = True > >> > >> enable_servo() > >> mindist = 80 > >> servo(90) > >> > >> set_right_speed(150) > >> set_left_speed(105) > >> > >> def MainLoop(): > >> # A check statement can be added to set KEEP_GOING to False > >> # and allow the function to end > >> while KEEP_GOING: > >> fwd() > >> print("forward1x") > >> if mindist > us_dist(15): > >> bwd() > >> print("backward1x",us_dist(15) > >> sleep(2) > >> left_rot() > >> print("left rot",us_dist(15)) > >> sleep(3) > >> stop() > >> if mindist < us_dist(15): > >> fwd() > >> print("forward2x",us_dist(15)) > >> time.sleep(2) > >> stop() > >> > >> # This is a simple check to determine if the script was run by itself > >> # or if it was imported by another script. If it was imported it will > >> # fail this check and not run the code but will allow access to the > >> # function defined above. > >> if __name__ == '__main__': > >> MainLoop() > >> -- https://mail.python.org/mailman/listinfo/python-list