Floating value when using extension wires

Hi!
I was using a phototransistor to detect light. I used a breadboard and some male to male jumper wires to connect to the phototransistor and I executed the AnalogReadSerial sketch.
What made me wonder is that, last night, I am receiving normal data through serial monitor, but the next morning, I uploaded the sketch again, and to my surprise, the data being sent is floating. Then I connected the phototransistor legs directly to the headers of the arduino and the data was not floating anymore. How did this happen? Everytime I use a jumper wire, the data becomes floating. My connections in both circumstances were the same. This is not the case when I use a potentiometer though.

You will encounter a bad jumper wire from time to time. You could test the wire or try a different wire. Sometimes breadboards don't provide the most reliable connections, which would be shown by wiggling the parts around in the breadboard to see if the problem comes and goes accordingly.

I had 5 jumpers that had the same floating results and I also noticed that just attaching one end of the jumper wire to the input pin and the other end not connected to anything yields the same floating values. I also left the device running for an hour and when I came back, the results were not floating anymore. I really wonder what happened and how to prevent it from happening in the future.

jasperizak:
I also noticed that just attaching one end of the jumper wire to the input pin and the other end not connected to anything yields the same floating values.

That's the expected behavior if you don't have a pull-up or pull-down resistor on the pin. You can turn on the internal pull-up by adding this to your setup():

pinMode(analogPin, INPUT_PULLUP);

where analogPin is the pin you're taking the analog readings from. This will cause analogRead() to always return a value around 1024 unless there is something connected to the pin pulling it down. That would not be necessary with a potentiometer connected as it never allows the pin to float.

But isn't pinMode for digital only?

A pull-up is a pull-up whether you're doing an analog or a digital read. Setting the pin mode to input is not necessary but the command also sets the internal pull-up. You could do the same like this:

digitalWrite(analogPin, HIGH);

but I think

pinMode(analogPin, INPUT_PULLUP);

makes the intention of the code much more clear.

"This will cause analogRead() to always return a value around 1024 "
1023 will be the max, 10 bits high, 0x03FF

Thanks!