#include #include #define F_CPU 8000000UL //8 MHz #include unsigned char hybyte=0; int result=0; unsigned char dutydata=0; unsigned char state=0; void adcread(); void random_delay(); int main() { cli();//disable gobal interrupts //pwm setup DDRB |= 0b00010000; //set pin 3 as output pb4 TCCR1 = 0b00000001; GTCCR = 0b01100000; //enable pwm OCR1B=0; //duty cycle //adc setup ADMUX = 0b00001111; //set adc reference and select channel to adc4 the internal temp sensor ADCSRA = 0b10000110; //enable adc and set presalcer to divide by 64 //watchdog WDTCR = 0b01010010; sei(); //enable global interrupts while(1){ ADMUX=0b00000001;//set adc to read from photoresistor on adc1, pin 7 adcread(); if(result < 25){ state=1; } if(result > 40){ state=0; dutydata=0; } if(state==1){ ADMUX=0b00001111;//set adc to read from channel 4, the internal temp sensor adcread(); while(result>255){ if(result>=512){ result=result-512; } if(result>=256){ result=result-256; } } srand(result); dutydata=rand()%255; random_delay(result); } } return 0; } ISR(WDT_vect){ OCR1B =dutydata; } void adcread(){ ADCSRA |= (1 << ADSC); // start ADC measurement while (ADCSRA & (1 << ADSC)==1){} // wait till conversion complete result = ADCL; hybyte = ADCH; while (hybyte > 0){ if (hybyte >=2){ result=result + 512; hybyte=hybyte-2; } if (hybyte >=1){ result=result+256; hybyte=hybyte-1; } } } void random_delay(int count){ while(count--){ _delay_ms(1); } }