Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Button Interrupts


Best Answer CW2, 15 December 2013 - 04:14 PM

If I run the code and press the buttons in order (T1, T2, T3, T4) I get the following output:

 

Since you have interrupts set to low (falling) edge and buttons connected to logic high, the order of pressing does not really matter - important is the order of releasing.

There is also incorrect assignment ipT[color=#ff0000;]3[/color].OnInterrupt += T_OnInterruptT4, it should be ipT[color=#ff0000;]4[/color].

 

Also, you'd need to use separate dtLastPress variables for each button to handle debounce; and initialize it when the interrupt is first called - the current implementation uses unitialized value, so the first time any interrupt is called, the condition if(dtLastPress.AddMilliseconds() > time) is always false.

Go to the full post


  • Please log in to reply
2 replies to this topic

#1 cce1911

cce1911

    Advanced Member

  • Members
  • PipPipPip
  • 71 posts
  • LocationGeorgia, USA

Posted 15 December 2013 - 03:32 PM

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.

Attached Files



#2 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 15 December 2013 - 04:14 PM   Best Answer

If I run the code and press the buttons in order (T1, T2, T3, T4) I get the following output:

 

Since you have interrupts set to low (falling) edge and buttons connected to logic high, the order of pressing does not really matter - important is the order of releasing.

There is also incorrect assignment ipT[color=#ff0000;]3[/color].OnInterrupt += T_OnInterruptT4, it should be ipT[color=#ff0000;]4[/color].

 

Also, you'd need to use separate dtLastPress variables for each button to handle debounce; and initialize it when the interrupt is first called - the current implementation uses unitialized value, so the first time any interrupt is called, the condition if(dtLastPress.AddMilliseconds() > time) is always false.



#3 cce1911

cce1911

    Advanced Member

  • Members
  • PipPipPip
  • 71 posts
  • LocationGeorgia, USA

Posted 15 December 2013 - 04:48 PM

CW2, thanks for your response. I made the changes you suggested and still had a problem. Then I realized that I had not tied the 3v button cell ground to the NP2 ground. Once I did that, everything seems to work just fine.

 
 





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.