I want to work with two analog sensors connected to ATmega16. The first one is the temperature sensor (LM35), connected to PA0. The LED should be ON when the temperature goes high. The second one is the photoresistor and it's connected to PA1. I want to turn the LED ON when the lighting is low.
I was looking for a solution all over the internet, but couldn't find something similar to my situation. They all just say one thing- you can't read two analog sensors at the same time.
I have this code that works just for one sensor.
So, you want the LED to turn on if either the light is low or the temperature is high? That's an OR function.
And, to add to what Klaus, and, apparently, the rest of the universe is telling you, the ATMega can only read one AD channel at a time. But I don't see why this is a problem.
Yeah, I want to switch between those two sensors constantly.
This code works just for one senor, when I try to adjust it the program gives weird results or nothing at all.
I tried something like this:
Code:
while (1) {
switch (ADMUX)
{
case 0x40:
value=ADC_Read(0);
if (value > 90)
{PORTB|=(1<<0);}
else
{ PORTB &=~(1<<0);}
break;
case 0x41:
value=ADC_Read(1);
if (value < 70)
{PORTB|=(1<<4);}
else
{ PORTB &=~(1<<4);}
break;
}
}
This gives us no information.
Please tell us exactly what you expect to see and what you see instead.... And your test setup.
Code: It´s a snippet (incomplete). Nobody of us can know what ADMUX is, and who or what controls ADMUX.
Nobody knows your schematic.
And please mind: You don´t spend time to add comments to your code, so why should anybody spend time to solve this riddle about the function of your code.
And comments are not only for us, if you later want to add some code or change something in your code .. you will urgently need some comments.
Take one thing at a time. Integrate it to the end then move to the next one. For example, if you start with a temperature sensor, you will find so many solutions for it on the internet. Focus on it and ensure that it works well, then move to the next thing. Thanks.
I tried to switch from 0x40 to 0x41 in the case loop.
Let me upload the schematic, it may be helpful.
I forgot to write the main loop code, where I intitialized PORTA and PORTB:
DDRA=0x0;
ADCSRA = 0x87;
DDRB=0xFF;