Skip to content

Instantly share code, notes, and snippets.

@bogdanr
Last active October 13, 2017 09:13
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 bogdanr/9cf92b924f3d03c737be438d1379dc0b to your computer and use it in GitHub Desktop.
Save bogdanr/9cf92b924f3d03c737be438d1379dc0b to your computer and use it in GitHub Desktop.
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