Flip flopping value when magnetic door contact is open

My Arduino has a magnetic contact of a door on digital pin 2, so when the door is open there is a voltage. All is fine when the door is closed and there's no voltage - my code registers the door has closed and then stops. When the door is opened though the value flipflops between 1 and 0 so it looks like the door is opening and closing.

Full code is at https://github.com/pauly/robot_butler_server but as there's quite a bit there the bit I think is relevant is below.

unsigned int doorOpen = 0;
void loop( ) {
  door( 2 );
}
int door( int pin ) {
  unsigned int v = digitalRead( pin );
  if ( v != doorOpen ) {
    Serial.print( v );
    Serial.print( " != " );
    Serial.println( doorOpen );
    doorOpen = v;
    Serial.print( "now doorOpen = " );
    Serial.println( doorOpen );
    delay( 1000 );
    // alert( doorOpen );
  }

When the magnetic door catch is closed everything is fine but when it's open my output is

1 != 0
now doorOpen = 1
0 != 1
now doorOpen = 0
1 != 0
now doorOpen = 1
0 != 1
now doorOpen = 0
1 != 0

I assume there's a bug in how I'm using the global variable doorOpen but I can't figure out what. Any help greatly appreciated.This is the magnetic contact, if you are interested: http://www.toolstation.com/shop/Magnetic+Contact/p33648 - I've tried connecting it to analog and digital inputs with the same effect.

How is it wired? Read this:

http://www.gammon.com.au/switches

Sounds like you are missing a pull-down or pull-up resistor.

Your example code is missing the "pinMode(2, INPUT);" or "pinMode(2, INPUT_PULLUP);"

not a programming issue at least which is a relief...