Visitor Counter using PIC Microcontroller

Thread Starter

farahinesa

Joined May 28, 2012
6
im doing a project that use PIC microcontroller 18F452 to count visitor using C programming. the project consist of two sensor (sensor1 and sensor2). when the person enters the door, sensor1will detect it, followed by sensor2. then when the person leave the room, sensor2 will detect it 1st followed by sensor1. hence, i can calculate the number of people entering and leaving the room. but im facing a problem. my problem is what if a really big person stand in between both of the sensors for a certain period of time. my program will keep counting up although there is nobody leaving or entering the room. so how should i solve this problem.
 

DumboFixer

Joined Feb 10, 2009
217
Instead of working on sensor1 is high and sensor2 is high to detect somebody entering the room how about just looking for a leading edge of the sensor signal ?

So if they enter the room you will get a leading edge on sensor1 then a leading edge on sensor2. If they stop as you suggest with both sensors triggered you will still only get 1 leading edge signal from each sensor.
 

Thread Starter

farahinesa

Joined May 28, 2012
6
Rich (BB code):
//Define port
#define sensor1 portb.f0
#define sensor2 portb.f1
#define error portb.f7
#define relay portb.f6

//Define LCD port

sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D0 at RD0_bit;
sbit LCD_D1 at RD1_bit;
sbit LCD_D2 at RD2_bit;
sbit LCD_D3 at RD3_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D0_Direction at TRISD0_bit;
sbit LCD_D1_Direction at TRISD1_bit;
sbit LCD_D2_Direction at TRISD2_bit;
sbit LCD_D3_Direction at TRISD3_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;


void introduction(void);
void displayLCD(int y);
void main() {
int count=0,count1=0,txt[7];
trisb=0b00000011;
trisc=0x30;
trisd=0;

portb=0;
adcon1=6;
lcd_init();
introduction();

 while(1){

         if  (sensor1==0){
             while (count<10){
                   if(sensor2==0){ count=13; delay_ms(500); count1++;displayLCD(count1);}
                   else{ delay_ms(50);count++;}
                             }
             while((count==10)&&(sensor1==0))
             {error=1;}
             if(sensor1==1){error=0;}
             }
         else if(sensor2==0){
              while (count<10){
                    if(sensor1==0){
                    count=13;delay_ms(500); count1--;displayLCD(count1);
                    }
                    else{
                    delay_ms(50);count++;
                    }
             }
             while((count==10)&&(sensor2==0))
             {error=1;}
             if(sensor2==1){error=0;}


             }

       if(count1>0){relay=1;}
       if(count1==0){
       relay=0;
       }
       count=0;
       
       if(count1>=5){
                    Lcd_Cmd(_LCD_CLEAR);
                    Lcd_Out(1,1,"     Full     ");
                     Lcd_Out(1,11,"Count:");
                    IntToStr(5,txt);
                    Lcd_Out(2,5,txt);
                    delay_ms(90);
                     count1=5;
                    }
       else if(count1<0){count1=0;}
}    }
void introduction(void)
{
unsigned char i;

Lcd_Cmd(_LCD_CLEAR);

Lcd_Out(1,1,"     WELCOME     ");
Lcd_Out(2,1,"  SELAMAT DATANG ");
delay_ms(2000);


for(i=0; i<16; i++) {             // Move text to the left 16 times
      Lcd_Cmd(_LCD_SHIFT_LEFT);
      delay_ms(80);
    }
Lcd_Cmd(_LCD_CLEAR);

}
void displayLCD(int y)
{ char txt[7];

 IntToStr(y,txt);
Lcd_Cmd(_LCD_CLEAR);
 Lcd_Out(1,1,"Count:");
 Lcd_Out(1,11,txt);
 Lcd_Out(2,11,"per 30");

 
 }
this is my coding..this is just a demo...im stuck when both sensor detect, my counter will keep counting..

i understand if i use leading edge but i have no idea how to do it..
please help me..
 
Last edited by a moderator:

MMcLaren

Joined Feb 14, 2010
861
... my problem is what if a really big person stand in between both of the sensors for a certain period of time. my program will keep counting up although there is nobody leaving or entering the room. so how should i solve this problem.
Consider generating an electric shock to move the person out from in-between the sensors. Of course this may potentially reduce the amount of traffic you're counting.
 

absf

Joined Dec 29, 2010
1,968
Assuming you're using 16F877A. Read descriptions on P21 of the 877A datasheet on SFR "OPTION_REG" and "INTCON". You may use RA4/TOCKI or RB0 or RB4..7 as inputs for your sensor. A pulse detected can be configured as leading or trailing edge as described in the 2 registers above.

An interrupt would be generated when one of the above is detected. You have to write an ISR to handle them.

Allen
 
Last edited:

Thread Starter

farahinesa

Joined May 28, 2012
6
i never know about ISR.
so do i need to alter my programming or i just need a new connection on my PIC..?
how can RA4/TOCKI set as input for sensor?
sorry..i'm a beginner..there's a lot of thing i don't understand.. :(
 

Thread Starter

farahinesa

Joined May 28, 2012
6
please help me.. i need to submit my project next week... :(

i dont understand how to do it...can anyone please explain further...
 

absf

Joined Dec 29, 2010
1,968
Why can't you place the 2 sensors further apart so no person is fat enough to block both sensors and also make sure one person can go in or out at one time. That'll solve your problem for such a limited time.

Understanding how to program using interrupt and redesigning your program using edge detection would most probably take longer time.

Allen
 

maxpower097

Joined Feb 20, 2009
816
What about using the timer to say if the time goes over .5 seconds count it as 1 until the sensors clear, then reset the counter to count again. A timer subroutine should be pretty easy to implement. Just make sure their a break between crowds going in and make em go in single file. Shouldn't be a problem.
 
Top