Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Long button press sample


Best Answer JerseyTechGuy, 03 May 2013 - 06:48 PM

Sorry for taking so long.  Had some project interviews this morning.  Just getting back to my home office.

 

You'll likely benefit from using Stefan's .NET Micro Framework Toolbox on CodePlex. In the Samples you'll want to take a look at the Auto-Repeat Button Example.  I am using this on Pandora's Box. Basically I have a menu so I use up and down buttons and an enter button to run different modes.  When I start my scan mode the only way to end it is to hold the up button for 3 seconds and it will stop scan mode then reset Pandora's Box.

 

Inside the button state change event handler the case of "AutoRepeatInputPort.AutoRepeatState.Tick" handles timed button presses. The documentation on the CodePlex site is pretty good.  You'll see the following properties help you set your timings.

 

InitialDelay - Get/set the initial delay before the auto-repeat starts.
AutoRepeatPeriod - Get/set the interval period of the auto-repeat.

 

Hope this helps.

Go to the full post


  • Please log in to reply
12 replies to this topic

#1 cce1911

cce1911

    Advanced Member

  • Members
  • PipPipPip
  • 71 posts
  • LocationGeorgia, USA

Posted 03 May 2013 - 02:43 AM

Can anyone point me to some sample code that shows how to setup a button to do two different things depending on how the user presses a button?

 

For example, if the user simply clicks the button, it will cause an LED to light up for 2 seconds, but if the user holds the button down (for more than a second) it will cause the LED to blink rapidly until the button is released.

 

I've searched high and low and I've found some sample code for the MF, but it was not specific to Netduino and I could not figure it out. I understand that you will need a timer and button down/up event handlers,  but I'm new to C# and just can't get the concept straight in my head.

-Capel



#2 JerseyTechGuy

JerseyTechGuy

    Advanced Member

  • Members
  • PipPipPip
  • 870 posts

Posted 03 May 2013 - 12:22 PM

Sure, ill post some code when I'm back in my office in a couple hours. I do this on one project where you have to hold the button to stop the loop. Pressing the button starts the loop.

#3 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 03 May 2013 - 12:37 PM

Basically what you do is connect a digital pin of your Netduino to ground via the switch (normally open).

 

In your code, you define an interrupt port with pull-up resistor and configured to fire on both low and high edges.

 

In the interrupt handler, you check parameter data2 - if it is zero the button was depressed and if it is non-zero, the button was released.

 

Each time the button is pressed (data2=0), you note the current time using DateTime.Now. When the button is later released (data2 != 0), you calculate the time dIfference that is equal to the duration for which the button was held down.

 

If the time is short (say less than 500 ms or so), then the button was pressed briefly but if the time is more than that, the button was held down for a long(er) time.

 

I don't have access to Visual Studio right now so you have to excuse me for not being able to supply sample code that will compile and settle for this pseudo code from the top of my head:

void Main(){    InterruptPort button = new InterruptPort(Pins.GPIO2, pullup, edge.both);    button.OnInterrupt += buttonHandler;    Thread.Sleep(Timeout.Infinite);}static DateTime t;void buttonHandler(DateTime time, int data1, int data2){   if(data2 == 0)      t = DateTime.Now;   else if((DateTime.Now - t).Milliseconds < 500)      Debug.Print("Short: " + t.ToString())   else      Debug.Print("Long: " + t.ToString())}}

Good luck!

 

EDIT: Sorry Dave, accidentally overlapping posts.



#4 cce1911

cce1911

    Advanced Member

  • Members
  • PipPipPip
  • 71 posts
  • LocationGeorgia, USA

Posted 03 May 2013 - 02:19 PM

hanzibal,

I don't see how you can do this without a timer. What I'm doing is collecting configuration data and displaying it on a LCD. So, if you click the up button, the value increases by 0.1 and if you click the down button the value decreases by 0.1. What I want to happen is if the user holds the either button (up or down) pressed for a second or two, the value will start to automatically increment. For example, if the value is initially 0.0 and I want to increment it to 10.0, I don't want the user to have to click the button 100 times. I want the user to simply hold the button down and let the software increment the value instead. An advanced version might accelerate the increments, but to start with I just want some sample code.

 

I've found the following links, but neither is complete enough to get me going.

http://blogs.msdn.co...th-buttons.aspx

http://forums.netdui...press#entry8689



#5 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 03 May 2013 - 03:52 PM

Well, there's a timer running in the background so to speak that updates system time available through DateTime.Now. The code example shows how you can detect short and long button presses respectively. Naturally, now you have to do whatever you want to do other than Debug.Print. I won't write all the code for you ;-) Instead of Debug.Print(), you could kick off a background thread or a timer to do the periodic thing you want to do once the interrupt routine above has detected ether a short or long button press. If I remember correctly, data1 contains the number of the pin causing the interrupt so you can actually use the same buttonHandler for both the up and down button and have that determine to move up or down, fast or slow.

#6 JerseyTechGuy

JerseyTechGuy

    Advanced Member

  • Members
  • PipPipPip
  • 870 posts

Posted 03 May 2013 - 06:48 PM   Best Answer

Sorry for taking so long.  Had some project interviews this morning.  Just getting back to my home office.

 

You'll likely benefit from using Stefan's .NET Micro Framework Toolbox on CodePlex. In the Samples you'll want to take a look at the Auto-Repeat Button Example.  I am using this on Pandora's Box. Basically I have a menu so I use up and down buttons and an enter button to run different modes.  When I start my scan mode the only way to end it is to hold the up button for 3 seconds and it will stop scan mode then reset Pandora's Box.

 

Inside the button state change event handler the case of "AutoRepeatInputPort.AutoRepeatState.Tick" handles timed button presses. The documentation on the CodePlex site is pretty good.  You'll see the following properties help you set your timings.

 

InitialDelay - Get/set the initial delay before the auto-repeat starts.
AutoRepeatPeriod - Get/set the interval period of the auto-repeat.

 

Hope this helps.



#7 cce1911

cce1911

    Advanced Member

  • Members
  • PipPipPip
  • 71 posts
  • LocationGeorgia, USA

Posted 03 May 2013 - 08:04 PM

Thanks Dave, I'll give this a shot.



#8 JerseyTechGuy

JerseyTechGuy

    Advanced Member

  • Members
  • PipPipPip
  • 870 posts

Posted 03 May 2013 - 08:07 PM

Thanks Dave, I'll give this a shot.

If you run into a problem, post up and I'll try to help.



#9 cce1911

cce1911

    Advanced Member

  • Members
  • PipPipPip
  • 71 posts
  • LocationGeorgia, USA

Posted 03 May 2013 - 08:16 PM

I assume that InitialDelay and AutoRepeatPeriod are in milliseconds?



#10 JerseyTechGuy

JerseyTechGuy

    Advanced Member

  • Members
  • PipPipPip
  • 870 posts

Posted 03 May 2013 - 09:03 PM

I assume that InitialDelay and AutoRepeatPeriod are in milliseconds?

That is correct



#11 cce1911

cce1911

    Advanced Member

  • Members
  • PipPipPip
  • 71 posts
  • LocationGeorgia, USA

Posted 04 May 2013 - 04:13 AM

Dave, your idea worked like a champ!

Thanks,

-Capel



#12 JerseyTechGuy

JerseyTechGuy

    Advanced Member

  • Members
  • PipPipPip
  • 870 posts

Posted 04 May 2013 - 11:30 AM

Dave, your idea worked like a champ! Thanks, -Capel

Glad to hear it.

#13 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 04 May 2013 - 06:30 PM

@cce1911: In retrospect, I realize I misread your post, sorry. Glad it worked out for you!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.