Skip to content

Instantly share code, notes, and snippets.

@brundage
Created January 24, 2017 02:26
Show Gist options
  • Save brundage/966b22dfbe9b6429de285c6368e1fc0c to your computer and use it in GitHub Desktop.
Save brundage/966b22dfbe9b6429de285c6368e1fc0c to your computer and use it in GitHub Desktop.
/*
* ATTINY85 Sleep Example
* by kevinwatts
*
* Commenting added by Dustin Westaby
*/
#include <avr/sleep.h>
#inlcude <avr/io.h>
//clear bit macro
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
//set bit macro
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
const int led_pin = 0;
const int interrupt_pin1 = 1;
const int interrupt_pin2 = 2;
int which_pin = 1;
void setup(){
//Set all pins to inputs w/ pull-up resistors (stop them from floating or sourcing power)
for (byte i=0; i<5; i++) {
pinMode(i, INPUT);
digitalWrite(i, HIGH);
}
//set LED pin to output
pinMode(led_pin,OUTPUT);
//Blink so I know you've reset/started
for (byte i=0; i<3; i++) {
digitalWrite(led_pin, HIGH);
delay(500);
digitalWrite(led_pin, LOW);
delay(500);
}
sbi(GIMSK,PCIE); // Turn on Pin Change interrupts (Tell Attiny85 we want to use pin change interrupts (can be any pin))
sbi(PCMSK,PCINT1); //Define which pins are doing the interrupting (digital pin 1, DIP 7 in this example)
sbi(PCMSK,PCINT2); //Define which pins are doing the interrupting (digital pin 2, DIP 6 in this example)
}
void loop(){
for(byte i=0; i<whichpin; i++){ //Blink to let me know which interrupt was pressed
digitalWrite(led_pin,HIGH);
delay(500);
digitalWrite(led_pin,LOW);
delay(500);
}
hush_lil_uC(); //Call the sleep function
}
// From http://interface.khm.de/index.php/lab/experiments/sleep_watchdog_battery/
void hush_lil_uC(); {
cbi(ADCSRA,ADEN); //Switch Analog to Digital converter OFF
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // Set sleep mode
sleep_mode(); //Go to sleep
sbi(ADCSRA,ADEN);//Switch Analog to Digital converter back ON
}
ISR(PCINT0_vect) { //All interrupts direct to this ISR function
if(digitalRead(interrupt_pin1)==LOW){
which_pin = 3;
}
else if(digitalRead(interrupt_pin2)==LOW){
which_pin = 6;
}
else{
which_pin = 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment