Skip to content

Instantly share code, notes, and snippets.

@dwhacks
Created September 13, 2013 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dwhacks/6549961 to your computer and use it in GitHub Desktop.
Save dwhacks/6549961 to your computer and use it in GitHub Desktop.
4Mhz_LED_Firefly.ino
/*
* LED Firefly by http://dwhacks.blogspot.ca/ using attiny 13a @4.8MHz
* inspired by http://www.seanet.com/~karllunt/fireflyLED.html
* using core13 for Arduino http://sourceforge.net/projects/ard-core13/
*/
#include <avr/sleep.h>
#include <avr/wdt.h>
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
int sense = 3; // sensing LED connected to analog3
int LED = 3; // LED output pin connected to digital3
int val = 0; // variable to store the value read from sense
int light = 80; // set light threshold, this is where you set how dark it needs to be for the LED to come on. Below 20 may not be accurate enough. Trial and Error.(for me, 50 for yellow 3mm led)
int val2 = 0; //while value to store amount of times to repeat blink
volatile boolean f_wdt = 1;
void setup(){
setup_watchdog(9); // approximately 8 seconds sleep
}
void loop(){
if (f_wdt==1) { // wait for timed out watchdog / flag is set when a watchdog timeout occurs
f_wdt=0; // reset flag
pinMode(sense, INPUT);
delay(150); // delay a little to allow residual voltage bleed off
val = analogRead(sense); // read sense led
delay(50); // delay a little after reading and storing the value
if (val >= light) { //compare value read to value we put in "light".
digitalWrite(LED, LOW); //if its light out, do not turn on LED
}
else {
blink_run(); //if its dark, run Blink function
}
pinMode(LED,INPUT); // set all used port to intput to save power
system_sleep();
pinMode(LED,OUTPUT); // set all ports into state before sleep
}
}
void blink_run(){
pinMode(LED, OUTPUT);
val2 = 0; //reset val2 to 0
while(val2 < 30){ //how long to blink for, in cycles
digitalWrite(LED, HIGH); //turn LED on
delay(2); //Leave on for this amount of time
digitalWrite(LED, LOW); //turn LED off
delay(80); //leave off for this long
val2++; //Add 1 to the val2 int
}
}
// set system into the sleep state
// system wakes up when watchdog is timed out
void system_sleep() {
cbi(ADCSRA,ADEN); // switch Analog to Digital converter OFF
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable();
sleep_mode(); // System sleeps here
sleep_disable(); // System continues execution here when watchdog timed out
sbi(ADCSRA,ADEN); // switch Analog to Digitalconverter ON
}
// 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms
// 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec
void setup_watchdog(int ii) {
byte bb;
int ww;
if (ii > 9 ) ii=9;
bb=ii & 7;
if (ii > 7) bb|= (1<<5);
bb|= (1<<WDCE);
ww=bb;
MCUSR &= ~(1<<WDRF);
// start timed sequence
WDTCR |= (1<<WDCE) | (1<<WDE);
// set new watchdog timeout value
WDTCR = bb;
WDTCR |= _BV(WDTIE);
}
// Watchdog Interrupt Service / is executed when watchdog timed out
ISR(WDT_vect) {
f_wdt=1; // set global flag
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment