Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Attiny13 flashlight fade in and out at button press
#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
You can’t perform that action at this time.