Trouble Exiting Sleep Mode Attiny1614

Hi there,

Im having an issue exiting sleep mode in the Attiny1614. Ive read lots of documentation and I don’t think I understand how to properly exit sleep. My inturrupt has the proper code to disable sleep but the inturrupt is dead once it goes to sleep. If i dont disable the inturrupt with cli(); then the device wakes itself up again right after going to sleep. The goal is one button press to sleep and one to wake.

Guidance is greatly appreciated.

#include<Arduino.h>
#include<EEPROM.h>
#include <avr/sleep.h>
#include <avr/interrupt.h>


static constexpr byte button = digitalPinToInterrupt(1);
static const uint8_t batteryPin = 6;
int ledPin = 4;
uint8_t ENBL = 3; //Pull high to enable the power supplies and pull low to disable and switch to unregulated vbat for sleep
byte isr_handler = 0;
byte power_status = 0; //if 0 == device off::if 1 == device on

void setup() {
  Serial.pins(8, 9);
  Serial.begin(9600);

  pinMode(ledPin, OUTPUT);
 // pinMode(button, INPUT_PULLUP);
  attachInterrupt(button, isr_trigger, CHANGE);
  pinMode(button, INPUT);
  pinMode(batteryPin, INPUT);
  pinMode(ENBL, OUTPUT);


}


void loop() {
  Serial.println(power_status);
  analogWrite(ledPin, 255);


  //the actual isr logic
  //delay(10); //quick and dirty debounce
  if (isr_handler == 1) {
    button_logic();
    delay (1000);
    isr_handler = 0;
  }

}

int button_logic() {

  if (power_status == 0) {
    power_status = 1;
    power_on();
    return;
  }
  if (power_status == 1) {
    power_status = 0;
    power_off();
    return;
  }

}



//what to do when the device is going to sleep or waking up

int power_on() {
  Serial.println("on");
  digitalWrite(ENBL, HIGH);
}

///////////////////////Sleep Here//////////////////////////////

int power_off() {
  Serial.println("off");

  cli();
  sleep_enable ();
  set_sleep_mode (SLEEP_MODE_PWR_DOWN);
  sleep_mode ();
  sei();
 
  static constexpr byte button = digitalPinToInterrupt(1);
  attachInterrupt(button, isr_trigger, CHANGE);
  digitalWrite(ENBL, HIGH);

}



//the isr trigger.

void isr_trigger() {
  analogWrite(ledPin, 10);
  sleep_disable();
  isr_handler = 1;
}

Anyone?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.