Please, be kind! Here is another topic "Netduino Timers", but there is no usefull information for me. I need to measure a friquency of incoming impulses, so i decided to use timers and interrupts. I want to start a timer with first interrupt from a sensor and then stop this timer with a second interrupt. In TimerCallback I wanted to add 1 with every timer tick. But do not understand how to start and stop a timer with the same event! Help me please.. At fist i want to test it with button interrupt, but it does not work correctly( The second interrupt does not work and timer ticks constantly
My code:
namespace Timer_testing_LED{ public class Program { private static Timer oneHerzTimer = null; private static OutputPort LED = null; private static InterruptPort button = null; public static int i = 0; public static int j = 0; public static void Main() { LED = new OutputPort(Pins.ONBOARD_LED, false); button = new InterruptPort(Pins.ONBOARD_SW1, false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLevelHigh); button.OnInterrupt += new NativeEventHandler(button_Interrupt_Event); //timerEvent = new TimerCallback(TimerInterrupt); while (j < 2) { if (j > 0) { oneHerzTimer = new Timer(new TimerCallback((object data) => { i++; LED.Write(!LED.Read()); }) , null, 100, 500); Thread.Sleep(Timeout.Infinite); } } } private static void TimerInterrupt(object state) { i++; //LED.Write(!LED.Read()); } private static void button_Interrupt_Event(uint data1, uint data2, System.DateTime time) { if (j > 0) { oneHerzTimer.Dispose(); oneHerzTimer = null; for (int k = 0; k <= (i / 2); k++) { LED.Write(true); Thread.Sleep(50); LED.Write(false); Thread.Sleep(350); } } j = 2; } }}