Skip to content

Instantly share code, notes, and snippets.

@kachurovskiy
Last active May 24, 2018 19:24
Show Gist options
  • Save kachurovskiy/0d70beb8f4935f6af158650c195be8db to your computer and use it in GitHub Desktop.
Save kachurovskiy/0d70beb8f4935f6af158650c195be8db to your computer and use it in GitHub Desktop.
#include <avr/sleep.h>
#include <avr/wdt.h>
void setup() {
pinMode(4, OUTPUT);
}
void water(int ms) {
digitalWrite(4, HIGH);
delay(ms);
digitalWrite(4, LOW);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
}
void loop() {
for (int i = 0; i < 10800; i++) {
sleep_enable();
ADCSRA &= ~(1<<ADEN);
setup_watchdog(9);
sleep_mode();
ADCSRA |= (1<<ADEN);
}
water(3000);
}
ISR(WDT_vect) {} // Wake up.
//0=16ms, 1=32ms, 2=64ms, 3=128ms, 4=250ms, 5=500ms, 6=1sec, 7=2sec, 8=4sec, 9=8sec
void setup_watchdog(int timerPrescaler) {
if (timerPrescaler > 9 ) timerPrescaler = 9; //Limit incoming amount to legal settings
byte bb = timerPrescaler & 7;
if (timerPrescaler > 7) bb |= (1<<5); //Set the special 5th bit if necessary
//This order of commands is important and cannot be combined
MCUSR &= ~(1<<WDRF); //Clear the watch dog reset
WDTCR |= (1<<WDCE) | (1<<WDE); //Set WD_change enable, set WD enable
WDTCR = bb; //Set new watchdog timeout value
WDTCR |= _BV(WDIE); //Set the interrupt enable, this will keep unit from resetting after each int
}
@kachurovskiy
Copy link
Author

This is for ATTINY 85. Deep sleep code copied from somewhere, credit to those folks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment