0

This is my Arduino code that checks the water Level of two different water reservoirs via 2 water Level Sensors A and B:

#define Water_Level_A 5;
#define Water_Level_B 6;

boolean Water_Level_Check_A(){
boolean Result = digitalRead(Water_Level_A);
return Result;
}
boolean Water_Level_Check_B(){
boolean Result = digitalRead(Water_Level_B);
return Result;
}

void setup() {
    Serial.begin(9600);
    pinMode(Water_Level_A,INPUT);
    pinMode(Water_Level_B,INPUT);
    }

void loop() {
if (Water_Level_Check_A() == LOW){
--> Water Pump A
}
if Water_Level_Check_B() == LOW{
--> Water Pump B
}

What's the best way to simplify this code so that I only need one Water_Level_Check function so that the distinction between A and B and Water_Level_A and Water_Level_B is made within the function and the loop part looks like this:

void loop() {
if (Water_Level_Check_(A) == LOW){
--> Water Pump A 
}
if Water_Level_Check_(B) == LOW{
--> Water Pump B
}
  • How about giving the function the pin number to read from as a parameter? In this case you would write if(Water_Level_Check(Water_Level_A) == LOW) – chrisl May 27 '18 at 15:47
1

I think what you're asking for is something like this:

boolean Water_Level_Check(uint8_t level_pin){
   boolean Result = digitalRead(pin);
   if( level_pin == Water_Level_A ){
      // manage pump A here
   }
   else if( level_pin == Water_Level_B ){
      // manage pump B here
   }
   else {
      Serial.println("Water_Level_Check: unknown pin number.");
      // or some other way of dealing with a bad-pin-number bug.
      return -1;
   }

   return Result;
}

for the level testing function, which will simplify you loop function to:

void loop() {
   (void)Water_Level_check(Water_Level_A);
   (void)Water_Level_check(Water_Level_B);
}

The (void) expression before each call is a "cast to void", telling the the compiler to consider the function's boolean return value to have been intentionally "voided", i.e. you intended not to read it, and not to give an error message.

3

Functions like

boolean Water_Level_Check_A(){
   boolean Result = digitalRead(Water_Level_A);
   return Result;
}

Are really overcomplicated. You're just calling one function and returning that value, so it can be simplified to

boolean Water_Level_Check_A(){
   return digitalRead(Water_Level_A);
}

Or just the expression

digitalRead(Water_Level_A)

whenever you need it.

So all you're doing is calling digitalRead() on a pin number and check if the result is low. If you really want to move this to a different function then you could give it the pin number argument and directly invert the result so that a true from this function means that some action needs to be done.

boolean Water_Level_Check(int sensorPin){
   return !digitalRead(sensorPin);
}

And then call it as

void loop() {
   if (Water_Level_Check(Water_Level_A)){
       //--> Water Pump A
   }
   if (Water_Level_Check(Water_Level_B )){
       //--> Water Pump B
   }
}
  • The boolean Water_Level_Check_A() I'm using in my code also includes the Powering On and OFF of the Water Level Sensor. That's why I used the function in the first place. The code here is just the minimal example for my question on how to change A and B in the function. The return !digitalRead(sensorPin) looks cleaner than my approach :-) Thanks – Peter S May 27 '18 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.