Overview
This example demonstrates how to work with push buttons and an Netduino board. In this tutorial a push button is attached to the Netduino board and the result is outputted to an LED.
Code Sample
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
namespace PushButtonExample
{
public class Program
{
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);
}
}
}
}
Details
There are three main objects created within this code sample.
1. LED – the output port used to indicate when the button is pressed
2. BUTTON – the push button itself that will be pressed
3. BUTTONSTATE – a boolean variable used to indicate the current state of the button
The code simply loops forever constantly checking the status of the button. If the button is pressed the LED is blinked.
Video Tutorial
The following video is brought to you buy Secret Labs. They have created a nifty video which shows you all the details and lays it out in great detail.