Go Down

Topic: Questions about Programming a Servo Motor [Solved] (Read 1 time) previous topic - next topic

spencerkim

Nov 01, 2019, 11:04 pm Last Edit: Nov 20, 2019, 12:56 am by spencerkim
Code: [Select]

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

UKHeliBob

Code: [Select]
  }//readTheSensors()Is that actually the end of the function ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

groundFungus

#2
Nov 01, 2019, 11:30 pm Last Edit: Nov 01, 2019, 11:32 pm by groundFungus
You are missing a } to close the readTheSensors() function definition.

Code: [Select]
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?

Code: [Select]
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.

spencerkim

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

spencerkim

Code: [Select]

#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

groundFungus

Quote
doesn't work
Provides no useful information.   What does the code do?  How does that differ from what you want?

spencerkim

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

hammy

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

lastchancename

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.
Experienced responders have a nose for laziness, (they were beginners once)... Sure, there are trolls, chest-beaters, and pretenders - but the help you'll get here is about as good as it gets - if you try to help youself!.

spencerkim

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

lastchancename

#10
Nov 02, 2019, 10:20 pm Last Edit: Nov 02, 2019, 11:47 pm by lastchancename
incorrect.   a servomotor is a type of (usually) dc motor.
Experienced responders have a nose for laziness, (they were beginners once)... Sure, there are trolls, chest-beaters, and pretenders - but the help you'll get here is about as good as it gets - if you try to help youself!.

Blackfin

Do you Serial.prints produce the expected results?


Blackfin

No, it does not

Well, what do you see?

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

Blackfin

#14
Nov 03, 2019, 02:10 am Last Edit: Nov 03, 2019, 02:55 am by Blackfin
Also:

Code: [Select]
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...

Go Up