Thanks for your help, I used the "debounce" code as a start and have managed to connect the reed :), I used the serial monitor to display when the pin goes from 1 to 0 with the magnet attached and it works fine. I have a question about the Reed, when I connect it directly through the 5V and Ground on the Arduino and close the switch with a magnet I get a LED light in the switch. However when I hook the 5V to Pin 7 and ground to Arduino I can read the change but the LED doesn't light? Any Ideas?
Thanks
Geoff
// constants won't change. They're used here to
// set pin numbers:
const int ReedPin = 7; // the number of the Reed Switch pin
const int ledPin = 13; // the number of the LED pin
// Variables will change:
int ledState = HIGH; // the current state of the output pin
int ReedState; // the current reading from the input pin
int lastReedState = LOW; // the previous reading from the input pin
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(ReedPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// read the state of the switch into a local variable:
int Reed_Val = digitalRead(ReedPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (Reed_Val != lastReedState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
ReedState = Reed_Val;
}
digitalWrite(ReedPin, HIGH);
Serial.println(Reed_Val);
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastReedState = Reed_Val;
}