Hi, i would like to know if it is possible to measure the time interval between the on and off of the float switch? I will use it in my project. is there someone here who knows the code? please help me, thanks!
Give this a try, change pin number to suit.
unsigned long floatSwTimer;
bool floatSwState;
byte floatSw = 4; // Float switch pin, other side of switch to GND
void setup()
{
Serial.begin(9600);
pinMode(floatSw,INPUT_PULLUP);
}
void loop()
{
if(digitalRead(floatSw) != floatSwState) // if they are different,
{
floatSwState ^= 1; // make them the same
if(floatSwState == false)
{
Serial.print("Float switch OFF time ");
Serial.print((millis() - floatSwTimer) / 1000);
Serial.print("\t");
Serial.println("Seconds");
}
else
{
Serial.print("Float switch ON time ");
Serial.print((millis() - floatSwTimer) / 1000);
Serial.print("\t");
Serial.println("Seconds");
}
floatSwTimer = millis(); // reset timer
}
}
If you get "false trips" from the float sw bobbing up and down, you may have to "debounce" it.