(atmega 16 )turning off mains when no one is inside after a delay

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

what my code should do is, whenever someone enters and he is the first one to be inside that time mains should turn on so tat he can use the  fan, light ,etc. I am using 2 ir sensors to detect if someone is entering or leaving the room.Once everyone leaves the room the microcontroller should wait for a certain delay so tat it wud b better to not keep turning off mains if ppl keep coming in and going out so tats d reason y i want to turn off mains wen all leave the room and wen no one enter even after tat specific delay. I am using 555 timer for timing and parallelly keep checking if someone entered. Wen i run the code n i keep hand to just giv a sign about a person entering and leaving and make count zero it directly turns off the mains without d delay. I am unable to understand how the timer function is getting skipped. Pls help me

 

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

int count=0,start_timer=0,ir_check,timer_op,OutOfTime=0;
int timer(void);

int main(void)
{

		DDRC=0x00;  //port A input for ir sensor values
		DDRD=0xff;	//for a relay which is used to switch from vcc to ground and vice versa
		PORTD |= (1<<PD5);  //trig pin is made high
		while(1)
		{
			ir_check = PINC & (0x03); //to read which ir is read and count is manipulated based on tat

			if((ir_check==1))
			{
				_delay_ms(50);
				ir_check = PINC & (0x03);
				if(ir_check==2)
				{
					count+=1;  //1 person entered
				}
			}
			else
			if((ir_check==2))
			{
				_delay_ms(50);
				ir_check = PINC & (0x03);
				if(ir_check==1){
				count-=1;	//1 person left
				}
			}
			if((count==0)&&(start_timer==0)){
				timer();  //none inside so start timer
			}
			else
			{
				PORTD |= (1<<PD6);//relay on
			}

			if(count!=0)
			{
				start_timer=0; //if someone entered then it should not turn off relay
			}

			OutOfTime=(PINC & (0x04)); // to read if 555 delay of X sec is over and output of is low
			if(OutOfTime==4){
				OutOfTime=0;
			}else
			{
				OutOfTime=1;  //timer crossed the designed T(ON)
			}

			if((OutOfTime==1)&&(count==0)&&(start_timer==1))
			{
				PORTD &= ~(1<<PD6);   //relay off
				start_timer=0;
			}
			_delay_ms(100);
		}
		return 0;

}

int timer(void)
{
	PORTD |= (1<<PD5);	//send 1 to 555 timer trig
	PORTD &= ~(1<<PD5);	//send 0 to trig
	_delay_ms(10);		//delay ms for hold time for 555 to detect trigger
	PORTD |= (1<<PD5);	//send 1 to trig
	start_timer = 1;
	return 0;
}

 

Last Edited: Wed. Sep 20, 2017 - 06:41 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

You already have a thread on this: http://www.avrfreaks.net/forum/avr-automatic-turn-power-mains-using-timer

 

prostriker wrote:
Pls help me and i am in a hurry to complete this

http://www.catb.org/~esr/faqs/sm...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

tats my thread but this is a different code than the previous one so i just made a new post

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

it's the same topic - stick with one thread, and pay attention to the advice that has already been given there - in particular, #10

 

 

Topic locked