Practical arduino automation sensor output flickers

Good morning Everybody
My board Freduino lite V.2 with atmega 168 tried for project from book practical arduino security automation sensor

byte channelAInput = 0;
byte channelALed = 4;

void setup()
{
Serial.begin(38400);

pinMode(channelALed, OUTPUT);
digitalWrite(channelALed, LOW);

}

void loop()
{
byte sensorStatus;
sensorStatus = checkSensor(channelAInput, channelALed);

Serial.println("");

}

boolean checkSensor( byte sensorInput, byte statusOutput )
{
byte state;
int sensorReading = analogRead(sensorInput);
if( sensorReading < 400 ) {
state = 0;
digitalWrite(statusOutput, HIGH);
delay(2000);
}
else if ( sensorReading >= 400 && sensorReading < 590 ) {
state = 1;
digitalWrite(statusOutput, LOW);
delay(2000);
}
else if ( sensorReading >= 590 && sensorReading < 800 ) {
state = 2;
digitalWrite(statusOutput, HIGH);
delay(2000);
}
else {
state = 3;
digitalWrite(statusOutput, HIGH);
delay(2000);
}

Serial.print(sensorInput, DEC);
Serial.print(": ");
Serial.print(sensorReading, DEC);
Serial.print(" (");
Serial.print(state, DEC);
Serial.print(") ");

return state;
}

the above code works perfectly by usb and by external power supply ,Were when the sensor (limit switch) is nearrer the board the said programe works correctly but when the sensor connected to door the out put flickers randomly.Searched forum,googled no result need kind help from learned members and thankful in advance.

1345767305455.jpg

nazeem:
Were when the sensor (limit switch) is nearrer the board the said programe works correctly but when the sensor connected to door the out put flickers randomly.

What sort of sensor is it? (A switch? A PIR sensor?)

What's the difference between the two scenarios - is it the same circuit with longer wires, or are there any other differences? Is it the identical 'sensor' (switch, whatever) connected in both cases?

If the answer is that everything's exactly the same and you're just using longer wires, and the sensor is something other than a switch, I suppose you're probably picking up interference. What sort of wires are you using?

Thankyou so much PeterH
For you kind intereset as the sensor is a switch and wires are normal flexible connecting wires at about ten feet length please see image

Thanking you

nazeem:
For you kind intereset as the sensor is a switch

I assume you mean it simply connects the two wires together, or isolates them.

Does the problem occur when the switch is closed? If so, that would point to a wiring fault.

Yes exactly the switch is connected in .NC (normally close) condition as per the above said code the output must go high when switch goes to normal open condition,as of the output flickering randomly at normally close condition , simply two wires connected to switch check the image.

Thanks peterH assumming close to solution

nazeem:
Yes exactly the switch is connected in .NC (normally close) condition .. the output flickering randomly at normally close condition

That suggests that you have an intermittent break in the circuit - perhaps a broken wire or poor connection. You could track it down by monitoring the end-to-end resistance as you wiggle wires and connections until you find the cause, bearing in mind that the Arduino would pick up even very brief breaks which might not be obvious to a digital meter.

Goodmorning PeterH
Well checked the connections soldered' on PCB further checked the sensor wires and connections are intact,yet no result found as everything is correct but ouput flickering not stopping,unable to find the reason-Is there any problem in code?