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.
#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
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
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 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?
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.