Questions about Programming a Servo Motor [Solved]

#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:

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

I suggest that you split

  setTheServo(readTheSensors());

so that you can print the value returned by readTheSensors() to see whether it matches your expectations

What do you see when you print currentSensorVal ?

You might want to supply a diagram of your wiring. There is not enough current available on the 5V pin on the arduino to power a servo and I guess that is your problem.

Diagram of my wiring

OPS IMAGE