0
\$\begingroup\$

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

    }  
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Glenn W9IQ Jul 18 '17 at 12:53
  • \$\begingroup\$ 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 :) \$\endgroup\$ – Shady Mohamed Jul 18 '17 at 16:08
  • \$\begingroup\$ Ok good luck with the project. Open a new question if you need more assistance. \$\endgroup\$ – Glenn W9IQ Jul 18 '17 at 16:10
0
\$\begingroup\$

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;
IR_INPUT = (PINB & IR_MASK);       /* mask off the other bits
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
     }
}
\$\endgroup\$
  • \$\begingroup\$ Thank you sir for your answer. but do you have like example so I can apply your idea \$\endgroup\$ – Shady Mohamed Jul 17 '17 at 11:43
  • \$\begingroup\$ I had an idea to use for loop triggered by remote switch to toggle status of light only once let me try it first \$\endgroup\$ – Shady Mohamed Jul 17 '17 at 11:51
  • \$\begingroup\$ I added some pseudo code to my answer to help you out. \$\endgroup\$ – Glenn W9IQ Jul 17 '17 at 14:40
  • \$\begingroup\$ this code is very useful it could work but i am thinking of using interrupt if connected the output of the circuit in this page embedded-lab.com/blog/… to microcontroller interrupt pin instead of relay i think i will not face the debounce problem because its ouput of transistor am i right?? \$\endgroup\$ – Shady Mohamed Jul 17 '17 at 15:36
  • \$\begingroup\$ You should not need debounce with a transistor output. So an interrupt based routine would work as well. \$\endgroup\$ – Glenn W9IQ Jul 17 '17 at 15:40
0
\$\begingroup\$

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.

Solution. Use an interrupt instead of polling. Or a simpler version, use an if else or two ifs or something.

Pseudocode: If pb0 = 1, turn on led. If pb0 = 0, turn off.

\$\endgroup\$
  • \$\begingroup\$ concerning the interrupt i couldnt use it because of debounce problem but here i use this circuit <img src="embedded-lab.com/blog/wp-content/uploads/2011/07/…" width="100" height="100"> \$\endgroup\$ – Shady Mohamed Jul 17 '17 at 15:23
  • \$\begingroup\$ You can handle denounce with code or with hardware or both. \$\endgroup\$ – Passerby Jul 17 '17 at 15:29
  • \$\begingroup\$ concerning interrupt if connected the output of the circuit in this page embedded-lab.com/blog/… to microcontroller interrupt pin instead of relay i think i will not face the debounce problem because its ouput of transistor am i right?? \$\endgroup\$ – Shady Mohamed Jul 17 '17 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.