External interrupt not working.

Hi, guys.

I have problem in following code: i have attached interrupt to pin 2. whenever it detects the Rising edge at pin 2 it should capture the pin 7 hundred times and store its data to array. after capturing it should send to serial port. but i am not getting anything on serial port.

look at the code below and shows me some help.

int dev[100];

void setup(){
Serial.begin(9600);
pinMode(2, INPUT);
pinMode(7, INPUT);
attachInterrupt(2, sta, RISING);
}

void sta()
{
for (int i=0;i<=100;i++)
{
dev*=digitalRead(7);*

  • }*
  • for (int i=0;i<=100;i++)*
  • {*
    _ Serial.println(dev*);_
    _
    }_
    _
    }_
    void loop()
    _
    {};*_
    Thanks
    EJ

Four issues right off:

  1. dev[100]: dev[0] -> dev[99]. Your loops go 0 to 100 (off by one, should be i<100).

  2. Kill two birds with one stone. Combine the data read and print in the same loop.

  3. In your read and print loops, you show working with dev not dev[ i ]. I think you had the subscript i listed in the code, but the message formatter interprets it as the code for starting italics (and does not print it).

  4. Do you have a 10k pull-down resistor from D2 to ground? If not, the pin input floats and it may not swing enough to trigger the rising-edge interrupt. A possible alternative is to enable the internal pull-up resistor and change the interrupt to falling-edge.

Still i am not getting anything. plz see the corrected code.

does serial.println() works in ISR..?

if NO then how to solve this issue.?

int dev[100];

void setup(){
Serial.begin(9600);
pinMode(2, INPUT); // Interrupt pin
pinMode(7, INPUT); // Read pin
attachInterrupt(2, sta, RISING);
}

void sta()
{
for (int i=0;i<100;i++)
{
dev*=digitalRead(7);*

  • }*
  • for (int i=0;i<100;i++)*
  • {*
    _ Serial.println(dev*);_
    _
    }_
    _
    }_
    void loop()
    _
    {};*_
    Thanks

plz see this there was problem.

int dev[100];

void setup(){
Serial.begin(9600);
pinMode(2, INPUT);
pinMode(7, INPUT);
attachInterrupt(2, sta, RISING);
}

void sta()
{
for (int i=0;i<100;i++)
{
dev*=digitalRead(7);*

  • }*
  • for (int i=0;i<100;i++)*
  • {*
    _ Serial.println(dev*);_
    _
    }_
    _
    }_
    void loop()
    _
    {};*_

int dev[100];

void setup(){
Serial.begin(9600);
pinMode(2, INPUT);
pinMode(7, INPUT);
attachInterrupt(2, sta, RISING);
}

void sta()
{
for (int i=0;i<100;i++)
{
dev*=digitalRead(7);*

  • }*
  • for (int i=0;i<100;i++)*
  • {*
    _ Serial.println(dev*);_
    _
    }_
    _
    }_
    void loop()
    _
    {};*_

i do not know bur somethiung ius going wrong here. i am able to see right code before posting. whenever i post it it will appear wrong. for example i wrote, dev*=digitalRead(7); but its not showing exactly the same i typed.*

it is not showing the [ i ] array variable because if you type in "[ i ]" without spaces between the braces and the i the reply formatter takes that as the code to start italic print (take a look at the posts)

From my experience with the DUE, you can run a Serial.print() from within a ISR. HOWEVER, you have to make sure that you finish your print and exit the ISR before the next interrupt is generated, otherwise the unit will behave unpredictably. My personal technique is to turn on an LED when it enter the ISR and turn it off when it exits. That will give you both an idea of the duty cycle and period.

Confirmed, it does not printing in the ISR. Better to pass flag into main loop and print values out there.

          int dev[100]  = { 0};
volatile  int flag      = 0;

void setup(){
  Serial.begin(115200);
  pinMode(2, INPUT);
  pinMode(7, INPUT);
  attachInterrupt(2, sta, RISING);
}

void sta()
{
  for(int i = 0; i < 100; i++) {
    dev[i] = digitalRead(7);
  }
  flag = 1;
}

void loop()
{
  if( flag ) {
  Serial.print("\n");  
  for(int i = 0; i < 100; i++) {
    Serial.print( i, DEC);
    Serial.print("\t");
    Serial.println(dev[i]);
    delay(10);
    }
  flag = 0;
  }
}

Hi, Master T. is it printing continuously or just whenever interrupt trigger,? i have no DUE at present.

It prints ones on each rising edge triggered on pin 2. attachInterrupt(2, sta, RISING);

