I just need to clarify that the program I am currently working on is doing what I think it is. I have an AdaFruit Motor Shield connected to an Arduino Uno, which will be powering a a servo.
So am I right in thinking that, in chronological order: the servo takes 50ms to reach its initial point (1) before detaching from pin 9 for 5 seconds. The servo is then reattached to the pin 9 and is set to return to its primary position within 50ms. This entire process is placed within the programs void loop, meaning that the process will continuously repeat itself until power is no longer supplied to the Arduino Uno.
The sketch has no idea what the position of the servo is, so it may take no time to reach the 1 "degree" position, or it may take longer than 50ms. There is no way of knowing.
AWOL:
The sketch has no idea what the position of the servo is, so it may take no time to reach the 1 "degree" position, or it may take longer than 50ms. There is no way of knowing.
Would you be able to offer any guidance as to how to incorporate the position into my sketch? Or is my sketch impractical?
It might be. Depends on if the servo is able to reach the position in that 50ms.
If you really want to detach the servo you have to know the time a full sweep, from 0 to 180, will take and use that. Because it can't take any longer then the full sweep. Or, just don't detach it.
PS Use code tags next time
PSS Use variables next time. A name is easier to remember then a random number when a program gets bigger.
PSSS And you want to stop using delay() if you want to be able to do anything else besides sweeping a servo
Avocado232:
is set to return to its primary position within 50ms.
There is a fundamental misunderstanding here.
When the Arduino sends Servo.write(pos); it starts the servo moving and the servo will continue moving at its own pace (admittedly that is usually fast) until the position is reached. However the Arduino has no way to find out when that happens.
Immediately after the Servo.write() statement the Arduino executes the next statement while the servo is moving.
Think of the process like throwing a ball over a wall to someone you can't see, and as soon as the ball leaves your hand you go back to reading your book.
The purpose of the delay(50) is to allow a guesstimated time for the servo to complete its movement. Whether that is sufficient or not depends on the speed of the servo and how far it has to move.
By the way, it is usual to attach the servo in setup() and leave it attached throughout the program unless there is a special reason not to.
Robin2:
There is a fundamental misunderstanding here.
When the Arduino sends Servo.write(pos); it starts the servo moving and the servo will continue moving at its own pace (admittedly that is usually fast) until the position is reached. However the Arduino has no way to find out when that happens.
Immediately after the Servo.write() statement the Arduino executes the next statement while the servo is moving.
Think of the process like throwing a ball over a wall to someone you can't see, and as soon as the ball leaves your hand you go back to reading your book.
The purpose of the delay(50) is to allow a guesstimated time for the servo to complete its movement. Whether that is sufficient or not depends on the speed of the servo and how far it has to move.
By the way, it is usual to attach the servo in setup() and leave it attached throughout the program unless there is a special reason not to.
...R
Thank you for the reply, it was most helpful! My plan was to control the speed in which the servo gets from specific point A to specific point B, but based on the responses this is not feasible. Would you recommend that I substitute the servo for another electronic component and revise a different program?
Thank you for the reply, it was most helpful! My plan was to control the speed in which the servo gets from specific point A to specific point B, but based on the responses this is not feasible.
That is NOT true. If you KNOW where the servo is and where you want it to be, you can make the servo take as long as you want to get from here to there. What you can NOT do is ask the servo where it is. It has no way of telling you.
A stepper motor with an absolute rotary encoder would be able to tell you where it is. Absolute encoders are expensive, though. A stepper motor with a regular rotary encoder could be moved from wherever it is, by a relative amount, easily. To learn exactly where the stepper (or servo) with relative encoder is requires additional hardware (some kind of limit switch or hall effect sensor) and some additional programming (rotate until you trigger the limit switch or hall effect sensor).
Indeed. After the first command (and some time) you KNOW where it is. After that you can send it to new positions in the speed you like as long its NOT faster then the servo can move.
Avocado232:
My plan was to control the speed in which the servo gets from specific point A to specific point B
If you look at how the servo is controlled in Several Things at a Time you will see how to do that. The trick is to get it to move in small steps with a suitable interval between movements.