Questions about Programming a Servo Motor [Solved]

#include <Servo.h> //include servo library
Servo myservo; //define servo as servo

byte waterSens[] = {A0, A1, A2};
const byte howManySensors = sizeof(waterSens) / sizeof(waterSens[0]);

int servoPos;
const byte servoDryPos = 60;  //
const byte servoWetPos = 50;  //
const int sensorWetThreshold = 450; //

void setup()
{
  Serial.begin(9600);
  Serial.println("https://forum.arduino.cc/index.php?topic=641224");
  myservo.attach(9);//attach servo to pin 9
  //also just going to use led13 as a signal
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.print("Number of sensors "); Serial.println(howManySensors);
  Serial.println("setup() done");
  delay(1000);
}

void loop()
{
  setTheServo(readTheSensors());
  delay(500);
}

bool readTheSensors()
{
  int currentSensorVal;
  bool result = true; //start assuming all dry, if any one (or more) is wet then set false
  for (int i = 0; i < howManySensors; i++)
  {
    currentSensorVal = analogRead(waterSens[i]);
    Serial.print(currentSensorVal); Serial.print(" ");
    if (currentSensorVal > sensorWetThreshold) 
    {
      result = false;
      break;}
  }

  if (result) {
  Serial.println("All dry");
  servoPos = 0
  
  else Serial.println("At least one sensor is wet"){
  servoPos = 90
  }
  return result;

}//readTheSensors()

void setTheServo(bool dry)
{
  if (dry)
  {
    myservo.write(servoDryPos);
    digitalWrite(LED_BUILTIN, LOW);
  }
  else
  {
    myservo.write(servoWetPos);
    digitalWrite(LED_BUILTIN, HIGH);
  }
}

I wrote this code and I tested to see if it will work and it didn’t. I want the servo motor to turn 90 degrees whenever water is detected at one or more of the three water level sensors and the servo motor should come back to the starting position when water is not detected. Also, it said that setTheServo was not detected in the code. How am I suppose to fix these problems?

  }//readTheSensors()Is that actually the end of the function ?

You are missing a } to close the readTheSensors() function definition.

if (result) {
  Serial.println("All dry");
  servoPos = 0
 
  else Serial.println("At least one sensor is wet"){
  servoPos = 90
  }

Take a look at the syntax in that statement. It is not right.

Should be?

if (result)
   {
      Serial.println("All dry");
      servoPos = 0
   }
   else
   {
      Serial.println("At least one sensor is wet");
      servoPos = 90
   }

Use the autoformat tool to indent your code. Mistakes like that will often show up. If you put every { and every } on their own lines it is easier to see if one is missing or misplaced.

Yes, I had some problems after that point on setting the servo motor.

#include <Servo.h> //include servo library
Servo myservo; //define servo as servo

byte waterSens[] = {A1, A3};
const byte howManySensors = sizeof(waterSens) / sizeof(waterSens[0]);

int servoPos;
const byte servoDryPos = 90;  //
const byte servoWetPos = 180;  //
const int sensorWetThreshold = 600; //

void setup()
{
  Serial.begin(9600);
  Serial.println("https://forum.arduino.cc/index.php?topic=641224");
  myservo.attach(2);//attach servo to pin 9
  //also just going to use led13 as a signal
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.print("Number of sensors "); Serial.println(howManySensors);
  Serial.println("setup() done");
  delay(1000);
}

void loop()
{
  setTheServo(readTheSensors());
  delay(500);
}

bool readTheSensors()
{
  int currentSensorVal;
  bool result = true; //start assuming all dry, if any one (or more) is wet then set false
  for (int i = 0; i < howManySensors; i++)
  {
    currentSensorVal = analogRead(waterSens[i]);
    Serial.print(currentSensorVal); Serial.print(" ");
    if (currentSensorVal > sensorWetThreshold) 
    {
      result = false;
      break;}
  }

  if (result) 
  {
    Serial.println("All dry");
    servoPos = 90;
  }
  else 
  {
    Serial.println("At least one sensor is wet");
    servoPos = 180;
  }
  return result;

}//readTheSensors()

void setTheServo(bool dry)
{
  if (dry)
  {
    myservo.write(servoDryPos);
    digitalWrite(LED_BUILTIN, LOW);
  }
  else
  {
    myservo.write(servoWetPos);
    digitalWrite(LED_BUILTIN, HIGH);
  }
}

I updated my code to this, but it still doesn’t work. What should I change so that the servo motor turns 90 degrees and remain at that position if one of the two water sensors sense the water and come back to the original position when the water is gone

doesn't work

Provides no useful information. What does the code do? How does that differ from what you want?

The code does not make the servo motor turn. If I plug in the battery(9V), the servo motor just starts to move by itself for 5 seconds and stops. However, I have to make it move 90 degrees only when water is sensed on the water sensor

You can’t run this stuff off a small 9v battery - it can’t supply the current .

Do you have a SERVO, or servo MOTOR ?
Big difference... one needs the servo library or similar, the other uses an h-bridge or driver to provide the control voltage.
Servos have all the goods in a single box, usually for RC hobby applications.
Servo motors are usually seen in larger automation / industrial control projects.

I have servos. Not servo motors. However, they are both called servo motors, but one is RC servo motors, and the other is industrial servo motors

incorrect. a servomotor is a type of (usually) dc motor.

Do you Serial.prints produce the expected results?

No, it does not

spencerkim:
No, it does not

Well, what do you see?

You know, the more detail you offer, the quicker the debug process will be.

Also:

myservo.attach(2);        //attach servo to pin 9

Why does your comment say pin 9 but your call to attach references pin 2?
The Servo library only supports pins 9 or 10.

Edit: N/M, misread library reference...

The Servo library only supports pins 9 or 10.

Where did you get that information?

Servo library reference.

AnalogWrite on pins 9 and 10 are disabled by the Servo library.

groundFungus:
Where did you get that information?

Servo library reference.

AnalogWrite on pins 9 and 10 are disabled by the Servo library.

Apologies, my mistake: I read this:

https://www.arduino.cc/en/Reference/ServoAttach

but only just noticed that applied to "...Arduino 0016 and earlier...".

I got that information from my friend. I see the light coming on the water sensor and the servo motor not turning on. Where am I suppose to connect the servo motor?

Well, that depends on whether it’s an RC servo or a servo motor,
Please post a link to the product or datasheet