LowPowerLab Forum

Hardware support => Moteino => Topic started by: K1JOS on June 25, 2014, 10:41:36 PM

Title: Interrupt doesnt work On pin2 but works on pin3 [SOLVED]
Post by: K1JOS on June 25, 2014, 10:41:36 PM
I have been trying to get a sketch working to debounce a button on pin 2 as part of an interrupt routine.  I have been using examples from the Arduino forum.  After struggling for some time, on a hunch, I switched to pin 3 and all an interrupt  and debounce works 100%

I have both pin 2 and 3 wired to one end of the button (also wired with 10k resistor to ground) and the other end of the button to the 5v rail.

I do change the "attachInterrupt(pin,x,rising) to match whichever pin, 2 or 3) i am tersting.

Should pin 2 work identical as pin 3 for interrupts 0 and 1 ?

Any advice appreciated.

Title: Re: Interrupt doesnt work On pin2 but works on pin3
Post by: K1JOS on June 25, 2014, 11:42:34 PM
Just to be clear, pin 2 and 3 do not share any common wiring, I have separate buttons for each pin and they are wired identically with one side of the button to either pin2 or pin 3 and both have a 10K pull down to ground.  the other side of each button is wired to 5v rail
Title: Re: Interrupt doesnt work On pin2 but works on pin3
Post by: Felix on June 26, 2014, 08:22:55 AM
D2 is used by the RFM69 transceiver. The pinout diagrams illustrate all the used pins: www.moteino.com/#pinout
Title: Re: Interrupt doesnt work On pin2 but works on pin3
Post by: K1JOS on June 26, 2014, 08:43:06 AM
OK, I missed that.... too bad.  I really could have used both external interrupts.  I guess now I will have to use Pinchange interrupt library.  Will your Mega have more than one free external interrupt available?  Any progress on the release of the Mega?
Title: Re: Interrupt doesnt work On pin2 but works on pin3
Post by: bobleponge on June 26, 2014, 09:24:45 AM
Or wire both buttons to the same interrupt pin and to a another digital input (with a diod).
Like this:

     3    4    5
     |     |    |
     \/    |    |
    ---    |    |
     |____ |    |___ connected to the 2nd diod on pin 3.
           B    B2
           |___|
               |
               Ground

Set all Digital pin to INPUT_PULLUP mode.
When no button is pressed you have VCC on pin 4, 5, and 3.
When you press a button, for example B, pin 4 will read 0V, and pin 3 will also read 0.6V (so 0 digital).

In software, you set the interrupt for falling, and upon interrupt you need to read both pin 4 and 5 to figure out which triggered the interrupt.
Title: Re: Interrupt doesnt work On pin2 but works on pin3
Post by: K1JOS on June 26, 2014, 09:49:33 AM
That's very cool !!  I need to think how I would use the debounce code for each button but I guess the external interrupt on pin 3 is superfast (microseconds) clockcycles while the debounce is in the range of milliseconds.  Gives me more to play with and to enjoy.  Although I need this project to complete asap I do love the journey :-)

Title: Re: Interrupt doesnt work On pin2 but works on pin3
Post by: bobleponge on June 26, 2014, 11:36:05 AM
Debounce is easy. In your interrupt handler, have code like this:

volatile int lastPress = 0;
void buttonPressed()
{
    if (!lastPress) lastPress = millis();
}


// In your loop
void loop()
{
     if (lastPress && (millis() - lastPress) > 30) // Wait 30ms for reading the button value
     {
          buttonA = !digitalRead(4); // Invert because of inverted logic
          buttonB = !digitalRead(5); // IIF buttons are connected to ground as in my schematic
          lastPress = 0; // Very important to reset this else the interrupt will not work
     }

     if (buttonA != prevButtonA)  // Or simply if (buttonA)
     {
         fireMissile();
     }

     if (buttonB)
     {
         callBackMissile();
     }
}
Title: Re: Interrupt doesnt work On pin2 but works on pin3
Post by: K1JOS on June 26, 2014, 09:15:06 PM
Many thanks, I wired it up tonight and it works perfectly.