What is up with While condition

This is just for my own education, but I am curious if anyone knows why the condition in my while loop is behaving as it is.
My project is a fish farming controller. I want to use a switch to activate a "Filter Mode", where while the switch is flipped the drain valves are both held open. I do have a pull-down resistor attached in the presumably appropriate manner.
This is my total code up to the While loop.

 byte ValveA=5;
  byte ValveB=2;
  byte PumpRly=6;
  byte FilterSwitch=4;
  byte TimeSwitch=3;

void setup(){
   Serial.begin(9600);

 pinMode(ValveA,OUTPUT);
 pinMode(ValveB,OUTPUT);
 pinMode(PumpRly,OUTPUT);
 pinMode(FilterSwitch,INPUT);
 pinMode(TimeSwitch,INPUT);
}

void loop(){

 bool PumpState = digitalRead(PumpRly);
 bool FilterSwitchState = digitalRead(FilterSwitch);
 bool TimeSwitchState = digitalRead(TimeSwitch);
 int LevelSense= analogRead(A0);
 float voltage= LevelSense * (5.0/1023.0);

  while (digitalRead(FilterSwitch)==1){
      Serial.println("Filter Mode");
      Serial.print (FilterSwitchState);
      digitalWrite(ValveA,HIGH);
      digitalWrite(ValveB,HIGH);
}

This works perfectly. However, what I originally tried was the while loop stated as

  while (FilterSwitchState==1)

and when the switch is flipped, it enters the while loop, but it will not exit when the switch is un-flipped. This line is the only change from the working to the non-working code. I thought this might be because the while loop was blocking the earlier assigning of "FilterSwitchState", so I asked it to print "FilterSwitchState" to the serial monitor during the While loop. It is switching from 1 to 0, but still will not exit the While loop. This happens in both TinkerCAD and Wokwi. Is there any particularly obvious thing I am missing that means I have to get the while function to actually read the switch itself versus referencing something that is already assigned? Thanks!!

Is the switch connected to 5V?

It is

Please describe how you have it connected

It would also be interesting to see your full non working sketch. Did you by any chance have a semicolon on the end of while (FilterSwitchState==1), for instance ?

Unless the original code that you didn't see fit to show included the line FilterSwitchState = digitalRead(FilterSwitch);, it will never exit the while loop.

And bool FilterSwitchState = digitalRead(FilterSwitch); inside the while loop wouldn't have done it either. Why? Scope. That would have created a new variable inside the loop called FilterSwitchState that disappeared when the interior of the loop was done. The while statement would have been checking the outer, earlier FilterSwitchState, which of course, never changed.

Guessing is not a productive use of time. If you have trouble with code, show that code.

The above statement will continually read the state of the switch so when it goes to 0 the while loop will exit.

 while (FilterSwitchState==1)

With this one the FilterSwitchState never changes and you are stuck in the while loop forever

And we ask you to post the code that did that.

a7

I will try to post a picture when i have access to my laptop again

The only difference between the working and non-working portions is that line. I should have been more clear about that. I can post that full code as well, also when I return to my laptop.

The only change from the working to non-working portions is that line (or at least so i think). I will post the full non-working code, once i again have access to my laptop.

You have a code issue. While many helpers can make educated guesses, you will get better help if you post the FULL code.

If the non working while loop looked like this

    while (FilterSwitchState == 1)
    {
        Serial.println("Filter Mode");
        Serial.print(FilterSwitchState);
        digitalWrite(ValveA, HIGH);
        digitalWrite(ValveB, HIGH);
    }

Then as @jim-p pointed out the while loop will never end because the value of FilterSwitchState does not change within it

I apologize. I will edit with the full non-working code once I return home.

See post #6
I don't need any further information from you.
Don't change anything.

Ok, fine, but I have a serious doubt that you should be using a while loop in this situation. Probably just an if statement.

I also guess that you should be detecting the change-of-state of the switch, and only taking any action when the state changes.

A picky point, but...

If you read the documentation for digitalWrite(), it says that the return value will be either HIGH or LOW. It's true that HIGH == 1, but it's not good practice to assume that when you don't have to.

Also not very good practice, because often in final construction of projects, the switch is mounted on the outside of a case/box and connected with flying wires to the main circuit board. If that wire comes loose, you have a "live" 5V wire floating around and potentially damaging the circuit.

So a better method is to connect the switch between the Arduino pin and ground and use an internal pullup resistor. If you want to use an external pullup resistor, solder it on the main board. Now, if those flying wires come loose, they are not live and much less likely to cause any damage.

Doing the above does mean that the pin will read LOW rather than HIGH when the switch is closed, but it's easy to deal with that in your code.

No need for a picture.

No need, see post #6

replace this with if (FilterSwitchState==1) to check it once,
The next iteration of loop() it will have reread the FilterSwitchState

Please don't modify a post that has answers. Rather post that code in a new reply.

Setting the state of a pin...

... then reading the pin...

...will have a lead/same/lag issue...

byte sw = 4;
void setup() {
  Serial.begin(9600);
  pinMode(sw, INPUT); // circuit needs pulldown resistor
}
void loop() {
  bool st = (digitalRead(sw));
  delay(150); // rudimentary debounce

  if (st != digitalRead(sw)) {
    Serial.print(" !"); // not the same
    if (st > digitalRead(sw)) Serial.print(">"); // state > switch
    if (st < digitalRead(sw)) Serial.print("<"); // switch > state
  }
}

Result of nine button presses using delay() debounce...

!< !> ! !> ! !< !> !< !>

But... without debounce (one button press)...

!> ! ! ! ! ! !> !> ! !> ! ! ! ! !< !> !> ! ! ! !< ! !> !> ! ! ! !< !> !> ! !< !> !< ! !> ! !< ! ! ! !< ! !> !> !< !< ! !< !> ! !<

@thetimatotot claims to have code where the tested variable changes value, as proven by printing it, yet the while never exits.

That is the code I want to see.

Don't just say what the difference is, post the actual code.

a7

You should spend some time reading the Arduino reference and learn how the different functions work.
Here is the section on while:
https://docs.arduino.cc/language-reference/en/structure/control-structure/while/