Perfect!
Thanks for the help Magician, and all.
That worked perfectly. Was it the "Open Collector" that gave you that answer? I am a bit noobish here. That was much simpler than I thought it was going to be.
It turns out that the sensor is sensitive to polarity. One side will read a north value, and one will read a south value, not that I know which side is which =)
A side question about the programming. I have the code below, and if you read it you might be able to see what I am trying to do. Basically, I want to do "something" when the magnet is detected and when it leaves, non-repeating. Right now it is just printing a message, and it is trivial. I am just trying to learn more about this type of development. I am a programmer by trade, but this type of programming is completely new to me.
It would be very easy to tie up the processor waiting for the event to occur, but that would mean that nothing else could happen while we were waiting for the events. What I have works perfectly for now, but I was wondering if anyone had a more "elegant" way of doing this. Basically my if statements in the loop method.
If I lost you with my explanation, let me know that too, it has been a long day =)
int sensorPin = 2;
int counter = 0;
boolean sensorState = false;
void setup()
{
// setup serial - diagnostics - port
Serial.begin(9600);
pinMode(sensorPin, INPUT);
digitalWrite(sensorPin, HIGH);
}
void loop()
{
if(magnetPresent(sensorPin) && !sensorState)
{
sensorState = true;
printMessage("Magnet Present");
}
else if(!magnetPresent(sensorPin) && sensorState)
{
sensorState = false;
printMessage("Magnet Gone");
}
}
void printMessage(String message){
counter++;
Serial.print(counter);
Serial.print(" ");
Serial.println(message);
// delay(1000);
}
boolean magnetPresent(int pin){
return digitalRead(pin) == LOW;
}