Pushing the Onboard Button

Both Netduino and Netduino Plus have an on onboard pushbutton labeled SW1. You can read the digital value of this pushbutton—true or false—to determine whether it is currently being pushed. Then you can take actions based on that pushbutton’s current state, such as turning an LED on and off or instructing a Netduino-powered robot to start moving its motors.

Note

By default, this pushbutton will reboot your Netduino and restart your current Netduino app. But because of a special configuration in the Netduino’s circuitry, it can alternatively be used as a digital input.

In Visual Studio, create a new project by selecting New Project in the File menu. Select Netduino Application as the template as before, and give your project a name such as “Pushbutton1.” Finally, double-click on Program.cs in the Solution Explorer to begin editing it.

In the main section of the Visual Studio editor, you are again editing Program.cs. Click on the line underneath the text // write your code here.

Now, type the following:

OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);1
InputPort button = 
  new InputPort(Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled);2
bool buttonState = false;3
1

As with the previous example, the first line of code creates an OutputPort so that you can turn the onboard LED on and off.

2

The second line of code creates an InputPort. An InputPort lets you read the voltage level of the pins on the Netduino (or in this case, the voltage coming from the pushbutton). Pins.ONBOARD_SW1 is shorthand that tells the Netduino which pin of the microcontroller to use for input. The second value, false, tells the runtime that you don’t need glitch filtering (if you enable it, Netduino will take multiple readings during a button press to make sure the reading is correct). The final value, Port.ResistorMode.Disabled indicates that Netduino won’t use a built-in resistor to affect incoming signals on the microcontroller’s digital pin.

Note

I explore the resistor mode in greater detail later in Pushing the MakerShield’s Button.

3

The third line of code creates a variable named buttonState. A variable is a way to store and manipulate data in the memory of the Netduino. In this case, the app stores whether or not the Netduino’s pushbutton is currently being pushed in the buttonState variable. The word bool, which precedes the variable, indicates that buttonState will store a Boolean value. Boolean values are values that are true or false, perfect for this application. Finally, = false sets the state of the value to false, by default. It’s generally a good idea to set variables to a default value so that you make your intentions clear to anyone else (including you!) who reads the source code in the future.

Now you’re going to read the state of the pushbutton and turn the LED on and off as the pushbutton is pushed and released. First, create an infinite loop as before. Add the following code to your project:

while (true)
{
}

Then, read the current state of the pushbutton and write out that state to the LED. Between the two sets of curly braces, insert the following two lines of code:

buttonState = button.Read();
led.Write(buttonState);

Your final program’s Main() method should look like this:

public static void Main()
{
    // write your code here
    OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
    InputPort button = 
      new InputPort(Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled);

    bool buttonState = false;

    while (true)
    {
        buttonState = button.Read();
        led.Write(buttonState);
    }
}

Now, you’re almost ready to run the app, press the pushbutton, and use it to control the state of the LED. But first, you need to make sure you’re deploying the project to the Netduino instead of to the built-in emulator. Click on the Project menu and select your project’s properties. Then click on the .NET Micro Framework category on the left side. Change the Transport from Emulator to USB, and then make sure that the Device selection box shows your Netduino.

Now run your project. Press the Start Debugging button in the toolbar at the top of the screen or press F5.

After a few seconds, your Netduino app will be running. Once the blue LED turns off, you know that your board has booted. Press the pushbutton, and the blue LED will turn on. Release the pushbutton, and the blue LED will turn back off. Congratulations!

Note

The Netduino’s pushbutton uses a special wiring configuration to enable it to act as both a reset button and a digital input. It also technically sends a low voltage when pushed even though your code will see a value of true. These values are reversed for the pushbutton inside the Netduino firmware; they are logical values instead of physical values.

Early versions of the Netduino firmware did not reverse the physical values. If your Netduino’s LED exhibits the reverse behavior when you run this sample, you can either update your firmware or change the code from:

buttonState = button.Read()

to:

buttonState = !button.Read()

The exclamation mark before the word button reverses the result.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.72.15