1
\$\begingroup\$

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.

\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

Yes, possible.

Problem what you now have may be that the pusbutton does wake the MCU, but depending on your clock source, it may take so long to wake up that the pushbutton is already released when MCU wakes up, so it will not run the ISR to toggle the flag.

Another thing what might happen is that when pressing the pushbutton, there is a constant INT0 flag getting triggered which makes the MCU to just execute the interrupt code multiple times and the flag may get left in the wrong state. Also it will only run one assembly instruction of main code between interrupts.

You might also want to look at avr-libc examples how to implement a sleep mode properly.

\$\endgroup\$
2
  • \$\begingroup\$ Ok, thanks for the comment. Seems like it kinda works with a pin change interrupt slightly better than with an external interrupt. But there occurred another problem: I flashed another sketch onto the Attiny85 and it suddenly did not let itself program itself, although I did not change anything in the wiring. Is it possible to damage the controller if you upload a program too often? 🤔 \$\endgroup\$ Commented Oct 8, 2023 at 18:35
  • \$\begingroup\$ @PostFah It is guaranteed for 10 thousand erase/write cycles, so yes it will damage ever so slightly every time you upload a program. But it is more likely you did something that caused it to not go into bootloader any more, than hit 10000 erase/write cycles. \$\endgroup\$ Commented Oct 8, 2023 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.