i built remote controlled switch and connected its output to PB0 in atmega16 microcontroller

then connected pir sensor with single trigger configuration for 30 sec and connected to PB1 in atmega16 

here is the circuit
[![enter image description here](https://i.stack.imgur.com/EKU1M.jpg)](https://i.stack.imgur.com/EKU1M.jpg)
my goal is to put pir sensor to detect that i entered my room so it trun on the light (this point is done in the code)

my problem in the RC switch i want it to toggle the status of PB1 so i can control turning the on and off using it 

i used this code 

```
/*
 * AVRGCC4.c
 *
 * Created: 7/9/2017 20:05:11
 *  Author: shadylap
 */ 

#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
    DDRA= 0b11111111;
    DDRB= 0b00000000;
    DDRC= 0b00000000;
    while(1)
    {

       if (PINB == 0b00000010)
       {
           _delay_ms(500);
           PORTA = 0b00000001;

       }

       if (PINB == 0b00000001)
       {
           _delay_ms(1000);
           PORTA &= ~ (1<<PA0);
       }

          }
    return 0 ;
}

```

when i switch on IR switch the led keeps turning on and off but i want when i turn IR switch on led toggles its state once only

new update ..................................................................... i used this code but its not working in the Proteus simulation although it was working before why ?? 

```
    #define F_CPU 1000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

int main(void)
{
    DDRA=0b11111111;
    DDRB=0b00000000;
    DDRD=0b00000000;

    MCUCR |= (1<<ISC00);
    GICR |= (1<<INT0);
    sei();

    while(1)
    {
    if (PINB == 0b00000010)
    {
        PORTA = 0b00000001;
        _delay_ms(500);
    }
        return 0;
    }

}

ISR(INT0_vect){

        PORTA ^=(1<<PA0);

    }  

```