I'm seeing some odd behavior for what should be a simple project. Maybe I'm missing something and someone can point out the error of my ways?
What I'm trying to do is capture a sensor input. To test my code I've simulated the sensor with a simple momentary push button. See the schematic attached.
The problem is when I push one button, it triggers the interrupt for the other buttons. Here is my code:
public class Program2 { static DateTime dtLastPress; static int intBounceWait = 200; public static void Main() { InterruptPort ipT1 = new InterruptPort(Pins.GPIO_PIN_D0, false, Port.ResistorMode.PullDown, Port.InterruptMode.InterruptEdgeLow); ipT1.OnInterrupt += T_OnInterruptT1; InterruptPort ipT2 = new InterruptPort(Pins.GPIO_PIN_D1, false, Port.ResistorMode.PullDown, Port.InterruptMode.InterruptEdgeLow); ipT2.OnInterrupt += T_OnInterruptT2; InterruptPort ipT3 = new InterruptPort(Pins.GPIO_PIN_D2, false, Port.ResistorMode.PullDown, Port.InterruptMode.InterruptEdgeLow); ipT3.OnInterrupt += T_OnInterruptT3; InterruptPort ipT4 = new InterruptPort(Pins.GPIO_PIN_D3, false, Port.ResistorMode.PullDown, Port.InterruptMode.InterruptEdgeLow); ipT3.OnInterrupt += T_OnInterruptT4; Thread.Sleep(Timeout.Infinite); } static void T_OnInterruptT1(uint data1, uint data2, DateTime time) { if (dtLastPress.AddMilliseconds(intBounceWait) > time) // prevent bounce { Debug.Print("T1 = bounce wait"); return; // prevent bounce } dtLastPress = time; RecordPress(1, time); } static void T_OnInterruptT2(uint data1, uint data2, DateTime time) { if (dtLastPress.AddMilliseconds(intBounceWait) > time) // prevent bounce { Debug.Print("T2 = bounce wait"); return; // prevent bounce } dtLastPress = time; RecordPress(2, time); } static void T_OnInterruptT3(uint data1, uint data2, DateTime time) { if (dtLastPress.AddMilliseconds(intBounceWait) > time) // prevent bounce { Debug.Print("T3 = bounce wait"); return; // prevent bounce } dtLastPress = time; RecordPress(3, time); } static void T_OnInterruptT4(uint data1, uint data2, DateTime time) { if (dtLastPress.AddMilliseconds(intBounceWait) > time) // prevent bounce { Debug.Print("T4 = bounce wait"); return; // prevent bounce } dtLastPress = time; RecordPress(4, time); } static void RecordPress(int intTarget, DateTime time) { Debug.Print("T" + intTarget.ToString() + " = " + DateTime.Now.ToString("MM/dd/yy H:mm:ss.fff")); } }
If I run the code and press the buttons in order (T1, T2, T3, T4) I get the following output:
T3 = 06/01/11 0:00:10.259
T4 = bounce wait
T3 = bounce wait
T4 = bounce wait
T2 = bounce wait
T1 = bounce wait
T2 = 06/01/11 0:00:12.705
T3 = 06/01/11 0:00:16.861
T4 = bounce wait
T2 = bounce wait
T1 = bounce wait
Any insight into what I'm doing wrong would be appreciated.