I have a Hall geartooth sensor (3 wire 5V sinking) that I wired to an Uno...I connected via serial monitor and verified that it's working...reading 1 or 0 alternating as the teeth pass by...
My goal is to have the Uno monitor the geartooth sensor and as long as the reading is alternating 0-1-0-1-0-1 ect then the gear is rotating...If the Uno reads either a 0 or a 1 for longer than say half a second then that means the gear has stopped rotating...If a this occurs then I'll be driving a relay to cut power...I got the relay part but reading and checking that the value is alternating 0-1-0-1-0 from the sensor and putting that into code I'm a bit stuck...
Ok easy, get a reading from the sensor and compare it to an "old" reading. If the two readings are different, then you know it is oscillating.
Example:
void loop() {
.
.
.
currentValue = get_from_sensor();
unsigned long timer = millis();
if(currentValue != oldValue && millis() - timer < 1000) // 1000 = 1 second
{
oldValue = currentValue; // they were different and values changed within 1 second, so update old value for next reading
Serial.println("Oscillating! "); // show a message that it was oscillating
timer = millis(); // Reset timer
}
}
You could also probably use the pulseIn() function. Set the pin you want to read from, set the state you want to look for HIGH or LOW, and you can also set a timeout value from 10 microseconds to a maximum of 3 minutes.
HazardsMind:
Ok easy, get a reading from the sensor and compare it to an "old" reading. If the two readings are different, then you know it is oscillating.
Example:
void loop() {
.
.
.
currentValue = get_from_sensor();
unsigned long timer = millis();
if(currentValue != oldValue && millis() - timer < 1000) // 1000 = 1 second
{
oldValue = currentValue; // they were different and values changed within 1 second, so update old value for next reading
Serial.println("Oscillating! "); // show a message that it was oscillating
timer = millis(); // Reset timer
}
}
You could also probably use the [pulseIn() function](http://arduino.cc/en/Reference/PulseIn). Set the pin you want to read from, set the state you want to look for HIGH or LOW, and you can also set a timeout value from 10 microseconds to a maximum of 3 minutes.
How would I insert an OUTPUT to a pin to trigger a relay if the 1 sec time out was reached due to the state of the sensor not changing?
An ELSE statement might work, or another IF statement where you check to see if it does stall for a second or more.
if(millis() - timer >= 1000)
{
// set an output pin high
}