I am new to the Netduino and I would like to share my first projet: a class for a rotary encoder (e.g. from Alps STEC 12E8). It might be useful for a radio-volume controls or the selection of menu entries. The example adapts the blinking LED example and changes the blink time by the encoder.
All you have to do is to connect the two encoder lines and the switch with three Netduino input ports and create a "RotaryEncoder" object. Register an event handler for the switch or the rotation and you are ready.
rot = new RotaryEncoder(Pins.GPIO_PIN_D13, Pins.GPIO_PIN_D11, Pins.GPIO_PIN_D12);
rot.setPosition(100);
rot.setRange(1, 1000);
rot.setFullCycleMode();
rot.OnMove += button_OnMove;
OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
while (true)
{
led.Write(true); // turn on the LED
Thread.Sleep(i); // sleep
led.Write(false); // turn off the LED
Thread.Sleep(i); // sleep
}
Of course you need an action method to do any useful work
static void button_OnMove(uint data1, uint data2, DateTime time) { i = (int)data1; Debug.Print("Tick " + i + " - Direction " + data2); }
The complete source code and an example is attached to this entry. I hope it will be useful for someone. I will expand it in the future with a progressive count (fast spinning will count even faster - in steps by 2, 5 or 10, slow spinning counts with the normal precision).
Bug fixes or corrections will be welcome.