Measuring time interval between on and off in float switch

this is a code that i had only copied and can show the float switch on and off time. What i want to do is to only show the "float switch ON time". i have little experience in programming and if there is another approach i would want to know. thank you

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
  }
}

What i want to do is to only show the "float switch ON time".

Do you mean that you want to know the time the switch went from off to on or the amount of time it was on ? If the latter, is it cumulative time that you want or individual period times ?

UKHeliBob:
Do you mean that you want to know the time the switch went from off to on or the amount of time it was on ? If the latter, is it cumulative time that you want or individual period times ?

Basically, i want to know the amount of time it was off. sorry for the misconception. i want individual period of times.

When the switch goes from on to off you print a message. At that point save the value of millis() in a variable. When the switch goes from off to on you print a message. At that point subtract the previously saved time from the current value of millis() and you have the off time in milliseconds for the latest period.

A little maths and you can have it in any format you want ready to print or perhaps save to an SD card or send via a link to another device.

i am sorry but can you show me the code? i have almost zero knowledge on coding and i only copy the work of others. thanks

Untested

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
    {
      unsigned long offTime = millis();//save start time
      Serial.print("Float switch ON time  ");
      Serial.print((millis() - floatSwTimer) / 1000);
      Serial.print("\t");
      Serial.println("Seconds");
      unsigned long onTime = millis();
      unsigned long offPeriod = offTime - onTime;
      Serial.print("Off for ");
      Serial.print(offPeriod);
      Serial.println(" milliseconds");
    }
    floatSwTimer = millis(); // reset timer
  }
}
byte floatSw = 4; // Float switch pin, other side of switch to GND

Why doesn't the variable name contain Pin? If it did, the comment would be unnecessary. And, it would be obvious that the variable contained a pin number.

unsigned long floatSwTimer;

That variable contains a TIME, not a TIMER.

    floatSwState ^= 1; // make them the same
    if(floatSwState == false)

The digitalRead() function return HIGH or LOW. Why would you compare HIGH or LOW to false?

LOW is by definition false, and HIGH true. This means you can simply say

  if (digitalRead (floatSwPin))

if you want to. Since pins may differ as to whether they are active-HIGH or active-LOW, it might
be more robust to use a function for each pin:

inline boolean floatSwActive () { return digitalRead (floatSwPin) == LOW ; }

The inline declaration saves the overhead of actually having functions at runtime, but the rest of the code
will be more readable as it doesn't care about such details of active-low or high.

LOW is by definition false, and HIGH true.

Is that actually true and if so, can you guarantee that it will stay that way ?

The Arduino functions cold, if they wished, be written to return say 123 for LOW and 124 for HIGH and they would still work if HIGH and LOW were defined appropriately when testing for HIGH and LOW. Such a change is unlikely to happen, but it could.

Is that actually true

Not really. HIGH is defined as 1, and LOW is defined as 0. true and false also are defined as 1 and 0.

So, HIGH and true are defined to have the same value, but HIGH is not defined to be true.

if so, can you guarantee that it will stay that way ?

Nothing in life is guaranteed, but the odds that true, false, HIGH, or LOW will be assigned different values are lower than the odds of Trump being re-elected.

the odds that true, false, HIGH, or LOW will be assigned different values are lower than the odds of Trump being re-elected.

Agreed, but it would be fun to see acres of naughty code stop working.

You would truly be in your element :slight_smile: