how to set a servo to it's zero/starting position?

Hi you all!

I'm new to Arduino and hzve a smaal project involving some servo's.
I need to know a servo's starting or zero point so my code can start with "pos = 0" instead of "pos = 46"!
Can I do this by changing the basic "Sweep" code into this?

// 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)  // goes from to 0 degrees 
  {                                  
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
  }
  }

You can write a start position before you call attach.

Can I do this by changing the basic "Sweep" code into this?

  for(pos = 0)  // goes from to 0 degrees 
  {                                  
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
  }

No.

A for loop has three parts - the initialization section, the while clause, and the increment section. If you want to do something once, you get rid of the for loop.

Okay....make sence that I don't want to loop. A loop is a constant returning thing. And I only want the servo to go to the starting position and hold there.
I want that start position so I can attache the arm to the servo.
So than it should look like this?

#include <Servo.h>
 Servo myservo;  

int pos = 0;
 void setup()
{
 myservo.attach(9);
}
  
for(pos = 0)
{                                  
 myservo.write(pos);
}

Again, no, that isn't how a for loop looks
Also, it is outside of a function, which isn't allowed in C

Just have the servo go to 0 in setup() and leave loop() empty until you want to do more.

It's much simpler than you think. Just move the myservo.write(pos) into setup() like this

int pos = 0;
 void setup()
{
 myservo.write(pos); // put it before the attach() so it goes straight to that position
 myservo.attach(9);                      
}

...R

1 Like

Thanks for that Robin.
Do I need to include an empty loop into the code or can I leave that completely out for this.
I do think so judging from the "bare minimum code" in the tutorial here:http://arduino.cc/en/Tutorial/BareMinimum

#include <Servo.h>
 Servo myservo;  

int pos = 0;
 void setup()
{
 myservo.write(pos); // put it before the attach() so it goes straight to that position
 myservo.attach(9);                      
}

void loop()
{}

Do I need to include an empty loop into the code

If you want it to compile you do.

That sketch seems real short on useful functionality.

Okay...thanks...then I'll try it soon.

A few last question:

  • can I actualy "destroy" my R3 onu or my servo's by doing something "stupid" in de code?
    Let's say I want to turn the servo 200 degrees instead of 180?
  • I need two servo's to retract my landinggear. What is better: make a servo 1 and servo 2 in the code and use lines of code for that.
    Or can I keep it simpel and pretent there's only 1 servo and connect the servo's together before connecting them to the R3 Uno?
    (I'm going to use the Arduino Micro when every ting is working on the R3 Uno)

It's unlikely your code will damage anything that is wired up correctly to start with.

Driving servos past their limits usually doesn't cause a problem - it certainly won't bother the Arduino.

I guess you could connect the signal wires from two servos to a single Arduino pin - it's worth trying. The potential problem I see is that the two servos may interpret (for example) 10 degrees slightly differently. If you drive them separately with the Arduino you can compensate for that.

...R