After getting the blinking LED, the next thing to do is experiment with a switch. Besides an on-board LED, the Netduino also comes with an on-board switch, so we can work without extra components.
There are two ways to use a digital input-port. As an InputPort, which requires us to constantly check for an input-value ourselves, or as an InterruptPort, which will provide an event when the value changes. I will show code for each.
For this example, the program will turn the LED on when the button is pressed. When using the on-board switch, the program may look like this:
public class Program
{
public static void Main()
{
// initialize the onboard LED
OutputPort ledPort = new OutputPort(Pins.ONBOARD_LED, false);
// initialize the onboard switch
InputPort switchPort = new InputPort(Pins.ONBOARD_SW1, false,
Port.ResistorMode.Disabled);
// do forever...
while (true)
{
// turn the LED on
//, if the switch is pressed (== false, because switch is inverted)
ledPort.Write(switchPort.Read() == false);
}
}
}
After the output port for the LED is created as in the blink example, the input port is created for the on-board switch. In this case the glitch-filter is turned off, since bouncing of the switch contacts is not an issue here. The pull-up resistor inside the port of the MCU is disabled, since the on-board switch already has a pull-up resistor. More on this later. Important note: since the switch has a pull-up resistor, the value on the port is low/false when the switch is pressed; the value is high/true when it is released.
Using an InputPort, as shown above, the code is required to regularly read the input port to check if the switch has been pressed and act accordingly. An easier way is to use an InterruptPort, as shown below.
public class Program
{
// initialize the onboard LED
private static OutputPort ledPort = new OutputPort(Pins.ONBOARD_LED, false);
public static void Main()
{
// initialize the onboard switch
InterruptPort switchPort = new InterruptPort(Pins.ONBOARD_SW1, false,
Port.ResistorMode.Disabled,
Port.InterruptMode.InterruptEdgeBoth);
// add an event-handler for the switch
switchPort.OnInterrupt += new NativeEventHandler(switchPort_OnInterrupt);
// wait forever...
Thread.Sleep(Timeout.Infinite);
}
///
/// Event handler for the onboard switch.
///
///The port for which the event occurs.
///The state of the switch.
///Time of the event.
private static void switchPort_OnInterrupt(uint port, uint data, DateTime time)
{
// turn the LED on, if the button is pressed (== 0, because switch is inverted)
ledPort.Write(data == 0);
}
}
In this version, an InterruptPort is created for the on-board switch, again with the glitch filter and the internal resistor disabled. The interrupt mode is set to both edges, meaning that an event is raised when the value on the port changes from low to high and when it changes from high to low. This allows us to respond when the switch is pressed and when it is released. In both cases, the OnInterrupt event is raised, for which a handler is registered. The handler turns the LED on or off depending on whether the switch is pressed or released.
In this example, the program enters sleep-mode as soon as the initialization of the ports is done. When an event is raised, the program wakes up, the handler is called and goes back to sleep again.
If you want to use your own switch, there are several ways to connect a switch to the Netduino (or a MCU in general), but a common way is shown in the schematic below.

As with the LED, you need to combine the switch with a resistor. Not only to restrict the current, but also to prevent a shortcut when the button is pressed. As with the on-board switch, because a pull-up resistor is used, the value at the port is high when the button is not pressed, and the value is low while the button is pressed.
You can connect the switch & resistor to any of the available ports A0-5 or D0-D13 , as long as you change the first argument of the InputPort or InterruptPort constructor to correspond with the selected port. E.g. when connected to the D0 port, the port should be constructed as follows:
InterruptPort switchPort = new InterruptPort(Pins.GPIO_PIN_D0, false,
Port.ResistorMode.Disabled,
Port.InterruptMode.InterruptEdgeBoth);
Next: getting started with transistors.