Interfacing optoelectric proximity sensor with arduino

PaulS:
A link to the device would be useful.

You have far more code than you need to illustrate the problem, which that code does not do, as there are no Serial.print() statements.

I am sorry I do not have a link to this sensor as I just found it in the store room of my company with no details of the manufacturer or the part number.

Print statement was used in another program when I connected the sensor to an analog pin. But the result in that case was floating between 0 and 1023 even thought LED1 connected to the same pin was glowing continuously (with no fluctuations in brightness (at least not any visible one)).