Servos and IF statements

I would just like to know if I can use the position of the servo as a condition for a if statement.

Welcome to the forum

Yes, of course you can

You can either use servo.read() to get the last position that you wrote to the servo or, of course, as the code wrote to it then presumably you know the value

What you can't do is have the servo moved by some external force then query it to find out or test its current position

What exactly are you trying to do ?

No. Because you can't read the position from a servo. All you can do is read the position you last told it to be at. So you can't see if it has arrived where you told it to go.

so i have a rover that needs to to multiple actions in a specific order.
1st : The rover has to follow a line on a table .
2nd : Once the rover reaches a specific distance away from a crate/package with the use of a ultrasonic sensor then i use the servo to lift the crate up.
3rd : When the crate has been lifted up the rover must travel 15cm and use the servo to drop the crate/package.
4th : After the crate/package has been dropped off the rover must reverse for 15 and come to a stop.

The current code i have can execute the line following and pick up the crate but the problem comes after i pick up the crate when the line following section of the code kicks in.

the if statements that i use to stop the rover for pick up and execute the line following are the distance values from the ultrasonic sensor and after the crate/package has been picked up the line following if statement is satisfied which is not what i want.

what i would like is to use the position of the servo when it has no package and is in its initial state and when it is raised up as conditions for a if statement to prevent the rover from executing the line following code after pickup,

I apologize if this is a mouthful.

Why use the servo position ? Once the package is picked up set a boolean variable to true instead of false and use its value to control which code sections are executed

Better still, control the program flow using a state machine so that only code relevant at any time is executed

Please post your sketch. Auto format it in the IDE before you copy it and paste it here in code tags

the code is incomplete. there are several undefined variables and i also think missing libraries:

  • controller
  • servo4
  • M1 & M2

presumably some type of gripper is on an arm that can be raised. does the arm nee to be lowered, the gripper closed and the arm then raised?

i see line following code for when distance > 2.4. doesn't this code also need to be used when moving an additional 15 cm

where is the gripper opened to drop the crate?

does it just run backwards? how does it know when to stop?

i assume these command set the duty-cycle which may be simple be from 0-100% and control speed but not direction. they would usually be positive.

i'll guess that these states indicate the robot is off to one side of the line and needs to be turned by running the motors at different speeds -- not running one motor in reverse.

seems like you should have normal and slow speed values. set both to normal to go straight and one to slow to turn

seems appropriate to run forward and in reverse but it seems that when you pick-up or release the crate you just need to perform a sequence of steps possibly using delay to allow the servo(s) to reach final positions.

so maybe only a forward and reverse state (how to stop in reverse)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.