Created
April 14, 2014 02:00
-
-
Save dwhacks/10610645 to your computer and use it in GitHub Desktop.
Blinking bike light.
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
/* | |
*Bike LED | |
*by DWHacks, using Watchdog sleep timer and Button State Change | |
*Coded for Attiny13a at 4.8mhz | |
*check out daynewaterlow.com for more projects and hacks | |
*/ | |
#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 | |
const int buttonPin = 1; // pushbutton pin | |
const int ledPin = 0; // LED output pin | |
// Variables will change: | |
int buttonPushCounter = 1; // counter for the number of button presses | |
int buttonState = 0; // current state of the button | |
int lastButtonState = 0; // previous state of the button | |
volatile boolean f_wdt = 1; | |
void setup(){ | |
setup_watchdog(8); // 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms | |
// 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec | |
} | |
void loop(){ | |
if (f_wdt==1) { // wait for timed out watchdog / flag is set when a watchdog timeout occurs | |
// initialize the button pin as a input: | |
pinMode(buttonPin, INPUT); | |
// initialize the LED as an output: | |
pinMode(ledPin, OUTPUT); | |
// read the pushbutton input pin: | |
buttonState = digitalRead(buttonPin); | |
// compare the buttonState to its previous state | |
if (buttonState != lastButtonState) { | |
// if the state has changed, increment the counter | |
if (buttonState == HIGH) { | |
// if the current state is HIGH then the button | |
// went from off to on: | |
buttonPushCounter++; | |
} | |
} | |
// save the current state as the last state, | |
//for next time through the loop | |
lastButtonState = buttonState; | |
//Full on baby | |
if (buttonPushCounter == 2){ | |
digitalWrite(ledPin, HIGH); | |
} | |
//Some On and Off | |
else if (buttonPushCounter == 3){ | |
digitalWrite(ledPin, HIGH); | |
delay(200); | |
digitalWrite(ledPin, LOW); | |
delay(150); | |
} | |
//Maybe a little Disco! | |
else if (buttonPushCounter == 4) { | |
int val = 0; //reset val to 0 | |
while(val < 10){ //how long to blink for, in cycles | |
digitalWrite(ledPin, HIGH); //turn LED on | |
delay(10); //Leave on for this amount of time | |
digitalWrite(ledPin, LOW); //turn LED off | |
delay(50); //leave off for this long | |
val++; //Add 1 to the val int | |
} | |
delay(200); //wait a bit extra | |
} | |
// Lets Go to Sleep | |
else if (buttonPushCounter == 1) { | |
digitalWrite(ledPin, LOW); | |
f_wdt=0; // reset sleep flag | |
pinMode(ledPin,INPUT); // set all used port to intput to save power | |
system_sleep(); | |
pinMode(ledPin,OUTPUT); // set all ports into state before sleep | |
} | |
//if we push the button more then 4 times, we go back to sleep. | |
else if (buttonPushCounter > 4){ | |
buttonPushCounter = 1; | |
} | |
} | |
} //end of LOOP | |
// set system into the sleep state | |
// system wakes up when wtchdog is timed out | |
void system_sleep() { | |
cbi(ADCSRA,ADEN); // switch Analog to Digitalconverter 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