IR sensor + LDR + LED

Discussion in 'Embedded Systems and Microcontrollers' started by izaty, Oct 21, 2009.

  1. izaty

    Thread Starter New Member

    Oct 20, 2009
    4
    0
    Hi all,


    I am doing a sensor project. I'm used LDR to detect the sunlight and Ir sensor to detect the movement of the human body..

    I have 2 conditions in order to ON the LED.
    First: LDR must detect the room is dark
    Second: Ir sensor detect the user
    Result: Counter increase the number of user + ON the LED.

    LDR detect the room is dark + Ir sensor detect the user --> Counter increase ( cnt++) -->= ON the light.
    Otherwise,

    LDR detect the room is light + Ir sensor detect the user --> Counter ++, but the light still OFF.


    Then, to exit the room...

    Led ON --> user pass by the sensor+press the exit button+count-- (if count == 0 --> OFF the light.


    But, i have a problem... i'm using the coding below...
    Hope someone can help me on this coding.


    Tq.


    /* =======================
    INFRA_RED
    KiT = Chip-877
    CPU = PIC16F877
    LANG = MikroC
    XTAL = 20Mhz
    STATUS = V2 = join InfraRED with 7-SEG counting

    ======================= */

    #define DOOR1_IN PORTC.F1
    #define DOOR2_OUT PORTC.F2

    // ========================
    /* 7 6 5 4 3 2 1 0
    PORTA X X g f DP c b a
    PORTE X X X X X X d e */

    /* C_Cathode PA0 = a
    PA4 = f PA1 = b
    PA5 = g PA2 = c
    PE0 = e PA3 = dp
    PE1 = d NC */
    const char
    SLED_A[10]={0x17,0x06,0x23,0x27,0x36,0x35,0x35,0x07,0x37,0x37};
    const char SLED_E[10]={0x03,0x00,0x03,0x02,0x00,0x02,0x03,0x00,0x03,0x02};
    // ========================


    void main(){
    char cnt, ten, exit, value;

    TRISC = 0x0F;
    TRISB = 0; /* set PORTB = ALL outputs */
    PORTB = 0; /* turn off ALL LEDs */

    // =====================================
    TRISA = 0; /* set PORTA as ALL outputs */
    TRISE = 0; /* set PORTE as ALL outputs */
    PORTA = 0b11000000;
    PORTE = 0b11111100;

    cnt = 0;
    ten = 0;

    exit = 1; // exit button is high --> no press
    value = 1; // LDR detect the room is light

    // =====================================

    for( ;; ){

    value = PORTE.F0;;

    if (value == 0) // if LDR detect the room is dark
    {
    cnt++;
    }
    else
    {
    cnt--;

    }

    if ( cnt != 0 ) PORTB = PORTB | 0x08;

    PORTA = SLED_A[cnt];
    PORTE = SLED_E[cnt];

    Delay_ms(250);

    PORTA = 0b11000000;
    PORTE = 0b11111100;

    exit = PORTD.F1;; // exit button

    if ( !DOOR1_IN ){ // if user x pass by the sensor
    PORTB = 0x01; // light remain ON

    if ( exit == 0 ) cnt--; // if user press exit button --> exit==0
    else cnt++; // cnt still increase

    }

    else if ( DOOR1_IN ) PORTB = 0x00;

    if ( cnt == 10 ){ cnt = 0;
    ten = 1; }

    if ( cnt == 0xFF ) cnt = 0;

    }
    }
     
  2. ambitious

    New Member

    Apr 23, 2009
    1
    0
    Hi iZaty,
    I think from reading your explanation that you wanna to Turn the light ON when there is/are user(s) and the room is light , So you don't have to use the Cnt.
    Just when detecting user(s) by the IR sensor, check the LDR status , if the LDR sens that the room is lighting Turn the light ON otherwise stay the room light OFF.

    Otherwise Turn OFF the room light if the IR sensor does not detect any user in the area.

    But if u wanna to know how many users are in the room and Turn the room ON and display the users number, you have to use two sensors but not IR sensor to detect the user direction (enter or Exit) not by using exit button

    Best regards
    MHamed
     
Loading...