Renfield-64 Posted May 13, 2015 Posted May 13, 2015 Hi We are using python to program the pibrella and have been using the push button to make one of the LEDs come on. Does anyone know the code to make a different LED come on when the button is pressed twice or held down? This is a function commonly used in electronics to make a push button have more than one function but I'm not sure how to code it.
SimpleSi Posted May 15, 2015 Posted May 15, 2015 to detect double click wait for button press but just remember time when it happens wait for say 0.5 sec and see if its pressed again during that time You now know whether it was clicked once or double clicked If it is still down at end of 0.5 sec then it was held down So that should give you 3 events from the one button Simon
Renfield-64 Posted May 17, 2015 Author Posted May 17, 2015 Thanks. Would you know what the code would be for this? Sorry, I am quite new to python.
SimpleSi Posted May 17, 2015 Posted May 17, 2015 I'm not one for just pasting working code (as its all about the learning with this stuff) but how about you publish your existing code and I'll suggest the mods needed for it based on what you;ve got so far? Simon
Renfield-64 Posted May 18, 2015 Author Posted May 18, 2015 (edited) Ok that sounds sensible. Here is what I have so far: import pibrella import time while True: if pibrella.button.read() == 1: pibrella.light.red.on() time.sleep(2) pibrella.light.red.off() I hope the formatting shows correctly on this post, I have put the right indents etc. Thanks, Edited May 19, 2015 by SimpleSi
SimpleSi Posted May 19, 2015 Posted May 19, 2015 (edited) use code and /code with [] to display code - it preserves all whitespacing See what you think of this then import pibrella import time while True: if pibrella.button.read() == 1: #get time when button pressed starttime = time.clock() # wait until button released while pibrella.button.read() == 1: endtime = time.clock() # check if button held down for less than 0.5 sec if endtime-starttime < 0.5: pibrella.light.red.on() time.sleep(2) pibrella.light.red.off() else: pibrella.light.yellow.on() time.sleep(2) pibrella.light.yellow.off() Edited May 19, 2015 by SimpleSi
Renfield-64 Posted May 19, 2015 Author Posted May 19, 2015 Thank you so much . It worked just fine. I promise I will give you credit for this when I show my students Best regards, Renfield
SimpleSi Posted May 19, 2015 Posted May 19, 2015 Hopefully they'll be able to adapt it to get the green light to show on double click Simon PS Its a really good idea BTW - I'm going to use it in my primary Scratch lessons as an extension activity
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now