Servo initial position and (unwanted)movement

Hi there,

I’m dealing with a s3003 servo:

I have this code that everytime I push the button it makes the servo go to a randomly determined position (at a certain and randomly generated “speed”) and go back to the initial position.

The thing is that when I connect the arduino board the servo goes to this X position, and also the first time I press the button It seems to go to that initial X position.

Please see what I mean:

https://youtu.be/Jdtc1Faeelo (I don’t know how to insert a youtube video)

I don’t want any of those two movements at all, any ideas?

This is the code, it’s a mix of the sweep code from the arduino examples and the ditigal read example:

int pushButton = 2;

#include <Servo.h>
Servo myservo;

void setup() {
  myservo.attach(9); 
  pinMode(pushButton, INPUT);
  digitalWrite(pushButton, HIGH);
  Serial.begin(9600);  
} 

void loop() {
  int buttonState = !digitalRead(pushButton);

  if (buttonState == HIGH){
    movimiento();
  }
  delay(10);
}

void movimiento(){
  float pos = 0;                                   // variable that stores servos position
  float incrementoINT = random(1,5);    // de a cuando aumenta o disminuye la posicion (la variable pos)
  float incrementoFLOAT = incrementoINT/100;
  int angulo = random(10,26);              // angle that sets the max. length of the servos movement
  
  Serial.print("Angle: "); 
  Serial.print(angulo);
  Serial.println("");
  Serial.print("Increment: ");
  Serial.print(incrementoFLOAT); 
  Serial.println("");
  
  for(pos = 0; pos < angulo; pos += incrementoFLOAT) {   
    myservo.write(pos);
    delay(15); 
  }  
  for(pos = angulo; pos>=1; pos-= incrementoFLOAT) {     
    myservo.write(pos);
    delay(15); 
  }
}

In setup(), give it a position you want it to go to before you attach the servo. If it's already in that 'zero' position then it won't move.

MorganS: In setup(), give it a position you want it to go to before you attach the servo. If it's already in that 'zero' position then it won't move.

Genius! thanks!

BTW Do you know if I'm handling the servo speed "correctly" I couldn't think of any ohter way to deal with it.

Yes, although it's unusual to use a float as the counter variable in a for() loop. For this usage it seems appropriate. Just don't try to add 1 to 100,000,000 because the result is 100,000,000 due to the limited precision of the float, and your for() loop would never end.

I would also make that servo starting position a #define constant at the top of the code, because it will be used in two places: setup() and the first for() loop. Even if it's zero now, you might need to change it in the future.

Thanks again!

I fixed the code regarding the initial position of the servo:

int pushButton = 2;

#include <Servo.h>
Servo myservo;

#define pos_ini 0 // I define the initial position value to 0

void setup() {
  myservo.write(pos_ini); // Initial position of the servo

  myservo.attach(9); // conecta el pin 9 de Arduino (donde se conecta el cable de señal del motor) al objeto servo:
  pinMode(pushButton, INPUT);
  digitalWrite(pushButton, HIGH);
  Serial.begin(9600);  
}