stop or pause servo

this script is working fine, but I didn’t manage to stop or pause the servo when it reaches the postition ‘N’. At the endposition it just makes some noise.

I tried:
while(1) {
}

but it’s not working…
anyone knows how to turn off the servo?

thanks!

#include <Servo.h> 

Servo myservo;  // create servo object to control a servo 

#define SMOOTHSTEP(x) ((x) * (x) * (3 - 2 * (x))) //SMOOTHSTEP expression.

const int SPEED = 40000;  //1/25 Seconds

float S = 180;       //NUMBER OF Servo Steps
float N = 500;        //Seconds to reach endposition x 25
int j = 0;             //iterator
int i = 0;              //iterator
float A = 0.0;         //Input Min Value
float B = 100.0;       //Input Max Value
float X;               //final smoothstepped value
float v;               //smoothstep expression variable
float move;

void setup() 
{ 
  myservo.attach(2);  // attaches the servo on pin 2 to the servo object 
  Serial.begin(57600);
  pinMode(2, OUTPUT);
} 

void loop() 
{ 
  if (j<=N)                    // Keep looping until we hit the pre-defined max number of steps

  {
    v = j / N;                  // Iteration divided by the number of steps.
    v = SMOOTHSTEP(v);            // Run the smoothstep expression on v.
    X = (B * v) + (A * (1 - v));   // Run the linear interpolation expression using the current smoothstep result
    for (int i=0; i < X ; i++)
      move = (v*S);
    Serial.println(move);
    myservo.write(move); 
    delayMicroseconds(SPEED);    // servo should move to the 

  }                              

  j++;

}

The processor will carry on sending PPM pulses whatever else the software is doing. Try a detach, or a position short of the end-stop.

I included a detach() . but now the servo does not move from 1 to 180°. What do you mean with position short?

#include <Servo.h> 

Servo myservo;  // create servo object to control a servo 

#define SMOOTHSTEP(x) ((x) * (x) * (3 - 2 * (x))) //SMOOTHSTEP expression.

const int SPEED = 40000;  //1/25 Seconds

float S = 180;       //NUMBER OF Servo Steps
float N = 501;        //Seconds to reach endposition x 25
int j = 0;             //iterator
int i = 0;              //iterator
float A = 0.0;         //Input Min Value
float B = 100.0;       //Input Max Value
float X;               //final smoothstepped value
float v;               //smoothstep expression variable
float move;

void setup() 
{ 
  myservo.attach(2);  // attaches the servo on pin 2 to the servo object 
  Serial.begin(57600);
  pinMode(2, OUTPUT);
} 

void loop() 
{ 
  if (j<N)                    // Keep looping until we hit the pre-defined max number of steps

  {
    v = j / N;                  // Iteration divided by the number of steps.
    v = SMOOTHSTEP(v);            // Run the smoothstep expression on v.
    X = (B * v) + (A * (1 - v));   // Run the linear interpolation expression using the current smoothstep result
    for (int i=0; i < X ; i++)
      move = (v*S);
    Serial.println(move);
    myservo.write(move); 
    delayMicroseconds(SPEED);    // servo should move to the 

  }  
  if (j=N)
  {
    myservo.detach();
  }  

  j++;




}

You want (j == N), not (j = N)

perfect. thank you!

still a little issue: when the servo is detached it steps back a few degrees. How could it be avoided?

when the servo is detached it steps back a few degrees. How could it be avoided?

This indicates to me that your servo can't really go to 180 degrees. Try going only to 175 degrees, without detaching.

Try going only to 175 degrees, without detaching

aka "a position short of the end-stop"

It really can only move 167 degrees!

now with this sketch I have the problem that when I start at 12°, it moves quickly forward and again backwards the starting point before it moves the way I want to from 12 to 152!

#include <Servo.h> 

Servo myservo;  // create servo object to control a servo 

#define SMOOTHSTEP(x) ((x) * (x) * (3 - 2 * (x))) //SMOOTHSTEP expression.

const int SPEED = 40000;  //1/25 Seconds

const int StartServo = 12;

float S = 140 ;       //NUMBER OF Servo Steps 
float N = 501;        //Seconds to reach endposition x 25
int j = 1;             //iterator
int i = 1;              //iterator
float A = 0.0;         //Input Min Value
float B = 100.0;       //Input Max Value
float X;               //final smoothstepped value
float v;               //smoothstep expression variable
float move;

void setup() 
{ 
  myservo.attach(2);// attaches the servo on pin 2 to the servo object 
  Serial.begin(57600);
  pinMode(2, OUTPUT);
} 

void loop() 
{ 


  {
    if (j<N)                    // Keep looping until we hit the pre-defined max number of steps

    {
      v = j / N;                  // Iteration divided by the number of steps.
      v = SMOOTHSTEP(v);            // Run the smoothstep expression on v.
      X = (B * v) + (A * (1 - v));   // Run the linear interpolation expression using the current smoothstep result
      for (int i=0; i < X ; i++)
        move = ((v*S)+StartServo);
      Serial.println(move);
      myservo.write(move); 
      delayMicroseconds(SPEED);    // servo should move to the 
    }
    if (j==N)
    {
      myservo.detach();
    } 
    j++;
  }
}

where is the bug???

      for (int i=0; i < X ; i++)
        move = ((v*S)+StartServo);

Why does move need to be calculated more than once?

const int SPEED = 40000;  //1/25 Seconds

Have you looked at the range of values that can be stored in an int?