sleep and PCINT again (yes I know!)

I’m struggling to get a grip on using WDT to wake up from sleep in the same sketch as a PCINT interrupt
my aim is for the WDT to wake every second, with the PCINT just counting interrupts in the background

my sketch wakes successfully every second on the WDT, as long as there are no interrupts
if there are interrupts, it seems to not wake up
what I suspect is happening is I am managing to reset the clock every interrupt
so it doesn’t get the chance to timeout

code is here:
main sketch

const byte LED = 6;

extern boolean wdtFlag;
extern boolean windFlag;

//======
// setup
//======
void setup(void) 
{ 
  Serial.begin(57600);
  Serial.println("sleep.wake.tester");
  pinMode(LED, OUTPUT);
  flash();
  windInit();
  sleep(true);
}

//======
// loop
//======
void loop(void) 
{
  if (wdtFlag)
  {
    Serial.print('D');
    Serial.flush();
    flash();
    wdtFlag = false;
    sleep(true);
  }
  
  if (windFlag)
  {
    Serial.print('W');
    Serial.flush();
    windFlag = false;
    sleep(false);
  }
}

void flash(void)
{
  //==========
  // flash LED
  //==========
  for (byte i = 0; i < 1; i++)
  {
    digitalWrite (LED, HIGH);
    delay (50);
    digitalWrite (LED, LOW);
    delay (50);
  }    
}

sleep stuff is here:

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

boolean wdtFlag;

//=====================================================================================
// prepare the watch dog timer and sleep
//=====================================================================================
void sleep(boolean start)
{
  // on entry 
  // true = start the clock
  // false = leave clock running
  //============================
  
  Serial.println('s');
  Serial.flush();

  // set interrupt mode and interval
  //
  // WDP3 WDP2 WDP1 WDP0 timer   ~seconds
  //    0    0    0    0   16 mS
  //    0    0    0    1   32 mS
  //    0    0    1    0   64 mS
  //    0    0    1    1  128 mS 0.125
  //    0    1    0    0  256 mS 0.25
  //    0    1    0    1  512 mS 0.5
  //    0    1    1    0 1024 mS 1
  //    0    1    1    1 2048 mS 2
  //    1    0    0    0 4096 mS 4
  //    1    0    0    1 8192 mS 8
  //
  //============================================
  
  MCUSR = 0;     

  if (start)
  {
    // starting the timer
    //===================
    
    // clear various "reset" flags
    //============================
      
  }
      
  if (start)
  {
    // allow changes, disable reset
    //=============================
    WDTCSR = bit (WDCE) | bit (WDE);

    // wdt enabled and 1 second timer
    //===============================
    WDTCSR = bit (WDIE) | bit (WDP2) | bit (WDP1);
  }
  
    // call reset
    //===========
    wdt_reset();
  

  // set sleep mode
  //===============
  set_sleep_mode(SLEEP_MODE_PWR_DOWN); 

  // disable ADC
  //============
  ADCSRA = 0;  
  
  // turn off brown-out enable in software
  //======================================
  MCUCR = bit (BODS) | bit (BODSE);
  MCUCR = bit (BODS); 

  // sleep...
  //=========
  sleep_mode(); 





  // ... and wake-up
  //================
  
  
  // disable the timer
  //==================
  sleep_disable();

  // enable ADC
  //===========
  ADCSRA |= _BV(ADEN);

  Serial.print('w');
  Serial.flush();
}

//=====================================================================================
// watchDogTimer interrupt
//=====================================================================================
ISR(WDT_vect)
{
  // just set the watchdog flag
  //===========================
  wdtFlag = true;
}

and finally the interrupt:

//=================================
// wind
//=================================
#define WINDSPEEDPIN 3

//=================================
// flag to show interrupt was from wind
//=================================
boolean windFlag;

//=====================================================================================
// interrupt handler for wind ticks
//=====================================================================================
void handleWindTick()
{
  // simply increment the counter
  //=============================
  windFlag = true;
}

//=====================================================================================
// routine to initialise wind sensors
//=====================================================================================
void windInit(void)
{
  pinMode(WINDSPEEDPIN, INPUT_PULLUP);

  // INT1 => D3
  //======================
  attachInterrupt(1, handleWindTick, LOW);          
  
  // ensure interrupts are enabled
  //==============================
  interrupts();

  windFlag = false;

  Serial.print('w');
}

with no interrupts I get

wDs

with the LED flashing every second :slight_smile:

with interrupts I get

wWs

with no LED, until I stop the interrupts, when it reverts to flashing correctly

I would be delighted if someone could cast an eye and tell me where I have been a plonker!
thanks