Why is my code working in proteus but not when burned into chip?

Thread Starter

DeanBA

Joined Dec 1, 2022
2
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:
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 );
    }
}
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.
 

MrChips

Joined Oct 2, 2009
27,674
Welcome to AAC!

What Atmel chip are you using?
What SW platform are you running?
What is the code ( 1<<0 ) and ( 1 <<1 ) supposed to accomplish?
 

Thread Starter

DeanBA

Joined Dec 1, 2022
2
Welcome to AAC!

What Atmel chip are you using?
What SW platform are you running?
What is the code ( 1<<0 ) and ( 1 <<1 ) supposed to accomplish?
Thank you!
I am using Atmega16. I am writing my code in Atmel studio and burning onto chip with AVEDUDESS.
I should name those pins, but there is comment by every important line.
 

MrChips

Joined Oct 2, 2009
27,674
Thank you!
I am using Atmega16. I am writing my code in Atmel studio and burning onto chip with AVEDUDESS.
I should name those pins, but there is comment by every important line.
So you are now learning how to program. You can create more than ten ways to do the same thing.
There is a huge number of tips and best practices you have to pick up on your journey.
As they say, when you come upon a fork on the road, take it.

Tip #1 - Make your code readable so that commenting becomes redundant.
Tip #2 - The simplest solution is often the best solution.

On to specifics:

Tip #3
(1 << 0) evaluates to 1. If you need 1 then why not use 1?
(1 << 1) evaluates to 2. If you need 2 then why not use 2?

Tip #4
Why use x when you can use number_of_persons?

Tip #5
If (condition) evaluates condition to give a boolean result.
On most compilers zero is FALSE and non-zero is TRUE.
Thus the value of x or number_of_persons will evalute to FALSE if zero and TRUE if non-zero.

Tip #6
Assign real names to your objects and associate them with I/O pins.
Make good use of #define statements.

#define FALSE 0
#define TRUE 1
#define OFF 0
#define ON 1

[This part would be too advanced for you. Hence skip over this.]
#define LED PORTB_bit.P0
#define IN_SENSOR PINC_bit.P0
#define OUT_SENSOR PINC_bit.P1

To turn on the LED, one would write
LED = ON;
To turn off the LED, would write
LED = OFF;

Your problem is related to the fact that your code executes in an endless loop and cannot tell which sensor is interrupted first. Your sensors are checked 1-2-1-2-1-2-1-2-1...

In order to determine direction of motion you need to implement how quadrature encoders operate.

1669942518728.png
 
Top