Created
December 16, 2017 21:18
-
-
Save undocumented-code/cc76214596b1a3bce6b902369ba1f83a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <avr/sleep.h> | |
#include <avr/interrupt.h> | |
void setup() { | |
//intialize the input (the power switch) | |
pinMode(4, INPUT); | |
//intialize the output (motherboard power switch) | |
pinMode(3, OUTPUT); | |
} | |
void loop() { | |
sleep(); | |
//Pretend to press the power button | |
digitalWrite(3, HIGH); | |
delay(500); | |
digitalWrite(3, LOW); | |
} | |
//Sleep code taken from https://bigdanzblog.wordpress.com/2014/08/10/attiny85-wake-from-sleep-on-pin-state-change-code-example/ | |
void sleep() { | |
GIMSK |= _BV(PCIE); // Enable Pin Change Interrupts | |
PCMSK |= _BV(PCINT4); // Use PB3 as interrupt pin | |
ADCSRA &= ~_BV(ADEN); // ADC off | |
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // replaces above statement | |
sleep_enable(); // Sets the Sleep Enable bit in the MCUCR Register (SE BIT) | |
sei(); // Enable interrupts | |
sleep_cpu(); // sleep | |
cli(); // Disable interrupts | |
PCMSK &= ~_BV(PCINT4); // Turn off PB3 as interrupt pin | |
sleep_disable(); // Clear SE bit | |
ADCSRA |= _BV(ADEN); // ADC on | |
sei(); // Enable interrupts | |
} // sleep | |
ISR(PCINT0_vect) { | |
// This is called when the interrupt occurs, but I don't need to do anything in it | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment