-->
Page 1 of 2

attach/detachInterrupt usage?

PostPosted: Tue Dec 27, 2016 3:47 am
by hiloz
I'm running this code on a LinkNode D1 using the Arduino IDE

The code uses an ISR to record the timings of edge transitions from a DHT11 sensor. However, it inevitably crashes after a random number of iterations.

Am I using interrupts correctly?

Note - I am calling micros() inside the ISR - is that safe?

UPDATE: The call to micros() inside pin_changed() is not the problem. I still get crashes if I replace it with t = last_time+1.

Code: Select all
#define DHT_PIN D7

volatile unsigned int tcount;
volatile unsigned long last_time;
volatile unsigned long timings[50];

int seqno = 0;

void pin_changed() {
  if (tcount >= 50) {
    return;
  }
  unsigned long t = micros();
  unsigned long duration = t - last_time;
  timings[tcount++] = duration;
  last_time = t;
}

void dht_begin(int pin) {
  last_time = micros();
  tcount = 0;
  pinMode(pin, OUTPUT);
  digitalWrite(pin, LOW);
  delay(15);
  pinMode(pin, INPUT);
  attachInterrupt(pin, pin_changed, FALLING);
}

void dht_end(int pin) {
  detachInterrupt(pin);
}

void dump_timings() {
  Serial.print(tcount);
  Serial.print(":");
  for (int i = 0; i < tcount; ++i) {
    Serial.print(" ");
    Serial.print(timings[i]);
  }
}

void setup() {
  Serial.begin(115200);
  seqno = 0;
}

void loop() {
  seqno++;
  dht_begin(DHT_PIN);
  delay(10);
  dht_end(DHT_PIN);
  Serial.print(seqno);
  Serial.print(" ");
  dump_timings();
  Serial.print("\r\n");
  delay(5000);
}


Here is some typical output:
Code: Select all1384, room 16
tail 8
chksum 1 42: 15070 167 79 80 79 126 127 127 127 79 79 79 79 79 79 80 79 80 79 79 80 126 79 80 126 127 79 79 79 80 79 79 79 81 79 79 127 126 79 80 79 124
2 42: 15079 168 79 79 127 79 127 79 127 79 79 79 79 79 80 79 79 80 80 79 79 127 79 79 127 79 79 79 79 79 79 79 79 81 79 79 127 127 127 126 80 76
3 42: 15026 172 79 79 79 127 127 127 127 79 79 79 79 79 79 79 80 80 79 79 80 126 79 80 126 127 79 79 80 79 79 79 79 81 79 79 127 127 79 79 79 124
4 41: 15250 75 79 79 127 127 126 127 79 79 80 79 79 79 79 79 81 79 79 79 127 79 79 127 127 79 79 79 79 79 79 79 81 79 79 127 127 79 79 79 124
5 42: 15022 172 79 80 79 126 127 127 127 127 79 79 79 79 79 80 79 80 79 80 79 127 79 79 127 126 80 79 79 79 79 79 79 81 79 79 127 127 79 79 127 77
6 41: 15245 76 79 79 127 127 126 127 127 79 79 79 80 79 79 79 81 79 79 79 127 79 79 127 127 79 79 79 79 80 79 79 80 80 79 127 126 80 79 126 77
7 41: 15241 76 79 79 127 126 127 127 127 79 79 79 79 80 79 79 80 80 79 79 127 79 79 127 126 79 80 79 79 79 79 79 81 79 79 127 127 79 79 127 76
8 41: 15241 75 80 79 126 127 127 127 126 79 79 80 79 79 79 79 80 80 79 79 126 80 79 126 127 79 79 79 79 80 79 79 80 79 79 127 127 79 79 127 76
9 41: 15241 75 79 80 126 127 127 127 126 79 79 80 79 79 79 79 81 79 79 79 127 79 79 126 127 79 79 79 80 79 79 79 80 80 79 126 127 79 79 127 77
10 41: 15241 75 80 126 80 79 126 127 127 79 79 80 79 79 79 79 81 79 79 79 127 79 79 127 127 79 79 79 79 79 79 80 80 79 79 127 127 127 79 127 76
11 41: 15242 75 79 127 79 79 79 79 79 80 79 79 79 79 79 79 81 79 79 79 127 79 79 127 127 79 79 79 79 80 80 83 75 80 79 126 127 79 79 127 124

Exception (0):
epc1=0x40201bf0 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

ctx: sys
sp: 3ffffc30 end: 3fffffb0 offset: 01a0

>>>stack>>>
3ffffdd0:  40106b4c 00bd9999 00040000 ffffffff 
3ffffde0:  ffffffff 00000020 40240000 00000199 
3ffffdf0:  42d80000 43610000 00000000 00000022 
...
<<<stack<<<

 ets Jan  8 2013,rst cause:2, boot mode:(1,6)


 ets Jan  8 2013,rst cause:4, boot mode:(1,6)

wdt reset

Re: attach/detachInterrupt usage?

PostPosted: Thu Jan 05, 2017 3:39 pm
by Drpepper
Did you find a Solution?

Re: attach/detachInterrupt usage?

PostPosted: Thu Jan 05, 2017 6:49 pm
by Ribeiro Santos
In pin_changed() just set a flag and do the rest of the code at loop() with some kind of flag.
and use Delay(10) maximum time, and in the loop() use some king of flag to know when execute your code.

Re: attach/detachInterrupt usage?

PostPosted: Fri Jan 06, 2017 10:17 am
by Drpepper
Cool.
I did mine with a timer See.. viewtopic.php?f=32&t=13202
Did that stop your crashes?