question regarding input

Hi guys,

Im working on a small project, which basically runs a motor to pump fluid, until it detects that the liquid is leaking out of the container.

however, i have the motor working fine, it runs perfectly using the analogue output, so that i can control the flow based on the power output.

however the sensor is connected to input pin 3, and 5v+

now the problem is, when i run the script, pin 3 is bouncing HIGH, LOW, HIGH, LOW, HIGH, LOW but the connection isnt made, (the sensor isnt connected) so it is automatically stating that the system is leaking. why is pin 3 outputting power, when i have it set to input?

also, the if statement runs all the time, even when pin 3 is LOW (or 0) why?

here is the code
int z = 0;

void setup() {
Serial.begin(9600);
pinMode(3, INPUT);
pinMode(15, OUTPUT);

}

void loop()
{
while(z == 0)
{
analogWrite(15, 255);
Serial.println(“Pump Started”);
for(int x = 0; x <1000; x++)
{

if(digitalRead(3)==HIGH)
{
z = z+1;
Serial.println(“Leaked, Ending Test”);
Serial.println(digitalRead(3)); // shows 0 for 5 seconds then 1 for 5 seconds
Serial.println(z);

}
}

digitalWrite(15, LOW);
Serial.println(“Pump Stopped”);
}

guys i am a beginner so please be nice… lol. (script is not complete, as it does not end test, but just keeps going through it, i will sort that… but why is pin 3 doing what its doing.

cheers
keith

Your input is floating; use an pull-down resistor.

thanks,

I didnt realise i needed to add a pull down…

thanks for the help
keith

Or use the internal pullup resistor with pinMode(3, INPUT_PULLUP)

...R