Visitor Counter using PIC Microcontroller

Discussion in 'Embedded Systems and Microcontrollers' started by farahinesa, May 28, 2012.

  1. farahinesa

    Thread Starter New Member

    May 28, 2012
    6
    0
    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.
     
  2. DumboFixer

    Active Member

    Feb 10, 2009
    219
    34
    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.
     
    maxpower097 likes this.
  3. farahinesa

    Thread Starter New Member

    May 28, 2012
    6
    0
    Code ( (Unknown Language)):
    1.  
    2. //Define port
    3. #define sensor1 portb.f0
    4. #define sensor2 portb.f1
    5. #define error portb.f7
    6. #define relay portb.f6
    7.  
    8. //Define LCD port
    9.  
    10. sbit LCD_RS at RB4_bit;
    11. sbit LCD_EN at RB5_bit;
    12. sbit LCD_D0 at RD0_bit;
    13. sbit LCD_D1 at RD1_bit;
    14. sbit LCD_D2 at RD2_bit;
    15. sbit LCD_D3 at RD3_bit;
    16. sbit LCD_D4 at RD4_bit;
    17. sbit LCD_D5 at RD5_bit;
    18. sbit LCD_D6 at RD6_bit;
    19. sbit LCD_D7 at RD7_bit;
    20.  
    21. sbit LCD_RS_Direction at TRISB4_bit;
    22. sbit LCD_EN_Direction at TRISB5_bit;
    23. sbit LCD_D0_Direction at TRISD0_bit;
    24. sbit LCD_D1_Direction at TRISD1_bit;
    25. sbit LCD_D2_Direction at TRISD2_bit;
    26. sbit LCD_D3_Direction at TRISD3_bit;
    27. sbit LCD_D4_Direction at TRISD4_bit;
    28. sbit LCD_D5_Direction at TRISD5_bit;
    29. sbit LCD_D6_Direction at TRISD6_bit;
    30. sbit LCD_D7_Direction at TRISD7_bit;
    31.  
    32.  
    33. void introduction(void);
    34. void displayLCD(int y);
    35. void main() {
    36. int count=0,count1=0,txt[7];
    37. trisb=0b00000011;
    38. trisc=0x30;
    39. trisd=0;
    40.  
    41. portb=0;
    42. adcon1=6;
    43. lcd_init();
    44. introduction();
    45.  
    46.  while(1){
    47.  
    48.          if  (sensor1==0){
    49.              while (count<10){
    50.                    if(sensor2==0){ count=13; delay_ms(500); count1++;displayLCD(count1);}
    51.                    else{ delay_ms(50);count++;}
    52.                              }
    53.              while((count==10)&&(sensor1==0))
    54.              {error=1;}
    55.              if(sensor1==1){error=0;}
    56.              }
    57.          else if(sensor2==0){
    58.               while (count<10){
    59.                     if(sensor1==0){
    60.                     count=13;delay_ms(500); count1--;displayLCD(count1);
    61.                     }
    62.                     else{
    63.                     delay_ms(50);count++;
    64.                     }
    65.              }
    66.              while((count==10)&&(sensor2==0))
    67.              {error=1;}
    68.              if(sensor2==1){error=0;}
    69.  
    70.  
    71.              }
    72.  
    73.        if(count1>0){relay=1;}
    74.        if(count1==0){
    75.        relay=0;
    76.        }
    77.        count=0;
    78.        
    79.        if(count1>=5){
    80.                     Lcd_Cmd(_LCD_CLEAR);
    81.                     Lcd_Out(1,1,"     Full     ");
    82.                      Lcd_Out(1,11,"Count:");
    83.                     IntToStr(5,txt);
    84.                     Lcd_Out(2,5,txt);
    85.                     delay_ms(90);
    86.                      count1=5;
    87.                     }
    88.        else if(count1<0){count1=0;}
    89. }    }
    90. void introduction(void)
    91. {
    92. unsigned char i;
    93.  
    94. Lcd_Cmd(_LCD_CLEAR);
    95.  
    96. Lcd_Out(1,1,"     WELCOME     ");
    97. Lcd_Out(2,1,"  SELAMAT DATANG ");
    98. delay_ms(2000);
    99.  
    100.  
    101. for(i=0; i<16; i++) {             // Move text to the left 16 times
    102.       Lcd_Cmd(_LCD_SHIFT_LEFT);
    103.       delay_ms(80);
    104.     }
    105. Lcd_Cmd(_LCD_CLEAR);
    106.  
    107. }
    108. void displayLCD(int y)
    109. { char txt[7];
    110.  
    111.  IntToStr(y,txt);
    112. Lcd_Cmd(_LCD_CLEAR);
    113.  Lcd_Out(1,1,"Count:");
    114.  Lcd_Out(1,11,txt);
    115.  Lcd_Out(2,11,"per 30");
    116.  
    117.  
    118.  }
    119.  
    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: May 28, 2012
  4. MMcLaren

    Well-Known Member

    Feb 14, 2010
    759
    116
    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.
     
  5. farahinesa

    Thread Starter New Member

    May 28, 2012
    6
    0
    hooo? electric shock? .......
     
  6. absf

    Senior Member

    Dec 29, 2010
    1,493
    372
    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: May 28, 2012
  7. farahinesa

    Thread Starter New Member

    May 28, 2012
    6
    0
    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.. :(
     
  8. farahinesa

    Thread Starter New Member

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

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

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
  10. absf

    Senior Member

    Dec 29, 2010
    1,493
    372
    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
     
  11. farahinesa

    Thread Starter New Member

    May 28, 2012
    6
    0
    urrm...... thats true.... mybe i can discuss with my team consider bout this problem..
    thankz :)
     
  12. maxpower097

    Well-Known Member

    Feb 20, 2009
    795
    388
    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.
     
Loading...