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.
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!!
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, earlierFilterSwitchState, which of course, never changed.
Guessing is not a productive use of time. If you have trouble with code, show that code.
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.
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.