Search code examples
arduinoarduino-c++

Why can't X be used as a function?


Sorry for the messy codebase. I am new to C++.

I'm trying to tie a loop and function together. (1) A moisture sensor and (2) a servo to turn a lever on and off based on moisture.

I'm receiving an error that 'servo' cannot be used as a function. I've tried changing servo to some other name. I'm not using servo anywhere else, such as a variable, so that doesn't seem like the issue.

Does anyone have any other advice?

#include "Servo.h"

Servo myservo;  // create servo object to control a servo

int pos = 0; // servo position variable
#define SensorPin A0 
float sensorValue = 0; 
bool lever = 0; // soil moisture sensor variable; 0 = dry; 1 = wet;
bool pump = 0; // pump is set to 0, which = off

// servo function
void servo(bool);

void setup() {
  Serial.begin(9600); // soil moisture sensor setup
  myservo.attach(9); // servo setup
}

// soil moisture sensor loop
void loop() { 
  for (int i = 0; i <= 100; i++) { 
    sensorValue = sensorValue + analogRead(SensorPin); 
    delay(20);
  } 
  sensorValue = sensorValue/100.0; 
  Serial.print("Moisture level: ");
  Serial.print(sensorValue);
  Serial.print(";");
  
  if(sensorValue > 1000) {
    lever = 0; // dry
  }

  if(sensorValue < 1000) {
    lever = 1; // wet
  }

  switchOnOff(lever);
  
  delay(1000);
}

// define the servo function
void switchOnOff(bool lever) {
  Serial.print(" Lever: ");
  Serial.print(lever);
  Serial.print(";");

  Serial.print(" Pump: ");
  Serial.print(pump);
  Serial.println(";");
  
  if(lever == 0 && pump == 0) { // it is dry and pump is off, pump needs to be turned on
    for (pos = 90; pos <= 180; pos++) { // goes from 0 degrees to 90 degrees
      // in steps of 1 degree
      myservo.write(pos);             // tell servo to go to position in variable 'pos'
      delay(15);
    }
    delay(4000);
    for (pos = 180; pos >= 90; pos--) { // goes from 90 degrees to 0 degrees
      // in steps of 1 degree
      myservo.write(pos);             // tell servo to go to position in variable 'pos'
      delay(15);
    }
    pump = 1; // noting that pump is on
  }

  if(lever == 0 && pump == 1) { // it is dry and pump is on
    return; // do nothing, return
  }

  if(lever == 1 && pump == 1) { // it is wet and pump is on, pump needs to be turned off
    for (pos = 90; pos <= 180; pos++) { // goes from 90 degrees to 0 degrees
      // in steps of 1 degree
      myservo.write(pos);             // tell servo to go to position in variable 'pos'
      delay(15);
    }
    delay(4000);
    for (pos = 180; pos >= 90; pos--) { // goes from 0 degrees to 90 degrees
      // in steps of 1 degree
      myservo.write(pos);             // tell servo to go to position in variable 'pos'
      delay(15);
    }
    pump = 0; // noting that pump is off
  }

  if(lever == 1 && pump == 0) { // it is wet and pump is off
    return; // do nothing, return
  }
}

Thank you.


Solution

  • The problem is in that you try to use the servo() function before properly declaring it. First, you need to declare a function before it can be mentioned in your code. Second, your definition for the servo() is incorrect. The int servo(lever), where lever is not a type. You should look toward the following: int servo(bool lever);, where lever is a parameter of type bool which the servo() function takes.

    Then, your function does not return anything, it only has some "side-effects" such as myservo.write(pos);. So, it should be void.

    Try the following arrangement:

    #include "Servo.h"
    
    Servo myservo;  // create servo object to control a servo
    // twelve servo objects can be created on most boards
    
    // Declare the function
    void servo(bool);
    
    int pos = 0;    // variable to store the servo position
    #define SensorPin A0 
    float sensorValue = 0; 
    bool lever = 0; // lever is global variable of type bool
    
    
    void setup() {
      myservo.attach(9);  // attaches the servo on pin 9 to the servo object    
      Serial.begin(9600); 
    }
    
    
    void loop() { 
      for (int i = 0; i <= 100; i++) { 
        sensorValue = sensorValue + analogRead(SensorPin); 
        delay(1); 
      } 
      sensorValue = sensorValue/100.0; 
      Serial.println(sensorValue); 
      delay(30);
      
      if(analogRead(0 > 100)) {
        lever = 1;
      }
    
      if(analogRead(0 < 100)) {
        lever = 0;
      }
    
      servo(lever);
    }
    
    
    // Define the function
    void servo(bool lever) {
      if(lever == 1) { // On
        for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
          // in steps of 1 degree
          myservo.write(pos);              // tell servo to go to position in variable 'pos'
        }
      }
      if(lever == 0) { // Off
        for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
          myservo.write(pos);              // tell servo to go to position in variable 'pos'
        } 
      }
    }
    

    P.S. There was also a mess with setup() being redefined here and there.