Newbie Question: Make a servo turn 90 degrees and stop.

I just want my servo to turn 90 degrees then stop. At that point I am done with the ardunio pro and would like to kill power to the servo until it is need the next show. I am running it off a 9 volt battery.

Thanks for any help.

I am using this example and can make the servo turn...WOW.

// Sweep
// by BARRAGAN http://barraganstudio.com
// This example code is in the public domain.

#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
}

void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}

You will need to detach the relevant servo pin, and set it to be LOW, then turn off power to the servo (a PNP or p-channel transistor to switch the maximum current the servo can draw - search for "high-side switch").

Small 9V batteries are both too high a voltage for most servos and too little current to actually power them. 6V from 4 AA's would be better unless your servo is specifically for 9V

I just want my servo to turn 90 degrees then stop. At that point I am done with the ardunio pro and would like to kill power to the servo until it is need the next show. I am running it off a 9 volt battery.

Your wimpy little battery will take care of killing power to the servo soon enough.

I am using this example and can make the servo turn...WOW.

Are you unable to determine HOW that code makes the servo move? The comments are stating the obvious, so it shouldn't take much effort on your part to highlight two lines, press delete, highlight 7 more lines, and press delete again. Then, change the initial value of pos, compile and upload, and you are done.

Thanks for the fast reply. That is very helpful.

How do I tell the sketch to stop turning the servo.

I am so new at this I can't write C yet :frowning:

I can see the code and will try that. Sorry for being such an idiot. I just got this today and am trying to get my pea sized brain around it.

Thanks.

You win Paul. ;-). I admit I can't read the code properly. I tried to figure out what 2 lines to delete and then what 7 lines to delete and failed. I got the servo to do all kinds of crazy stuff but not turn 90 and stop.

:frowning:

If you post your code, we can help you better, otherwise it looks like we'd be writing it for you, and there'd be no fun in that for you.

Totally agree. Just trying to get things started here. Soldering....setting up bluetooth....all very exciting.

Here is the example I started with:
// Sweep
// by BARRAGAN http://barraganstudio.com
// This example code is in the public domain.

#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
}

void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}

This works as expected. I have tried 10 different combinations. Here my latest attempt.

// Sweep
// by BARRAGAN http://barraganstudio.com
// This example code is in the public domain.

#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
}

void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
{ delay(15); // waits 15ms for the servo to reach the position
}
}

the server stops then reverses direction ?

thanks for the help. once i get the concept down this will all be easier.

OK, with that last code you posted, you code sweeps from 0 to 179 degrees with steps of 15milliseconds between them, then it waits another 15 milliseconds (the braces aren't needed) then loop() exits...and gets called again, so the sweep starts over again.
Does that make sense?

Here my latest attempt.

So, by now, you've figure that the thing that makes the servo actually move is

servo.write(pos);

Since you only want the servo to move once, and there are two functions, setup() and loop(), and one of them is executed once, and one is executed over and over, which one should contain the single statement to move the servo?

What value should be in pos to move the servo to 90?

Baby steps.

So putting the code in the setup is what I want.

I did this....(and several other things) and the server continues to move in one direction and does not stop.

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);
delay(200);
myservo.write(0);
}

void loop()
{

}

Add Serial.begin(115200); and Serial.println("Setup called"); to setup(). See if your Arduino is constantly resetting.

I thought this might do the trick, but no.

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

}

void loop()
{
myservo.write(0);
delay(15);
myservo.write(120);
delay(1000);

}

Are you saying the servo keeps on rotating without hitting end stops?
This isn't a so-called continuous rotation servo is it?

Tried this...
#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(100);
Serial.begin(115200);
Serial.println("Setup called");

}

void loop()
{

}

Just goes slowly in one direction and doesn't stop.

It is ROB-09347 Large full rotation

Despite what you've been told by whoever sold you it, that's not a servo.
Sorry

Edit: the below is of no use now that the the OP identified his servo as a "ROB-09347 Large full rotation" servo, as it is a "continous rotation" servo.

I just want my servo to turn 90 degrees then stop. At that point I am done with the ardunio pro and would like to kill power to the servo until it is need the next show. I am running it off a 9 volt battery.

Time for somebody to start using the code tag # to put their code in a code box. Below is a way to have a servo move to a specific position in the setup portion of the code. Your request to "turn 90 degrees then stop" is not clear in reguards to how servos operate. You can move a servo to a specific position, but there is no feedback from the servo to the arduino as to the current servo position when the arduino starts up.

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control 
  Serial.println("servo-test-22"); // so I can keep track of what is loaded
}

Thanks.

Here is a link to the "servo" I am using. I think it is a servo?

Think what you like, that isn't a servo.
I wish vendors would stop describing them as such.
Read the comments at the bottom of the page.