Arduino and Servo Motor

Hi, I’m new to arduino and I was experimenting with the servomotor code:

#include <Servo.h>

Servo Servo;

int servoPin = 9;

void setup()
{

Servo.attach(servoPin);

}

void loop()
{

for (int i=30; i<=90; i=i+60)
{
Servo.write(i);
delay(3000);
}
}

The Servo motor goes waaaaaaay too fast, how do I make it go slower? I tried plugging it into the 3.3 volts pin instead of the 5V one, and it did delay it a little bit, but it’s still going too fast…what piece of code can I attach to slow it down? (new to coding too).

Another thing is that this code makes it rotate and come back to the position without stopping.

I just need it to rotate in an angle (slowly) and come back to the position, that’s it. Don’t want an infinite loop…

Can anyone help?

Thanks!

You have a single step of 60 "degrees". Try reducing the for loop increment.

Thanks, I did, it’s looking good now:

#include <Servo.h>

Servo AnasServo;

int servoPin = 9;

void setup()
{

AnasServo.attach(servoPin);

}

void loop()
{

for (int i=30; i<=100; i=i+5)
{
AnasServo.write(i);
delay(4000);
}
}

but, how do i make it stop??? it keeps going, i want the loop to stop after completing the for loop the first time?

what do i do? another for loop before this for loop?

And I did connect the grounds, otherwise it wouldn't be working.

Put all the code into setup()

i get an error...if i get rid of the "void loop"

how can i make it only go once? It shouldn't be that difficult, but i can't figure it out!

I said put all the code into setup, not get rid of loop.

Sorry, I don't get it, can you write it for me?

#include <Servo.h>

Servo AnasServo;
int servoPin = 9;

void setup()
{
  AnasServo.attach(servoPin);
  for (int i=30; i<=100; i=i+5)
  {
    AnasServo.write(i);
    delay(4000);
  }
}

void loop()
{
}

Thanks, but the servo motor doesn't come back?

HOw can I make come back to the original place?

Add another servo.write() statement to return it to it's original position, or a loop to make it return slowly.

Try something. If it doesn't work, come on back.

works!

thanks for your help!!!!! :)

Yes, the increment of 60 was way off, you probably got the motor in a panic trying to keep up. I want to explain how a loop works so that this makes sense to you instead of looking foreign. code is your friend.

for (int i = 0, i < 180, i++) {
}

for whatever is in the parenthesis, do what’s between the brackets { }
int i = 0 means we just made up an integer (non-decimal number) named i and we set the value to zero.
then a comma. i < 180 means that as long as i is less than 180 or whatever number you write, the computer will do what’s in the { } brackets. so you can see there will be a time when i IS greater than 180 by the next statement,

i++ means that every time we go through this loop, we add 1 to the value of i.

So in you code, adding sixty degrees every loop was crazy! Does this make sense?

-Jonathan Brand

No, no...I'm programming a servo motor, so I need it to move 60 degrees, that's why i did increments of 60, but thanks for explaining code to me :)