/* * main.c * * A small program for the ATtiny44a to keep LEDs on for a certain amount of time * after hearing a noise. * * Created for the "Lighten Up Your Room Interior With A Summer Touch" instructable. * * Created on: 9 Mar 2012 * Author: d3h */ #define ON_FOR 300 // time in seconds to keep the lights on after hearing a noise #define NOISE_THRESHOLD 25 // noise picking threshold #define NOISE_DURATION 2 // minimum duration of the noise to be picked #include #include volatile unsigned char secondsCounter = 0; volatile unsigned char noiseCounter = 0; int main (void) { unsigned char noiseHeard = 0; // 1 if noise heard, 0 otherwise // ADC initialisation ADMUX |= (1 << REFS1); // set ADC reference to 1.1V ADCSRB |= (1 << ADLAR); // left adjust ADC result to allow easy 8 bit reading // no MUX values needed to be changed to use ADC0 (PA0) for ADC input // set conversion timing to Fcpu/16, auto-triggering on, enable ADC interrupt // enable ADC, start A2D conversions ADCSRA |= (1 << ADPS2) | (1 << ADATE) | (1 << ADIE) | (1 << ADEN) | (1 << ADSC); // timer initialisation TCCR1B |= (1 << WGM12); // configure timer 1 for CTC mode TIMSK1 |= (1 << OCIE1A); // enable CTC interrupt OCR1A = 15624; // set CTC compare value to 1Hz at 1MHz AVR clock, with a prescaler of 64 TCCR1B |= (1 << CS10)|(1 << CS11); // set up timer at Fcpu/64 sei(); // enable global interrupts DDRA |= (1 << DDA2); // set PA2 as output PORTA &= (0 << PA2); // set LEDs to be initially OFF for (;;) { if (noiseHeard && secondsCounter >= ON_FOR) { // no noise was heard for ON_FOR seconds, switch LEDs OFF PORTA &= (0 << PA2); // set the LEDs OFF noiseHeard = 0; } if (noiseCounter >= NOISE_DURATION) { // set the LEDs ON (or keep them on) for ON_FOR seconds PORTA |= (1 << PA2); noiseHeard = 1; secondsCounter = 0; // start counting ON_FOR seconds noiseCounter = 0; // keep listening for further sounds } } return 1; // should never reach here } // Interrupts ISR(TIM1_COMPA_vect) { // fires when the timer ticks secondsCounter++; } ISR(ADC_vect) { // fires when an ADC conversion occurs if (ADCH >= NOISE_THRESHOLD) noiseCounter++; else noiseCounter = 0; }