Digispark Power Save
Disable analog-to-digital conversion
To safe power you can deactivate function you don't use:
ADCSRA = 0; // Disable analog-to-digital conversion
Power down & button wakeup
This example shows how to let the ATTiny go to sleep and letting it wake up with an interrupt for a button state change:
// The pin to use in the interupt
#define PIN_I 0
bool _sleeping = false;
bool _goAsleep= false;
void setup() {
/*************************\
** Prepare pin interrupt **
\*************************/
// Disable interrupts
cli();
// Enable interrupt handler (ISR) for PIN_I (PCINT1)
PCMSK |= (1 << PIN_I);
// Enable PCINT interrupt in the general interrupt mask
GIMSK |= (1 << PCIE);
// Set pin as input pin - has dedecated pullup
pinMode(PIN_I, INPUT);
// Last line of setup - re-enable interrupts
sei();
}
ISR(PCINT0_vect) {
// Handle only falling edges
if (digitalRead(PIN_I) == LOW) {
if (_sleeping) {
// Interrupt while sleeping
_sleeping = false;
MCUCR&=~(1<<SE); // Disabling sleep mode
} else {
// Interrupt while awake -> flag sleep request
_goAsleep = true;
}
}
}
void fallAsleep() {
_sleeping = true;
// Enabling sleep mode and powerdown sleep mode
MCUCR|=(1<<SM1);
// Enabling sleep enable bit
MCUCR|= (1<<SE);
// Sleep instruction to put controller to sleep
__asm__ __volatile__ ( "sleep" "\n\t" :: );
}
void loop() {
if (_goAsleep) {
_goAsleep = false; // handled
fallAsleep();
}
}
Power down extreme
// Disable interrupts
cli();
// Reduce the clock frequency to conserve power
clock_prescale_set(clock_div_128);
// Disable all modules
power_all_disable();
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
// Disable the ADC
ADCSRA &= ~ bit(ADEN);
power_all_disable();
sleep_enable();
sleep_bod_disable();
sleep_cpu();