I’m still waiting for some bits and pieces to arrive so I can actually connect something up to the Netduino. Perhaps I’ll use a breadboard so I don’t have to do any soldering yet. In the meantime I’ll have a look at the only other onboard component (unless you have a Netduino Plus) – the button.
The button in the middle of the board (Pins.ONBOARD_SW1) is just another port that you can read using the .Net MF libraries. This simple example loops and reads the status of the button. If it’s up it returns true, down returns false. Running the program and pressing the button just toggles the LED on and off.
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
namespace Netduino.TestButton
{
public class Program
{
public static void Main()
{
// write your code here
var led=new OutputPort(Pins.ONBOARD_LED, false);
var button = new InputPort(Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled);
while (true)
{
if (!button.Read())
{
led.Write(!led.Read());
Thread.Sleep(300);
}
}
}
}
}
There is another way to check button state. InterruptPort is an event driven class that fires when a port is in a certain state. This is better because it won’t tie up the processor. The changes in levels are fired as events at the leading and bottom edge (Imagine the output from the button port as a graph with y axis at 0 going to 1 when pressed and back to 0 when released). The constructor has the following signature:
public InterruptPort(Cpu.Pin portId, bool glitchFilter, Port.ResistorMode resistor, Port.InterruptMode interrupt);
Glitch filter tells the controller to ignore the button unless it is pressed down for a determinate length of time. This avoids spikes that would otherwise cause erroneous events.
For the onboard button we can disable the resistor mode and for interrupt mode we are interested in both edges (high=push down and low=released). Pushing the button turns on the LED
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
namespace Netduino.TestInterrupt
{
public class Program
{
static OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
public static void Main()
{
// write your code here
InterruptPort button = new InterruptPort(Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth);
button.OnInterrupt += new NativeEventHandler(button_OnInterrupt);
Thread.Sleep(-1);
}
static void button_OnInterrupt(uint data1, uint data2, DateTime time)
{
led.Write(data2==0);
}
}
}
It would interesting to knock up a little app that recorded a pattern of button presses. After recording, it loops, waiting for you to press the button in the same sequence. If you do, it will light up the LED. You could imagine this connected to some sensor measuring a secret knock-knock password or a keyboard with which you play some musical notes. If you get it right, it unlocks something like a box for instance. It might also be a nice way for sending morse code. There’s quite a few people that have worked on morse output but no readers as far as I know. It’s probably a bit tricky to get the timings right.
Thanks! For helping me get started. Soooo coool.