Jump to content

Recommended Posts

Posted

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.

Posted

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

Posted

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

Posted (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 by SimpleSi
Posted (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 by SimpleSi
Posted

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...