Skip to content

Instantly share code, notes, and snippets.

@scottheckel
Created November 21, 2012 04:22
Show Gist options
  • Save scottheckel/4123006 to your computer and use it in GitHub Desktop.
Save scottheckel/4123006 to your computer and use it in GitHub Desktop.
Simple Netduino Project – Toggle LED
using Microsoft.SPOT.Hardware;
using Netduino = SecretLabs.NETMF.Hardware.Netduino;
namespace ToggleLED
{
/// <summary>
/// Toggle the Netduino LED on/off based on a button press.
/// </summary>
public class Program
{
/// <summary>Main</summary>
public static void Main()
{
bool isOn = false; // initial state of LED
bool lastButtonState = true; // assume button initially not pressed
// Create our LED/Button objects
OutputPort led = new OutputPort(Netduino.Pins.ONBOARD_LED, isOn);
InputPort button = new InputPort(Netduino.Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled);
// Loop forever reading the button and turning on/off the LED
while (true)
{
bool buttonState = button.Read();
// Toggle light only if the Button is pushed
if (buttonState != lastButtonState && !buttonState)
{
isOn = !isOn;
led.Write(isOn);
}
// Store current state as last state
lastButtonState = buttonState;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment