boolean on = true; // whether the display is 'on' or not
boolean dirtee = false; // is the EEPROM brightness wrong?
void setBrightness(uint8_t b) {
OCR1A = b;
}
void setup() {
digitalWrite(led, LOW);
pinMode(led, OUTPUT);
// way faster than analogWrite, 15.625Khz!
OCR1C = 255;
OCR1A = 0;
TCCR1 = _BV(CS10) | _BV(CS11) | _BV(PWM1A) | _BV(COM1A1);
// read the eeprom location 0!
brightness = EEPROM.read(0);
// slowly fade up!
for (uint8_t i=0; i < brightness; i++) {
setBrightness(i);
delay(10);
}
pinMode(upbutton, INPUT);
digitalWrite(upbutton, HIGH);
pinMode(downbutton, INPUT);
digitalWrite(downbutton, HIGH);
pinMode(onoffbutton, INPUT);
digitalWrite(onoffbutton, HIGH);
}
void loop() {
if (on) {
while (! digitalRead(downbutton)) {
// Don't let it get dimmer than 2/255
if (brightness > 2) {
brightness --;
setBrightness(brightness);
dirtee = true;
}
delay(10);
}
while (! digitalRead(upbutton)) {
if (brightness != 255) {
brightness ++;
setBrightness(brightness);
dirtee = true;
}
delay(10);
}
// once they release the button, write the new brightness to EEPROM
if (dirtee) {
EEPROM.write(0, brightness);
dirtee = false;
}