Waking Mega from sleep using serial and pin interrupt

Hi All

Looking for some advice on how to wake my mega project from serial port input and pin change interrupt. The pin change interrupt counts a 32kHz clock IC pulse to set a 1 second flag for the main loop.

I can command the device to sleepNow() and get a reduced current but need a different sleep function that does not detach Interrupt.

The following code sends the mega to sleep which wakes on serial input but it is missing the pin change

#include <avr/power.h>
#include <avr/sleep.h>

//pins
const byte pin_32K = 6;

//global vars
boolean secFlag=LOW;
unsigned long intCount=0;

void setup() {
  Serial.begin(115200);
  Serial1.begin(9600); 
  attachInterrupt(digitalPinToInterrupt(pin_32K), isr_32k, FALLING);
}

void loop() {
  while(1){
    MainPortCommand();  //intercept serial in from port 0

    //1 second event flag
    if(secFlag==HIGH){
      //do some work
      sleepNow();
    }
  }
}

void sleepNow(void){
  set_sleep_mode(SLEEP_MODE_IDLE); // sleep mode is set here
  detachInterrupt(digitalPinToInterrupt(pin_32K));
    
  sleep_enable(); // enables the sleep bit in the mcucr register
  
  //disable periferals
  power_adc_disable();
  power_spi_disable();
  power_timer0_disable();
  power_timer1_disable();
  power_timer2_disable();
  power_twi_disable();

  sleep_mode(); // here the device is actually put to sleep!!
    
  // THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP
  sleep_disable();    // disable sleep...
  power_all_enable(); // enable periferals  
  attachInterrupt(digitalPinToInterrupt(pin_32K), isr_32k, FALLING);
}

void MainPortCommand(void){
  //check serial port buffer if data available
}

void isr_32k(void) {
  intCount+=1;
  if (intCount>=32000) {
    intCount=0;
    secFlag=HIGH;
  }
}

What is the serial input coming from?

Why do you have an infinite loop in an infinite loop()?

  attachInterrupt(digitalPinToInterrupt(pin_32K), isr_32k, FALLING);

Which interrupt number does pin 6 map to? Can you say none?

Why are you not using a pin that actually maps to an interrupt number?

attachInterrupt() only activates a hardware interrupt, it does not handle pin change interrupts. There is no (built-in) pin change interrupt library in the Arduino IDE, so you either have to change the necessary registers yourself or install any of the libraries for this task the library manager offers (just enter "pin change interrupt" into the search field).
BTW: if you detach the interrupt before sleeping (idling) the Arduino won't wake up even if you use the hardware interrupt.

PaulS:
What is the serial input coming from?

The project powers up a sensing probe then reads text data at 9600 on Serial port 1. Serial port 0 outputs the data to a serial modem.

PaulS:
Why do you have an infinite loop in an infinite loop()?

The complete code has variables in the main loop that functionally I dont want global. Also have been working on this code on and off for some time and there was an Arduino function that did not work properly in the main loop but cannot remember which and have kept to my ingrained void main(void) style from C days.

PaulS:
Which interrupt number does pin 6 map to? Can you say none?
Why are you not using a pin that actually maps to an interrupt number?

My Mega chip is a 2561 not the 2560 and Arduino pin 6 (IC pin 8 ) is the External Interrupt 6 and a valid interrupt pin. Chips very almost identical but with less and larger pins to solder.

pylon:
attachInterrupt() only activates a hardware interrupt, it does not handle pin change interrupts. There is no (built-in) pin change interrupt library in the Arduino IDE, so you either have to change the necessary registers yourself or install any of the libraries for this task the library manager offers (just enter "pin change interrupt" into the search field).

The pin is an External Interrupt pin and as such has its own interrupt vector which is supported by Arduino. The hardware generated interrupt is handled by the isr_32k() function.

pylon:
BTW: if you detach the interrupt before sleeping (idling) the Arduino won't wake up even if you use the hardware interrupt.

Quite correct.

What I want to do is to wake at each pulse and increment a counter then go back to low power. When the counter reaches a point leave sleep and run. Additionally the mega needs to wake from sleep from a serial port interrupt so the sleep mode can only be SLEEP_MODE_IDLE - the least power saving level unfortunately.

The pin is an External Interrupt pin and as such has its own interrupt vector which is supported by Arduino. The hardware generated interrupt is handled by the isr_32k() function.

On the Mega pin 6 is not an external interrupt pin, these are only available on pins 2,3 and 18-21.

My Mega chip is a 2561 not the 2560 and Arduino pin 6 (IC pin 8 ) is the External Interrupt 6 and a valid interrupt pin. Chips very almost identical but with less and larger pins to solder.

The 2561 is not supported by the standard IDE. Which core do you use? Post a link to the schematics of that board and don't tell us you use a Mega if you use a custom board with a non-standard chip.

What I want to do is to wake at each pulse and increment a counter then go back to low power. When the counter reaches a point leave sleep and run. Additionally the mega needs to wake from sleep from a serial port interrupt so the sleep mode can only be SLEEP_MODE_IDLE - the least power saving level unfortunately.

Post the current code when you corrected above errors.