IR LED EMMITTER AND SENSOR CODE PROBLEM

Hello All,
I am currently using an IR LED emitter(NOT TV REMOTE) and a IR receiver. I want to use them to detect if something is in between them(block signal).
I currently have a led setup to light up if the IR receiver senses that there is no signal from the emitter.
I took this code from a website and modified it to fit my needs.
My problem occurs when i turn on the arduino the Led that is supposed to indicate if the IR receiver is blocked or not, is already on. The led just remains on the whole time even when i don't block the signal.
I've read on these forums about TV remote and ir sensor but it's too complicated of a code for my application.
I tried to film the IR LED on camera and play the video to see if the led was flashing or not( IR is out of our visual spectrum). But The camera didn't seem to capture the IR LED flashing.
Please tell me if i'm missing any other information you guys need to help me. This is for a school project and it's my first time using arduino.
Thank you for your time.

int ledPin = 0;        // LED connected to digital input 5
int emitter= 11;
//int beepPin = 18;       // Pietzio beeper to analogue input 4
//int resetSwitch = 15;   // Alarm reset from analogue input 1
int analogPin = 0;      // IR sensor connected to analog pin 0
int curVal = 0;         // variable to store the current value of IR sensor
int lstVal = 0;         // variable to store the last value of IR sensor
int threshold = 300;    // threshold to trigger alarm the bigger this is the easer it is
//boolean alarm = false;  // to see if we have triggerd an alarm

void setup() {
  pinMode(ledPin, OUTPUT);         // sets the LED pin as output
  //pinMode(beepPin, OUTPUT);        // sets the beeper pin as output
  //pinMode(resetSwitch,INPUT);      // sets the Alarm reset pin as a digital input
  //digitalWrite(beepPin, LOW);      // make sure alarm is off
  //digitalWrite(resetSwitch, HIGH); // enables the pull up resistor on alarm reset
 pinMode(emitter,OUTPUT);
  curVal = analogRead(analogPin);  // read the input pin 
  lstVal = curVal;                 // set it for no change
  Serial.begin(9600);              //  setup serial for debuging
}

void loop() {
  //if(alarm) doAlarm(); 
  digitalWrite(emitter,HIGH);
  doWatch();
  delay(20);                                  // time between looks at the system
}

void doWatch() {
  curVal = analogRead(analogPin);              // read the input pin  
  if (abs(curVal - lstVal) >= threshold) digitalWrite(ledPin, HIGH);
  Serial.println(curVal - lstVal);             // debug value
  lstVal = curVal;
}
int ledPin = 0;        // LED connected to digital input 5

Is the LED connected to pin 0 or pin5? If it really is connected to pin 0, that is one of the pins that the Arduino uses to connect to the serial port. That would explain why it is on when you start the Arduino. The Arduino is trying to make a serial connection. The only way it has of knowing whether the serial cable is connected is to try sending data. If it receives any, a cable is connected. If not, it will eventually decide that a cable is not present, and quit trying to send serial data.

If you have an IR sender, and IR receiver, and an LED connected, you are missing a pin (and pinMode) declaration for one of the devices.

Your comments say that the LED is on pin 5 but the sketch says pin 0. Which is correct? Maybe you've put the LED on the wrong pin?

Okay thanks for your reply.
The comment on the LedPin should be that it is connected to pin zero.
The LED is physically connected to pin zero also.
I'll try connecting it to a different pin.
Thanks for the advice.
I'll update soon.

hello

I tried a similar code to Hilroy00 but I cant seem to make it work. I am wondering if the code for the emitter is wrong. When I open the serial monitor, only zeros appear. According to the receiver data sheet, there should be a 1 when the beam is not detected and 0 when detected.

I am using Inex infrared LED Board with Inex 38kHz Infrared Receiver Module.

Anyways here is the code:
any feedback appreciated

int ledPin = 2;        // LED connected to digital pin 2
int emitter = 9;       // Infrared LED to digital pin 9

int analogPin = 0;      // IR sensor connected to analog pin 0
int curVal = 0;         // variable to store the current value of IR sensor
int lstVal = 0;         // variable to store the last value of IR sensor
int threshold = 1;

void setup() {
  pinMode(ledPin, OUTPUT);
 digitalWrite(emitter,HIGH);
  curVal = analogRead(analogPin);  // read the input pin 
  lstVal = curVal;                 // set it for no change
  Serial.begin(9600);              
}

void loop() {
  doWatch();
  delay(20);                                  // time between looks at the system
}

void doWatch() {
  curVal = analogRead(analogPin);              // read the input pin  
  if (abs(curVal - lstVal) >= threshold){
    digitalWrite(ledPin, HIGH);
  }
  else {
    digitalWrite(ledPin, LOW);
  }
  Serial.println(abs(curVal - lstVal+1));             // debug value
  lstVal = curVal;
}

The comment on the LedPin should be that it is connected to pin zero.

Don’t use pin 0 that is connected to the serial data and attempts to use it will cause confusion like you are experiencing. Until you know what you are doing avoid pin 0 and pin 1 and pick one of the other pins.

You will not see the IR LED flickering on a viewfinder as it is flashing too fast. What resistor you you have with the IR LED, these are normally lower than visible ones as they can take more current.
There is no need to control the IR LED with the Arduino, it needs to be on all the time so you can just wire it up to the supply.

go_downhill
Where are you modulating the IR emitter in your code? Without it you will get nothing back from the module. This is different from the setup that hilroy has.