this might seem a dumb question, but is it somehow possible with an ATtiny85 (or any other microcontroller) to toggle with a push-button between active and sleep mode?
Startup -> doing something -> if button press, go sleep
if button is pressed again while sleeping, wake up and go back to work
if button is pressed while working -> go sleep again
If yes, how? My µC seems to be stuck in the sleep mode. What am I doing wrong?
Thanks for help.
This is my code so far:
#include <avr/interrupt.h>
#include <avr/sleep.h>
volatile bool sleepflag = false;
void setup() {
pinMode(PB1, OUTPUT);
//Pin PB1 as output
pinMode(PB2, INPUT);
//I used an external pull up resistor here
//setup interrupt INT0
MCUCR |= (1<<ISC01); //trigger interrupt on falling edge
GIMSK |= (1<<INT0); //enable interrupt INT0
}
ISR(INT0_vect){
sleepflag = !sleepflag;
}
void work(){
// do something
}
void loop() {
if(sleepflag){
PORTB &= ~(1<<PB1); //LED off
sei();
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_cpu();
sleep_disable();
}
else{
work();
if(!digitalRead(PB2)){
sleepflag = true;
}
}
Edit: Seems like waking up is possible with INT0 only with a low (?) level according to the datasheet of the ATtiny85. Set the ISC01 bit to zero, but now it seems like it is stuck again in the sleep mode.