Trouble with serial monitor (beginner)

The following is a small part of my code.

Is there an easy way I can print the outcome of the "void waterLevelSensor" after the first Serial.print?

void loop () {

Serial.print("Water level: "); Serial.println(**I WANT IT HERE**);
  
 
  WaterLevelSensor();
 
}


void WaterLevelSensor() {
  waterSensorValue = analogRead(waterSensor);


 
  if (waterSensorValue <= 200) {
   
    Serial.print("0mm");
  }

 else if (waterSensorValue > 200 && waterSensorValue <= 400) {

    Serial.print("0 to 5mm");
}

The function doesn't return anything i.e. it has no "outcome". But it does seem to print something. If it's not already printing it where you say you want it then where is it being printed?

Steve

What outcome do you want to print ?

@OP

Do you want print out like this -- ?

Water level: 0mm

or

Water level: 0 to 5mm

scr-13.png

scr-13.png

What is the reason you want it there? Why don't you just move all the Serial.print commands to the WaterLevelSensor function?

Dermaptera:
What is the reason you want it there? Why don't you just move all the Serial.print commands to the WaterLevelSensor function?

Maybe because it's a good idea to keep user interface code separate from other code.

sterretje:
Maybe because it's a good idea to keep user interface code separate from other code.

Why would it be a good idea to do part of the printing directly in the loop() and part in the WaterLevelSensor function? I would do all the printing in the same place, either in the WaterLevelSensor function or in the loop and then change the WaterLevelSensor that it would return the relevant value. What are the advantages of splitting it up?

Edit: sorry sterretje, of course you are right, I misunderstood your comment.

Dermaptera:
Why would it be a good idea to do part of the printing directly in the loop() and part in the WaterLevelSensor function? I would do all the printing in the same place, either in the WaterLevelSensor function or in the loop and then change the WaterLevelSensor that it would return the relevant value. What are the advantages of splitting it up?

That is excactly what I'm trying to do. I want the values from the void WaterLevelSensor to be printed up in the loop part

Have you read https://www.arduino.cc/en/Reference/FunctionDeclaration on how to declare a function with a return value?

Dermaptera:
Have you read https://www.arduino.cc/en/Reference/FunctionDeclaration on how to declare a function with a return value?

Thanks for the tip! I've read it and tried to understand and make it work for over an hour now, but I still can't quite seem to get it.

If someone still wants to help, I've put the entire thing as an attachment here for you guys to look on

Weatherstation.ino (4.65 KB)

char textBuffer[32];

void setup()
{
  ...
  ...
}

void loop ()
{
  Serial.print("Water level: "); Serial.println(WaterLevelSensor());
}

/*
  Measures the water level and returns a text string
  Returns
    (pointer to) text string
 */
char* WaterLevelSensor()
{
  // read water sensor
  waterSensorValue = analogRead(waterSensor);

  // prepare text
  if (waterSensorValue <= 200)
  {
    strcpy(textBuffer, "0mm");
  }
  else if (waterSensorValue > 200 && waterSensorValue <= 400)
  {
    strcpy(textBuffer, "0 to 5mm");
  }
  else
  {
      strcpy(textBuffer, "--");
  }

  // return pointer to text
  return textBuffer;
}

I would however consider to write it differently; one reason is that if you need the measurement for something else, you will need to do another analogRead().

@OP

I have moderated your codes of original post in the following way so that now all printings can take place in the loop() function.

int  waterSensorValue;
#define waterSensor A0  //sensor is connected at A0-pin of ADC

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

void loop ()
{

  Serial.print("Water level: "); //Serial.println(**I WANT IT HERE**);
  char *x = WaterLevelSensor();
  Serial.println(x);
  delay(5000);

}

char *WaterLevelSensor()//void WaterLevelSensor()
{
  waterSensorValue = analogRead(waterSensor);
  if (waterSensorValue <= 200)
  {

    return "0mm";//Serial.println("0mm");
  }

  else
  {
    if (waterSensorValue > 200 && waterSensorValue <= 700)
    {
      return "0 to 5mm";//Serial.println("0 to 5mm");
    }
  }
}

scr-13.png