This is the first in a new series of posts: Thursday Featured Modules.  Each week we'll post about an interesting Gadgeteer module, including how to use it and where to find it.

The Mighty Button 

Today we'll cover one of the most fundamental modules in many devices you already use today: the button.  It's universally understood by users and easy to program.  There are currently two button modules available for Gadgeteer, one from GHI Electronics and one from Sytech.  Each offers a little something extra: an LED you can use as a status indicator or to visually register button presses.

                                                       

 

How to Add a Button to Your Gadgeteer Project 

Adding a button to your Gadgeteer project is really easy.  In Visual Studio or Visual C# Express, create a new Gadgeteer project.  (This walkthrough assumes you have the necessary Gadgeteer software installed on your computer.  If you still need to do that, go here first.)

Drag a button from the toolbox onto the design surface and connect it to a compatible socket.

 

 

Now, go to Program.cs and use Intellisense to see what the Button module can do.  The API is quite similar for the GHI and Sytech buttons, so this walkthrough works well for both.

 

 

Detecting Button Presses

The Button exposes two events:  ButtonPressed and ButtonReleased.  By listening to both, your gadget can do something when the button is pressed and again when the user releases it.  Here's a quick code snippet that shows how to toggle the Button's LED each time the button is pressed:

public partial class Program
    {
        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            button.ButtonPressed += new Button.ButtonEventHandler(button_ButtonPressed);
 
            // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
            Debug.Print("Program Started");
        }
 
        void button_ButtonPressed(Button sender, Button.ButtonState state)
        {
            // Toggle the LED when the button is pressed.
            button.ToggleLED();
        }
    }

Using Button.LEDMode Property

You can use the Button's LEDMode property to make the LED automatically respond to button presses.  You can set the LED to respond in several ways:

 

 

This snippet shows how to make the LED's button light up while the button is held down:

public partial class Program
    {
        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            button.LEDMode = Button.LEDModes.OnWhilePressed;
 
            // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
            Debug.Print("Program Started");
        }
 
    }

That's it!  Click away :)

Questions?

Ask them in the Gadgeteer forums

Want up to date information on Gadgeteer-compatible button modules?  Search the Gadgeteer Product Showcase for buttons to see individual modules and kit options.