software based AMBIENT light detection

Discussion in 'Embedded Systems and Microcontrollers' started by fantabulous68, Aug 20, 2009.

Thread Status:
Not open for further replies.
  1. fantabulous68

    Thread Starter Active Member

    Nov 3, 2007
    51
    4
    /*generating 40KHz low duty cycle pulses which will drive an Infrared transmitter. When an object is
    infront of the infrared transmitter then the receiver IC will detect the object and an LED will GLOW.

    i want the project to be immune to ambient light such as a desk lamp.
    1)when i shine light on the transmitter i want the led to be off.
    2)when there is no object infront of the transmitter i want the led to be off.
    3)when there is an object infront of the transmitter i want the led to glow.

    i programmed the pic16f690 and put it in my circuit. this is what happens:
    1)LED pulses continuously when no object in front of it.
    2)when object infront of transmitter it stops glowing
    3)when shine light on transmitter the LED pulses very fast.


    hardware connections:
    LED connected to RC3
    output of receiver connected to RC2
    micro generates pulses at RA2

    here is my code:

    */

    #include <pic.h>
    #include "pic.h"
    #include "math.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include "delay.h"

    void Transmit40(void);

    bit ir1,ir2; / 2 different outputs of sensor during On & Off pulse
    unsigned char ir; // stores final result


    void Transmit40(void)
    {


    int t=0;
    int cnt=0;

    while(cnt<20) //Tx 10 IR pulses/burst
    { if (t==0)
    {
    PORTA=PORTA^0x04; //send IR, Toggles RA2
    DelayUs(5);
    ir1=RC2; // IR bit1= RC2
    cnt++;
    t=1;

    }
    else if (t==1)
    {

    PORTA=PORTA^0x04; //stop IR, Toggles RA2, RA2 is source of low duty cycle pulses
    DelayUs(20);
    ir2=RC2; // O/P IR bit2= O/P pin of receiver IC
    cnt++;
    t=0;
    }


    if ((ir1 == 1)&(ir2 == 0)) //Obstacle detected
    {
    ir = 1;
    }


    else
    {
    ir = 0; // The way is clear in front of the sensor
    }


    RC3=ir; // led connected to RC3, LED indicates detection

    }
    }

    void main(void)
    {
    ANSEL=0; // Set inputs to digital
    TRISC=0x04; //making pin RC2 input. output of detector IC connected to RC2
    while (1)

    Transmit40();

    }


    /*
    the hardware part of my circuit works. i initially used a 555timer to generate pulses and the hardware worked.
    all im doin now is using a microcontroller to generate pulses instead of a 555timer. so the problem lies in the software.

    please do assist me to correct my code. it is for a project Im a beginner with the pic*/
     
    Last edited: Aug 20, 2009
  2. beenthere

    Retired Moderator

    Apr 20, 2004
    15,815
    282
    This appears to duplicate your earlier post, which has a helpful reply.
     
Loading...
Thread Status:
Not open for further replies.