Last active
October 13, 2017 09:13
-
-
Save bogdanr/9cf92b924f3d03c737be438d1379dc0b to your computer and use it in GitHub Desktop.
Attiny13 flashlight fade in and out at button press
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/interrupt.h> | |
#include <avr/sleep.h> | |
#define BUTTON 4 | |
#define LED 1 | |
int dir = 1; | |
int lum = 0; | |
boolean toggle = false; | |
int vals [] = {0, 1, 2, 3, 5, 9, 12, 18, 22, 32, 44, 58, 76, 120, 163, 255}; | |
void setup() { | |
pinMode(BUTTON, INPUT); | |
pinMode(LED, OUTPUT); | |
// These two allow us to save ~25% of total energy | |
ADCSRA &= ~(1<<ADEN); //Disable ADC | |
ACSR = (1<<ACD); //Disable the analog comparator | |
} | |
void loop() { | |
if (digitalRead(BUTTON) == HIGH) { | |
toggle = true; | |
lum += dir; | |
if ((lum >= 0) && (lum < 16)) | |
analogWrite(LED, vals[lum]); | |
delay(100); | |
} else { // toggle direction | |
if (toggle) { | |
dir = -1 * dir; | |
toggle = false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment