/* * SmartLED.c * * Created: May 2016 * Author: Mar.lux * * * Device: Atmel ATTiny25/45/85(V) * * Power consumption (3V): * * Standby: 0.1 uA * Active (no LED): 670 uA * Active (with LED, bright light): approx. 5.7 mA * Active (with LED, darkness): approx. 700 uA * */ #include #include #include #include #define LED PB4 #define SNS PB3 #define MUXCONFIG ((1 << MUX1) | (1 << MUX0)) // PB3 #define ENON PB0 #define ENBLINK PB1 #define ENFLASH PB2 #define BASELINE 9 // discharge in complete darkness volatile uint8_t Discharge = 0; volatile uint16_t DischargeSum = 0; volatile uint8_t level = 8; volatile uint8_t factor = 0; volatile uint8_t TimerCounter = 0; volatile uint8_t LightDuty = 0xff; // Fraction of time the LED is on (0xff: always, 0x80 blinking, 0x0f flashing) void initDevice(); int main(void) { /* Replace with your application code */ initDevice(); //PORTB |= (1<> 4; // smooth out noise in the ADC conversion //Discharge = (DischargeSum + 128) >> 8; DischargeSum = DischargeSum - Discharge + (255-ADCH); if (Discharge > BASELINE) Discharge -= BASELINE; else Discharge = 0; uint8_t shift = 0; if (Discharge > 31) shift = 1; if (Discharge > 63) shift = 2; if (Discharge > 127) shift = 3; factor = 1 << shift; level = ((Discharge >> shift)+1) << 2; } EMPTY_INTERRUPT(PCINT0_vect); // Pin change interrupt: Do nothing, just wake up