monitoring switch state

Yes add debug traces where the values you care about are being changed - then you’ll get a chance to see what is,going on

J-M-L:
Yes add debug traces where the values you care about are being changed - then you’ll get a chance to see what is,going on

its not that simple i cant access the device very easily there no easy way to monitor and wait for the serialprint can you tell me about this

  FloatSwitchState = digitalRead(FloatSwitch);
  if (FloatSwitchState != lastFloatSwitchState) {
    if (FloatSwitchState) {
     FloatSwitchStateCounter++;
    }
    timerParams.FloatSwitchCounter++;
    FloatSwitchLowMillis = millis(); //to reset float switch every time water level changes
    FloatSwitchMillis = millis();  //to reset float switch every time water level changes
    FloatSwitchResetMillis = millis();
  }
  lastFloatSwitchState = FloatSwitchState;

is lastFloatSwitchState -FloatSwitchState; in the right place. to help visualize what im doing. . i want to update FloatSwitchMillis = millis(); every time the float sensor physically moves. this part of the code is the only thing i can think if thats not working the way i think it does or theres a bug in the compiler

is lastFloatSwitchState -FloatSwitchState; in the right place.

Yes.

That does NOT mean that the code before it makes any sense.

  if (FloatSwitchState != lastFloatSwitchState) {

This is checking to see if a change occurred. Presumably, FloatSwitchState was just valued by a call to digitalRead(), but you can't seem to understand the need to post all of your code, or at least enough of it, so that is only a guess.

    if (FloatSwitchState) {
     FloatSwitchStateCounter++;
    }

If FloatSwitchState what? digitalRead() doesn't return true or false, so an explicit test makes more sense.

    timerParams.FloatSwitchCounter++;
    FloatSwitchLowMillis = millis(); //to reset float switch every time water level changes
    FloatSwitchMillis = millis();  //to reset float switch every time water level changes
    FloatSwitchResetMillis = millis();

Regardless of what the change was, reset the three event times to nearly, but not necessarily exactly, the same time.

The change could have made FloatSwitchState HIGH, so setting the FloatSwitchLow event time to now does not make sense.

Why ARE there three variables to hold event times related to the float switch? What, EXACTLY, are the three events you are recording "when" for?

PaulS:
Yes.

That does NOT mean that the code before it makes any sense.

  if (FloatSwitchState != lastFloatSwitchState) {

This is checking to see if a change occurred. Presumably, FloatSwitchState was just valued by a call to digitalRead(), but you can't seem to understand the need to post all of your code, or at least enough of it, so that is only a guess.

    if (FloatSwitchState) {

FloatSwitchStateCounter++;
    }



If FloatSwitchState what? digitalRead() doesn't return true or false, so an explicit test makes more sense.



timerParams.FloatSwitchCounter++;
    FloatSwitchLowMillis = millis(); //to reset float switch every time water level changes
    FloatSwitchMillis = millis();  //to reset float switch every time water level changes
    FloatSwitchResetMillis = millis();



Regardless of what the change was, reset the three event times to nearly, but not necessarily exactly, the same time.

The change could have made FloatSwitchState HIGH, so setting the FloatSwitchLow event time to now does not make sense.

Why ARE there three variables to hold event times related to the float switch? What, EXACTLY, are the three events you are recording "when" for?

iposted the whole code in an attachment in post #9. im trying hard to comprehend whats going on here for some reason i just cant visualize the problem. so basically your saying that this FloatSwitchMillis = millis(); may not be update EVERY time the float siwtch change its physical state. because for some reason i cant comprehend right now the state detection in the code is not working the way i think it does. just to clarify the switch never physically changes state in the tank faster that 1 time every 15 minutes

and this part of the code

    if (FloatSwitchState) {
     FloatSwitchStateCounter++;
    }

i put that there becausue if FloatSwitchState is high it will not equal 0 meaning its true and maybe not but i thought LOW would be 0 aka false. just for a non noise debounce affect incase the water had turbulence it would not run that counter up too fast. when it equals 4 counts within 5 seconds it will reset the alarm status. thats what the reset millis is for. so basically there are 3 timers counting down to 0 and the time restarts when the float switch state changes aka the water level changes from high to low

This thread is very painful to read. Try this as a basis for your program. It is based on my earlier suggestion to simplify your program

const byte switchPin = A1;
unsigned long currentTime;
unsigned long stateChangeTime;
unsigned long period = 5000;
byte currentSwitchState = HIGH;
byte previousSwitchState = HIGH;
boolean alarmTriggered = false;
unsigned long alarmStartTime;
unsigned long alarmPeriod = 1000;

void setup()
{
  Serial.begin(115200);
  pinMode(switchPin, INPUT_PULLUP);
}

void loop()
{
  currentTime = millis();
  currentSwitchState = digitalRead(switchPin);
  if (currentSwitchState != previousSwitchState)  //switch state has changed
  {
    stateChangeTime = currentTime;
    alarmTriggered = false; //suppress alrm
    previousSwitchState = currentSwitchState;
  }
  else if (currentTime - stateChangeTime > period && !alarmTriggered) //time's up
  {
    alarmTriggered = true;  //turn on the alarm
    alarmStartTime = currentTime;
  }
  if (alarmTriggered) //sound the alarm if active
  {
    soundAlarm();
  }
}

void soundAlarm()
{
  if (currentTime - alarmStartTime > alarmPeriod) //is it time for a message or noise ?
  {
    Serial.println("Alarm");
    alarmStartTime = currentTime;
  }
}

I believe that the compiler treats 60000 as 60000ul so that 60000 == 60000ul. BUT...

You can put 60000 in an unsigned int, an unsigned long, or in a long. An int on most (all?) Arduinos has a maximum value of 32767. 60000 and even 60000ul will wrap around to a negative number when stored into an int.

an unsigned int on a "standard" arduino (16 bits) ranges from 0 to 65535 (216 - 1).

(cf the doc for unsigned int)