I am using atmel chip and I have a problem with the code, I guess. Trying to make automatic light system. I am working with two IR sensors that should detect a person entering and leaving the room. I have connected them to PC0 and PC1. The LED is connected to PB0 and should be ON when someone enters the room, and OFF when room is empty. When I tried it in proteus it worked fine, but now the chip doesn't give the same results. LED goes ON when sensors detect the person, but then wont go off whatever I do. The code is compiled with no error, it's just not working in the way I want to.
The code:
have connected LCD so I could track the counter. Turns out counter always incresses by double, so it goes like this 0 2 4 6 8 10 and so on. When I put an object in front of second sensor counter goes to 1, and when I remove the object it goes to 2. Regardless of which sensor detects the object first, counter always increases when second sensor is triggered. And it never decreases. It's like the first sensor is not even working.
So it looks like this:
Person enters the room, count=2
Person leaves the room, count=4
Person enters, count=6
Person leaves, count=8
Does anyone have an idea why this is happening with the sensors?
Please keep in mind I am a student, beginner at this field, any suggestion will be helpful.
The code:
C:
void LED( uint8_t x )
{
if ( x == 0 ) //if no persons in the room
{
PORTB &= ~( 1<<0 ); // LED OFF
}
else
{
PORTB |= ( 1<<0 ); // LED ON
}
}
int main( void )
{
DDRB = 0xFF; // LED
DDRC = 0x00; // IR sensors
uint8_t count = 0;
while (1)
{
if ( PINC & ( 1<<0 ) ) //check if first sensor detected
{
// entrance
while ( ! ( PINC & ( 1<<1 ) ) ); // waits for the second to detect
while ( PINC & ( 1<<1 ) ); // second sensor stops detecting
++count;
}
else if ( PINC & ( 1<<1 ) ) // second sensor detects first
{
// exit
while ( ! ( PINC & ( 1<<0 ) ) );
while ( PINC & ( 1<<0 ) );
--count;
}
LED( count );
}
}
So it looks like this:
Person enters the room, count=2
Person leaves the room, count=4
Person enters, count=6
Person leaves, count=8
Does anyone have an idea why this is happening with the sensors?
Please keep in mind I am a student, beginner at this field, any suggestion will be helpful.