Go Down

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

spencerkim


DuffleBag

#46
Nov 11, 2019, 11:19 pm Last Edit: Nov 11, 2019, 11:23 pm by DuffleBag
Not true
Really?? I learned something today then.... :smiley-zipper:

UKHeliBob

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

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 2
  //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);
  }
}


Would this coding make the servo motor turn 90 degrees when water sensed on the water sensor?
It is not working when the water is sensed? What are some corrections that I should make in my coding?
I used some information from here to get my help.https://forum.arduino.cc/index.php?topic=321984.0

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 2
  //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 SensorVal;
  bool result = true; //start assuming all dry, if any one (or more) is wet then set false
  for (int i = 0; i < howManySensors; i++);
  {
    int SensorVal = analogRead;{A1, A3;};
    Serial.print(SensorVal); Serial.print(" ");
    if (SensorVal > sensorWetThreshold);
    {
      result = false;
    }
  }

  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);
  }
}


Or would this coding work better on making the servo motor turn 90 degrees when water is sensed on the water sensor?? I am new to coding, so I don't get this very well

DuffleBag

I'm still not clear as to what is working, and what isn't.  If you're new to coding, try commenting out pieces of your code and trying each thing by itself.

What happens if you change your loop to this?
Code: [Select]

void loop()
{
  //setTheServo(readTheSensors());
  setTheServo(true);
  delay(500);
  setTheServo(false);
  delay(500);
}


If it moves, then you know your servo setup is fine, and the problem is in your logic, or water sensors

sayHovis

What on Earth is this:

Code: [Select]
int SensorVal = analogRead;{A1, A3;};

Looks to me like that should refer to the sensor array, running through them in the "for", so probably:

Code: [Select]
int SensorVal = analogRead(waterSens[i]);



sayHovis

#52
Nov 12, 2019, 06:19 am Last Edit: Nov 12, 2019, 06:19 am by sayHovis
The way you structured that code is good practice, separating the reading of the sensors from the moving of the servo. That is, you send a bool from readTheSensors() to setTheServo(bool dry) with this line:

Code: [Select]
setTheServo(readTheSensors());

So why are you also attempting to position the servo inside readTheSensors():

Code: [Select]

Serial.println("All dry");
servoPos = 90; // ??????


That would defeat the object of the nice tidy way you already have it, so separate the input from the output.




spencerkim

The servo motor has to move when the water is sensed, so I thought that I had to read the sensor inside the set the servo. Also, for the second problem, should I put the input and the output on a different line

spencerkim

I tried doing everything to the servo that was related to the wiring of the circuit. However, it didn't work. I think there is a problem with my coding, but I can't figure it out. Help please!!!

UKHeliBob

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

spencerkim


UKHeliBob

Do you mean servo.attach ?

If that was a question about reply #55 then the answer is no
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

spencerkim

Then, what do you mean by servo sweep

UKHeliBob

Then, what do you mean by servo sweep

Look in the example programs for the Servo library.  One of them is named Sweep.  Does your servo move as expected when you upload the program ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up