I'm new to arduino and am trying to code my servo into moving 90 without looping. I want to execute the code so the motor moves 90 degrees then stops.
Compared to some of the things i have seen the arduino do, this is surely a simple process yet i cant get my head around it.
Here is a sample of the code im using...
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo.write(180); //starts servo moving as fast as it can go CCW
delay(400); //waits for the server to move
myservo.write(90); //stops the servo
}
void loop()
{
}
When i enter this code the servo moves about 45 degrees then returns to its staring position.
Any input would be appreciated
(Servo: Futaba S3003)
(Arduino Uno R3)
(Wired Directly from the Arduino board)
I'm fairly new to arduino myself but I'm not sure why you would move the servo twice in the setup(), if you must have you tried using a delay after the second write() command?
I suggest using void loop() to do any movements aside from your starting position, good luck!
There may be a problem powering the servo from the Arduino. It is best to give the servo its own power supply with a common ground with the Arduino. If the servo draws too much current from the Arduino there can be very strange behaviour and possible damage to the Arduino.
As you have a problem your code is too complex - it has two parts in it myServo.write(180) followed by myServo.write(90).
When you have a problem test it with the simplest possible code. Drop the second write() and see what happens. Change the 180 to other values and see if it behaves properly.
If all works well with just a single write() then reinstate the second write() and increase the delay() to see what effect that has.
Also keep in mind that the arduino servo commands using degree values is based on assumptions that your servo may not apply. The servo library assumes all servos are capable of moving a full 180 degrees and that may not be the case of your specific servo model. You should first just test sending a 0 degree command and marking that as one travel limit, then a 180 degree command and see how many actual degrees your servo can move using the servo library's min and max assumption values.
After messing around with some of the values i have realized that the power is indeed the issue, like you implied Robin2 sometimes its fine and others it just behaves strangely. Has any one got any advice on sustaining a servo motor, and how many volts will i need to make it work fluently?
Clembo69:
After messing around with some of the values i have realized that the power is indeed the issue, like you implied Robin2 sometimes its fine and others it just behaves strangely. Has any one got any advice on sustaining a servo motor, and how many volts will i need to make it work fluently?
It's not an issue of the voltage, as the arduino shield 5V pin is a regulated +5vdc (in the sweet spot for servos), it's a matter of the amount of current a servo can draw which is more then you should try and draw directly from a arduino board. Even a 4 cell AA battery pack would be an improvement wired to just power the servo and also add a wire from the battery negative terminal to one of the shield ground pins.
Ive done as you said, Erased part of the code to simplify it and have common ground with the arduino. The arduino has its own power supply from a 9v battery. Its still acting strange, when i input my code...
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo.write(180); //starts servo moving as fast as it can go CCW
delay(400); //waits for the server to move
}
void loop()
{
}
The servo will execute the code once or twice each time the code is uploaded. After this the servo will act strange and sometimes will not even move each time the code is uploaded until i change the power source, then the same happens again. when i upload an example from the arduino library, the servo does as it is meant to with out any issues.
Assuming you mean a PP3 type of 9v battery - they can't provide enough current for the Arduino except for a short period when they are brand new.
Going back to my theme of just trying one thing at a time --- power your Arduino from the PC USB connection until you have everything else working properly. Then if you switch to another power source you will know exactly where the problem lies.
A good question to ask yourself at every stage is "if this doesn't work how will I know why?"
As R mentioned, the power was an issue but have an external power source for the motor which also runs 5 volts but does the job.
I was confused about the code, I thought i needed to tell the arduino i wanted to move 90 degrees, but i just needed to tell the arduino what variable positions i want the motor to go to and from. For example...