I am working on a system where i need to detect the speed of an object as it passes by. It will have 10 microswitchs and i would like to know the time difference between each switch being hit. Whats the best way to go about this? Oh yea i gotta be accurate to the millisecond.



You use InterruptPort object and when you receive the interrupt you get a time-stamp of when the edge was detected. Even if you receive the interrupt after few milliseconds, the time is latched when the interrupt actually happened so it is accurate to the millisecond.

Sorry, I am a little new to C# and .net, how do i get the time stamp?



Baby steps…you will get to everything very quickly. Start reading the free ebook and then you will get to InterruptPort. The time is passed to the interrupt routine.

If this is the very first thing you are doing then you should first try to blink some LED and read a button…etc. then jump into more fun things :slight_smile:

im doing pretty good, i have the 16x relay board controlling a compressor, pneumatic solenoids, i have the lcd screen displaying pressure from a board mount pressure sensor.

all i have left is this and getting the infrared sensor going.

never mind i think i got it,


thanks for your help

DateTime.Now.MilliSeconds will just give you some random time delay after the interrupt occured, you need to use the timestamp passed to the interrupt event handler to get the actual time the sensor was activated.

i used this just as a test

private static int time1priv = 0;

        public static int time1
            get { return time1priv;}
            set { time1priv = value;}

        public static void Main()
            InterruptPort button1 = new InterruptPort((Cpu.Pin)FEZ_Pin.Interrupt.Di0, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeHigh);
            button1.OnInterrupt += new NativeEventHandler(button1_OnInterrupt);
        public static void button1_OnInterrupt(uint port, uint state, DateTime time)
            FEZ_Components.SerialLCD LCD = new FEZ_Components.SerialLCD(FEZ_Pin.Digital.Di1);
            time1 = time.Millisecond;


one suggestion I have - don’t leave all that stuff in your interrupt handler once your testing is done!

Create and dispose an LCD, clear the display and move to the home position, that’ll cost lots of time, given there’s inherent delays in the LCD code. Your next interrupt might not get handled any time soon :wink: