Hey,
I am trying to work with to analog sensors connected to ATmega16. The first one is LM35 temperature sensor connected to PA0, the second one is photoresistor, connected to PA1. Also I have connected two LEDs to PB0 and PB1. LED1 should be ON when the temperature goes high and LED2 turns ON when the lighting is low. Here is a code I tryed, but it's not working:
I'm also uploading a schematis:
Can you help understand why the code is not working? Also you can share an example of similar code, or some instructions how can I do it..
I am trying to work with to analog sensors connected to ATmega16. The first one is LM35 temperature sensor connected to PA0, the second one is photoresistor, connected to PA1. Also I have connected two LEDs to PB0 and PB1. LED1 should be ON when the temperature goes high and LED2 turns ON when the lighting is low. Here is a code I tryed, but it's not working:
Code:
#include <avr/io.h>
#include <util/delay.h>
void InitADC()
{
ADMUX=(1<<REFS0); // Aref=AVcc;
ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); //Rrescalar div factor =128
}
uint16_t ReadADC(uint8_t ch)
{
ch=ch&0b00000111;
ADMUX&=0b11100000;
ADMUX|=ch;
ADCSRA|=(1<<ADSC);
while(!(ADCSRA & (1<<ADIF)));
ADCSRA|=(1<<ADIF);
return(ADC);
}
void main()
{
InitADC();
DDRB=0Xff;
uint16_t adc_result[2];
while(1)
{
adc_result[0]=ReadADC(0); // Read PA0
adc_result[1]=ReadADC(1); // Read PA1
if(adc_result[0]>60) //id it is more then 30 degrees
{ PORTB|=(1<<0); } //LED1 ON
else
{ PORTB &=~(1<<0);} //LED1 OFF
if(adc_result[1]<50) //light lower then 50
{ PORTB|=(1<<4); } //LED2 ON
else
{ PORTB &=~(1<<4);} //LED2 OFF
}
}
I'm also uploading a schematis:
Can you help understand why the code is not working? Also you can share an example of similar code, or some instructions how can I do it..