Tapatalk

Interrupt IO

Interrupt IO

4

PostMar 03, 2017#1

Is there a way to configure an input to trigger a function?

I want to optimize the code so i can send only 1 message every hour or so (just to make sure the node is alive) and every time an input changes state (like a door sensor) i send the status update.

1885
1885

PostMar 22, 2017#2

Hi iznougudpt,

The Interrupts can be used the same way as another Arduino. Make sure you use the D3 (INT1) or the Pin Change Interrupt for other Pins as D2 (INT0) is already allocated to the Radio.

You can use the same Pins used by the Button 1 and Button 2 by using the Pin Change Interrupt. One important thing is to keep the Interrupt handler as small as possible, for example, just changing a single variable. After that you can use your main program to react based on the variable change.

Here is a great reference about interrupts: http://gammon.com.au/interrupts. Here a very small example adapted from the previous link:

Code: Select all

volatile boolean intFlag;

// Interrupt Service Routine (ISR)
void myInterruptHandler()
{
 intFlag = true;
}  // end of isr

void setup ()
{
  attachInterrupt (digitalPinToInterrupt (3), myInterruptHandler , RISING);  // attach interrupt handler
}  // end of setup

void loop ()
{
  if (intFlag)
    {
    // interrupt has occurred
      intFlag = false; // Turn off flag
      //do something or call a function
    }
}  // end of loop
Remember that Interrupts can wake-up the MCU from sleep, so you can use together with sleep functions.

PostMar 22, 2017#3

I just found a good example about using Pin Change Interrupt, in this case for the BT1 and BT2 of the Whisper Node.

Please, use it as an example only as I cut from another project and haven't had time to test much:

Code: Select all

#include <T2WhisperNode.h>

void setup()
{
  // Serial
  Serial.begin(115200);
  Serial.println(F("Pin Change Interrupt"));

  // Enable Pin Change Interrupt for port D, see table below.
  PCMSK2 |= bit(PCINT20);  // want pin D4 (T2_WPN_BTN_1)
  PCMSK2 |= bit(PCINT21);  // want pin D5 (T2_WPN_BTN_2)
  PCIFR  |= bit(PCIF2);   // clear any outstanding interrupts
  PCICR  |= bit(PCIE2);   // enable pin change interrupts for D0 to D5
}

volatile uint8_t bt1Flag= 0;
volatile uint8_t bt2Flag= 0;

// Handle Interrupt
ISR (PCINT2_vect)
{
  if(digitalRead(T2_WPN_BTN_1))
  {
    bt1Flag = 1;
  }
  if(digitalRead(T2_WPN_BTN_2))
  {
    bt2Flag = 1;
  }
}

void loop()
{
  // Button 1 Pressed
  if(bt1Flag)
  {
    Serial.println(F("Bt1 Pressed"));
    // Reset Button Flag
    bt1Flag = 0;
  }

  // Button 2 Pressed
  if(bt2Flag)
  {
    Serial.println(F("Bt2 Pressed"));
    // Reset Button Flag
    bt2Flag = 0;
  }

  // You can do any other thing, but if you use "delay"
  // The execution of the code above will be delayed as well
  // as the interrupt only sets the Flag.

}
Table of Pins and Interrupt Vectors:

Code: Select all

D0	  PCINT16 (PCMSK2 / PCIF2 / PCIE2)
D1	  PCINT17 (PCMSK2 / PCIF2 / PCIE2)
D2	  PCINT18 (PCMSK2 / PCIF2 / PCIE2)
D3	  PCINT19 (PCMSK2 / PCIF2 / PCIE2)
D4	  PCINT20 (PCMSK2 / PCIF2 / PCIE2)
D5	  PCINT21 (PCMSK2 / PCIF2 / PCIE2)
D6	  PCINT22 (PCMSK2 / PCIF2 / PCIE2)
D7	  PCINT23 (PCMSK2 / PCIF2 / PCIE2)
D8	  PCINT0  (PCMSK0 / PCIF0 / PCIE0)
D9	  PCINT1  (PCMSK0 / PCIF0 / PCIE0)
D10	  PCINT2  (PCMSK0 / PCIF0 / PCIE0)
D11	  PCINT3  (PCMSK0 / PCIF0 / PCIE0)
D12	  PCINT4  (PCMSK0 / PCIF0 / PCIE0)
D13	  PCINT5  (PCMSK0 / PCIF0 / PCIE0)
A0	  PCINT8  (PCMSK1 / PCIF1 / PCIE1)
A1	  PCINT9  (PCMSK1 / PCIF1 / PCIE1)
A2	  PCINT10 (PCMSK1 / PCIF1 / PCIE1)
A3	  PCINT11 (PCMSK1 / PCIF1 / PCIE1)
A4	  PCINT12 (PCMSK1 / PCIF1 / PCIE1)
A5	  PCINT13 (PCMSK1 / PCIF1 / PCIE1)

3

PostNov 17, 2017#4

With the example code above, the D2 (INT0) and D3 (INT1) is still working? I ask this because I see that there is one instruction that says "clear any outstanding interrupts".

Thanks!

1885
1885

PostNov 17, 2017#5

Hi mnenoni,

Other interrupts should still work fine, this is only executed once when the software initializes. Just make sure you configure INT0 or INT1 after the cleaning procedure and should be ok.