Interruption meanwhile MCU is sleeping

Hi,

I'm working with interruptions and sleep mode in Arduino Mega2560.

I've created a simply interruption function that increases a counter when interruption is attached.

  • When the MCU is awake, it works fine. The counter increases as the same way the interruption is done.
  • When the MCU is asleep, it doesn't work. The counter always counts less than the real number of interruptions.

I tried several paths to solve this, but it doesn't matter, even saving the data at EEPROM and reading it every interruption.

  • I'm using interruption 5 (pin 18):
attachInterrupt(5, count, FALLING);

My code is like shown:

void count() { 
   init_int_time = millis();
   
   Counter = EEPROM.read(20);
   
   if (init_int_time - last_int_time > 200) {
     Counter++;      
     last_int_time = init_int_time;
   }
   
   EEPROM.write(20, Counter);
}

Could some give me some help with this??

Thanks!!

Regards.

I recall reading in one of the datasheets that a sleeping MCU will only wake for level interrupts (HIGH, LOW) and not edge-triggered interrupts (RISING, FALLING, BOTH). Check the datasheet to see if that restriction exists in the sleep mode you are using.

Hi,

thanks johnwasser.

you were right, I read in Atmega datasheet about this.

  • Only INT0:3 wake up MCU by non edge-triggered (Rising, Falling, Change) interrupts.
  • And INT4:7 wake up MCU by level (High, Low) interrupts.

But I was working with interruption 5 (pin 18 in Arduino Mega2560), and the fact is MCU wake up with 'Falling' mode.

After reading datasheet, I also understand that MCU need a some cycles plus a time (14CK [I don't know which is CK]) to start-up.

Now, I'm still trying to know why is counting less than the number of interruptions (it could be because of the time of level has to be hold before the MCU wake up and can trigger the interruption).

Regards.

The regular interrupts can only wake the MCU from sleep mode when the interrupt mode is LOW. If you want to wake the MCU from sleep mode on an edge, use a pin change interrupt instead.