My Water Level Sensing and LED Problem

I want to validate that my water level is at a certain sensor. If it is at that sensor level, then I want it to turn on an LED. Now water starts to drain out and now that sensor is off or (LOW) state. When it drains and the next sensor goes (LOW) I want the LED to go off (led LOW). Having it go LOW is my problem.

p.s idk if you try this with water. Obviously you can test this without water.

const int waterLevel3= 9; //water level 1
int buttonState9 = 0;

const int waterLevel2 = 8; //water level 2
int buttonState8 = 0;




const int buttonPin2 = 2;
int buttonState2 = 0;

void setup() {
  
  pinMode(11, OUTPUT); //led pin
  pinMode(9, INPUT);
  pinMode(8, INPUT);
  
  

  
  pinMode(2, INPUT); //turn on system


}


void loop() {

buttonState9 = digitalRead(waterLevel3); //read pin 9 whether water is present;
buttonState8 = digitalRead(waterLevel2); //read pin 8 whether water is present;



buttonState2 = digitalRead(buttonPin2); //power is on.



  if (buttonState2 == HIGH)//power on
    
    {if (waterLevel3 == HIGH) // is there water at level 3?
     
      
     {while (waterLevel2 == HIGH) //turn led on while water at this level

// The problem here is waterLevel3 is now LOW
  
      
     
       
 { digitalWrite(11, HIGH); }  // turn on the LED 
}}}

You dont need to set the LED state every time you go through the loop()- it will stay at whatever state you set until you change it. Once you understand this its very simple.

ledstate=digitalRead(11); // can you read an output? yes you can!

//if the LED is off and the water level reaches the top sensor turn the LED on
if ( ! ledstate && buttonstate9) {
     digitalWrite(11,HIGH);  
} 

//if the LED is on and the water level drops below the bottom sensor turn the LED off
if ( ledstate && ! buttonstate8) {
     digitalWrite(11,LOW);  
} 

// otherwise leave the state of the LED alone

I get what you're saying. Thank you for the logic! I will try this tonight when I get home.

Tried your code and of course it works! The next question (which is assumed) if I put a boatload of these sensors in water (with a ground and +V for each level) will it act as it should? Assuming electricity will try to find the shortest distance between pos. and neg. connection, as long as the +/- connections are closer to each other than the distance between each level, than this should work. Like this:

pos neg

pos neg

pos neg (notice the distance between the words pos and neg is shorter than next pos neg.

Post a link to the data sheet or product page for the sensor.

jremington, or anyone else who can help,

I have a spare Arduino Nano. I was thinking of using a bunch of pins (like 2 - 12 digital) for this. I would source the project with the 5v from the Nano. Of course I would be splitting the 5v with however many pins I decide to use.

Please advise for any painfully obvious problems I may run into. I'm novice to Arduino and much Electrical theory. I just got visions and a dream....

Also worth mentioning:

I'd like an interrupt state whereas if this other condition is not met, it stops the original code (above) and then goes back to it once the condition is met.

something like...

if (!otherCondition){
while (!otherCondition){
shut off the LED;}}
then go back to original state.

The thing is, if you try to go back to the original code (up above) the first condition will now be FALSE (because we assume water has already drained past that point) so how can revert back to it? THAT...is truly beyond me at my current knowledge level.

Spend a little time with google. "water level sensor pins" brought up this as the first images hit. That idea would work with Arduino.

That's a good diagram and almost exactly as I imagined it. Thank you. Now it's just a matter of interrupting the code.