# i need atmega 16 output to toggle using switch

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 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
*/

#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);

}

• Please explain what is and what not working with your new code and what wiring changes you made to operate with interrupts. Just saying it doesn't work is lazy. – Glenn W9IQ Jul 18 '17 at 12:53
• first of all i want to thank you very much for helping me i appreciate it , i need to do more searching and reading , my last question is there any thing wrong in the updated code thank you again :) – Shady Mohamed Jul 18 '17 at 16:08
• Ok good luck with the project. Open a new question if you need more assistance. – Glenn W9IQ Jul 18 '17 at 16:10

Your description of the desired functionality is a bit ambiguous.

I will assume the following. If this is not correct, then indicate so in the comments to this answer and I will adjust it accordingly.

Normal operation is that the PIR sensor determines if the LED is on or off. However, through an infrared remote control, you would like to toggle the state of the LED. You would like the LED to remain in that state until the next PIR or IR control event.

Your current code is constantly examining the level of the inputs and changing the output based on this information, this is called level sensing. Instead, your code should perform edge triggering. This is where the code takes some action only when the state of the input changes from low to high or high to low, as appropriate.

The basic technique for performing edge detection is to save the value of the input bit each time and the next time the input bit is read, compare it to its previous value. If it is not the same, it has just changed states (you detected the edge of the pulse). You can now take appropriate action based on the direction of the change. This entire mechanism will add only a half dozen or so lines of code to your program.

Here is some pseudo code that catches as rising edge of a port bit input:

IR_MASK = 0b00000001;
if (IR_INPUT ^ IR_LAST)            /* if true, we caught an edge (change)
{
IR_LAST = IR_INPUT;           /* save the new IR input bit value
if (IR_INPUT);
{
/* put any rising edge code here
else {
/* put any falling edge code here
}
}

• Thank you sir for your answer. but do you have like example so I can apply your idea – Shady Mohamed Jul 17 '17 at 11:43
• I had an idea to use for loop triggered by remote switch to toggle status of light only once let me try it first – Shady Mohamed Jul 17 '17 at 11:51
PORTA &= ~ (1<<PA0); is a toggle. And your code polls the input instead using an interrupt. So every time that PinB = 1, it will toggle PA0.