MasterT: It's not the printing in the ISR that's the issue. I frequently do a serial.print() in an ISR when I read sensors. What does not work are FOR and WHILE loops. If you have a loop in the ISR it will execute once and hang (not return). I have not found a satisfactory expnalation for this behavior.

EJLED: Do you need to save the state data from D7 for later use or do you just dump it out the serial port?

Here's my take on just dumping it out the port:

// volatile uint8_t dev[100];
volatile bool flag;

void setup() {

  Serial.begin(115200);

  flag = false;

  // Setup blinky lights for each of the interrupts PIOC:B.27/L

  REG_PIOB_WPMR = 0x50494F00;                // p.674 PIO Write Protect Mode (RW) 32 bit - enable write to PIOB register
  REG_PIOB_PER = REG_PIOB_PSR | 1 << 27;     // p.633 Enable PIO control / disable peripheral control on port B.27 'L' LED
  REG_PIOB_OER = REG_PIOB_OSR | 1 << 27;     // p.636 Enable output on port B.27
  REG_PIOB_PUDR = REG_PIOB_PUSR | 1 << 27;   // p.653 Disable pullup resistor on port B.27
  REG_PIOB_CODR = 1 << 27;                   // Turn off B.27

  // Setup interrupt inputs: B.25 - Arduino D7

  REG_PMC_PCER0 = REG_PMC_PCSR0 | 0x1000;     // p.542 Turn on PIOB clock, necessary for reading input
  REG_PIOB_PER = REG_PIOB_PSR | 1 << 25;      // p.633 Enable PIO control / disable peripheral control on port B.25 - Arduino D2
  REG_PIOB_ODR = !REG_PIOB_OSR | 1 << 25;     // p.637 Enable input on port B.25
  REG_PIOB_PUDR = !REG_PIOB_PUSR | 1 << 25;   // p.654 Disable pullup resistor on port B.25
  REG_PIOB_IFER = REG_PIOB_IFSR | 1 << 25;    // p.640 Enable glitch filter on PIO B.25
  REG_PIOB_ESR = !REG_PIOB_ELSR | 1 << 25;    // p.668 interrupt source is edge detection on PIO B.25
  REG_PIOB_REHLSR = REG_PIOB_FRLHSR | 1 << 25;// p.671 interrupt source is rising edge detection on PIO B.25
  REG_PIOB_AIMER = REG_PIOB_AIMMR | 1 << 25;  // p.664 additional interrupt modes enable on PIO B.25
  REG_PIOB_IER = REG_PIOB_IMR | 1 << 25;      // p.646 enable input change interrupt on PIO B.25

  // Setup input w/ pullup: C.23 - Arduino D7

  REG_PMC_PCER0 = REG_PMC_PCSR0 | 0x2000;     // p.542 Turn on PIOC clock, necessary for reading input
  REG_PIOC_PER = REG_PIOC_PSR | 1 << 23;      // p.633 Enable PIO control / disable peripheral control on port C.23 - Arduino D7
  REG_PIOC_ODR = !REG_PIOC_OSR | 1 << 23;     // p.637 Enable input on port C.23
  REG_PIOC_PUER = REG_PIOC_PUSR | 1 << 23;    // p.654 Disable pullup resistor on port C.23
  REG_PIOC_IFDR = !REG_PIOC_IFSR | 1 << 23;   // p.640 Disable glitch filter on PIO C.23

  NVIC_EnableIRQ(PIOB_IRQn);                 // Enable PIOC interrupts

}

void loop() {
  REG_PIOB_CODR = 1 << 27;                   // Turn off B.27
  if (flag) {
    for (int i = 0; i < 100; i++) {
      Serial.println((REG_PIOB_PDSR & 1 << 23) >> 23 );
    }
    flag = false;
  }
}

void PIOB_Handler() {
  boolean dummy = (REG_PIOB_ISR >> 4);       //  p. 649 Read PIOC interrupt status, clear register
  REG_PIOB_SODR = 1 << 27;                   //  Turn on PIO B.27
  flag = true;
}

I am using Here again , I setup an LED to flag interrupt status. When operating as intended, it blinks briefly when D2 goes high. Its also shows that the ISR hangs when is has a FOR or WHILE loop, as the LED does not extinguish.

hey, XUTHUS, I must SAVE data. i compiled your above code but it showing following error.

C:\Users\jethi\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.10\cores\arduino/WInterrupts.c:147: multiple definition of `PIOB_Handler'

sketch\DUE_INT_COre.ino.cpp.o:C:\Users\jethi\Desktop\interrupt_due\DUE_INT_COre/DUE_INT_COre.ino:55: first defined here

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Arduino Due (Programming Port).

Thanks