How to avoiding to "Serial println" a non necessary input?

Hello, i'm new here, and new on arduino. So thank you for your patience.

I am trying to program a water level sensor constituted of 5 to 10 non-contact sensors.
When the sensors detects water, it sends a signal printed as "1", else, it's "0".

Everything is working except that the serial monitor is printing not necessary informations as seen below
Water height 10% reached : 1
Water height 20% reached : 1
Water height 30% reached : 1
Water height 40% reached : 0
Water height 50% reached : 0

...when the water reachs 30%, i want to only print the reached level,
ex . "Water height 30% reached : 1"
and not the others heights like "Water height 10% reached : 0" and "Water height 20% reached : 0"

I can't find the argument to tell my program to "print only ... " perhaps should i search something with the "else" and "else if" arguments... but i didn't succeed...may i ask for help ? this is my beginner code :


int wls10 = 0;  //Water level sensor 1 ok
int wls20 = A2;  //Water level sensor 2 ok
int wls30 = 6;  //Water level sensor 3 ok
int wls40 = 7;  //Water level sensor 4 ok
int wls50 = 5;  //Water level sensor 5 ok
//int wls = 32;  //Water level sensor 6 ok
//int wls = 33;  //Water level sensor 7 ok
//int wls = 34;  //Water level sensor 8 ok
//int wls = 36;  //Water level sensor 9 ok
//int wls = 39;  //Water level sensor 10 ok

int level10 = 0;  //Variable to store the data received from sensor
int level20 = 0;  
int level30 = 0;  
int level40 = 0;  
int level50 = 0;  

//int buzzer = 5;

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

  pinMode(wls10, INPUT);
  pinMode(wls20, INPUT);
  pinMode(wls30, INPUT);
  pinMode(wls40, INPUT);
  pinMode(wls50, INPUT);
  //pinMode(wls60, INPUT);
  //pinMode(wls70, INPUT);
  //pinMode(wls80, INPUT);
  //pinMode(wls90, INPUT);
  //pinMode(wls100, INPUT);
  //pinMode(pump, OUTPUT);
  
}




void loop() {
  level10 = digitalRead(wls10);
  level20 = digitalRead(wls20);
  level30 = digitalRead(wls30);
  level40 = digitalRead(wls40);
  level50 = digitalRead(wls50);



///////////////////////////////////////////////////////////////

  
   //if (level10 == 1 && level20 != 1 && level30 != 1 && level40 != 1 && level50 != 1)  //My sensor gives 0 when it senses water and 1 when there is no water
   //if (level10 == 1 && level20 == 1 && level30 != 1 && level40 != 1 && level50 != 1)  
   //if (level10 == 1 && level20 == 1 && level30 == 1 && level40 != 1 && level50 != 1)  
   //if (level10 == 1 && level20 == 1 && level30 == 1 && level40 == 1 && level50 != 1)
   //if (level10 == 1 && level20 == 1 && level30 == 1 && level40 == 1 && level50 == 1)


    if (level10 == 1 && level20 != 1 && level30 != 1 && level40 != 1 && level50 != 1);
    {

    Serial.print("Water height 10% reached :            ");
    Serial.print(level10);
    Serial.println("");
    } 
  
    if (level10 == 1 && level20 == 1 && level30 != 1 && level40 != 1 && level50 != 1);
    {

    Serial.print("Water height 20% reached :            ");
    Serial.print(level20);
    Serial.println("");
    }

    if (level10 == 1 && level20 == 1 && level30 == 1 && level40 != 1 && level50 != 1);
    {

    Serial.print("Water height 30% reached :            ");
    Serial.print(level30);
    Serial.println("");
    } 

    if (level10 == 1 && level20 == 1 && level30 == 1 && level40 == 1 && level50 != 1);
    {

    Serial.print("Water height 40% reached :            ");
    Serial.print(level40);
    Serial.println("");
    } 

    if (level10 == 1 && level20 == 1 && level30 == 1 && level40 == 1 && level50 == 1);
    {

    Serial.print("Water height 50% reached :            ");
    Serial.print(level50);
    Serial.println("");
    } 

   

  Serial.println("");
  Serial.println("");
  Serial.println("");
  Serial.println("");


  

 delay(2000);
}

Thanks in advance ! Alain

don't use pin 0 (nor pin 1), they are better suited for uploading your code and debugging with the Serial monitor

you should use an array for the pins and iterate through the array to find the "highest" pin that tests positive and print that.

may be something like this

const byte levelPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
const byte levelCount = sizeof levelPins;

void setup() {
  Serial.begin(115200); Serial.println();
  for (auto &p : levelPins) pinMode(p, INPUT);
}

void loop() {
  byte pinIndex = 0;
  while (pinIndex < levelCount - 1) {
    if ((digitalRead(levelPins[pinIndex]) == HIGH) && (digitalRead(levelPins[pinIndex + 1]) == LOW)) break;
    pinIndex++;
  }
  Serial.print("Water reached level : "); Serial.println(pinIndex);
  delay(1000);
}

fully untested but shorter than your code :slight_smile:

1 Like

Ouch... Thank you Jackson, people than can manage this language are genious for me... i'm not but i'll try to adapt your code ! Alain

:slight_smile: just takes time and practice. You'll get there (and my code might be buggy)

The idea is to iterate through all the pins and check if the current one is HIGH and the next one LOW. if you find that, then you have found the highest point. if not you go to the next pair of pins and try again.

when you exit the while() loop, pinIndex ie either the pin that met the criteria of being the highest HIGH or it's the last one.

this assumes the sensors do work and that you have always a bit of water at level 0

so some fine tuning need

1 Like

Woudn't it be sufficent to start from the bottom and simply see the first sensor that returns '0' ? Without comparing to the previous one. And then only printing the value corresponding to the previous one?

1 Like

that's actually very true :slight_smile: - time for coffee
even simpler (@steppenwolf111 - see no genius here, I made it too complicated)

1 Like

Thanks jackson, it works, i've just to find a way to not showing a value else than "0" when it's empty...actually when no water, it's 20%...but you helped me to make me make the way... good evening from Switzerland ! Alain

Good - as suggested a simple compare would do


const byte levelPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
const byte levelCount = sizeof levelPins;

void setup() {
  Serial.begin(115200); Serial.println();
  for (auto &p : levelPins) pinMode(p, INPUT);
}

void loop() {
  byte pinIndex = 0;
  bool found = false;
  while (pinIndex < levelCount) {
    if (digitalRead(levelPins[pinIndex]) == LOW) {
      found = true;
      break;
    }
    pinIndex++;
  }
  if (found) {Serial.print("Water reached level : "); Serial.println(pinIndex);}
  else Serial.println("Water tank is super full : ");
  delay(1000);
}

:rofl: it's the "poo" tank of my boat... when in this state...stop, no more details !

:rofl:

Don’t let it go above the mid level !

The code is perfect... and good for this project. With my other tanks, i managed to get the readings from an ultrasonic captor that is not afraid to be in contact with the liquid, easyer to read with arduino... now i have to wait the 5 other ( external ) captors i ordered to validate everything...
So you have graciously participated in the peace of my mind and in the health of the geneva lake...

Thanks to you and Microbanner !

Don't forget to use Serial.flush(). Just kidding.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.