how to convert my code from delay to millis?

my code to set pos on servo.

#include <Servo.h>

Servo myservoL11;

void setup() {
  myservoL11.attach(9); 
}

void loop() {
  rw(20, myservoL11);
}

void rw(int speed, Servo serv){
  for (pos = 0; pos <= 180; pos += 1) { 
    serv.write(pos);             
    delay(speed); 
  }
  
  for (pos = 180; pos >= 0; pos -= 1) {
    serv.write(pos); 
    delay(speed);               
  }

I can't see the point of the two for loops.
Am I missing something?

I fixed it. now how to convert my code from delay to millis?

Fixed what?
Now it doesn't compile.

To convert "to millis", by which I assume you mean eliminate delay, you have to get rid of the for loops completely.

You have to write a simple state machine that increments or decrements your position each timeout.

Take a look at the bink without delay example sketch included in the IDE

maordany:
I fixed it. now how to convert my code from delay to millis?

You get rid of delay() by providing a function that calculates (and sets) the current servo position by the time passing by.

For example:

#include <Servo.h>

Servo myservoL11;

void setup() {
  Serial.begin(115200);
  myservoL11.attach(9); 
}

void loop() {
  setServoPos(10000, myservoL11);
}

void setServoPos(int fullCycleTime, Servo serv){
  int ServoPos;
  int halfCycleTime=fullCycleTime/2;
  int currentTime=millis()%fullCycleTime;
  if (currentTime<=halfCycleTime) ServoPos= 180L*currentTime/halfCycleTime;
  else ServoPos=180-180L*(currentTime-halfCycleTime)/halfCycleTime;
  Serial.println(ServoPos);
  serv.write(ServoPos);    
}

That "setServoPos()" function should be suitable for 'fullCycleTime' up to 32767 milliseconds.

The demo Several Things at a Time includes a function that moves a servo using millis() for timing.

...R

jurs:
You get rid of delay() by providing a function that calculates (and sets) the current servo position by the time passing by.

For example:

#include <Servo.h>

Servo myservoL11;

void setup() {
 Serial.begin(115200);
 myservoL11.attach(9);
}

void loop() {
 setServoPos(10000, myservoL11);
}

void setServoPos(int fullCycleTime, Servo serv){
 int ServoPos;
 int halfCycleTime=fullCycleTime/2;
 int currentTime=millis()%fullCycleTime;
 if (currentTime<=halfCycleTime) ServoPos= 180LcurrentTime/halfCycleTime;
 else ServoPos=180-180L
(currentTime-halfCycleTime)/halfCycleTime;
 Serial.println(ServoPos);
 serv.write(ServoPos);    
}




That "setServoPos()" function should be suitable for 'fullCycleTime' up to 32767 milliseconds.

Very nice code. Thank you

maordany:
my code to set pos on servo.

#include <Servo.h>

Servo myservoL11;

void setup() {
 myservoL11.attach(9);
}

void loop() {
 rw(20, myservoL11);
}

void rw(int speed, Servo serv){
 for (int pos = 0; pos <= 180; pos += 1) {
     serv.write(pos);
     delay(speed);
   }

for (int pos = 0; pos <= 180; pos += 1) {
     serv.write(pos);
     delay(speed);
   }

NON-blocking Servo

something like this ...

#include <Servo.h>

// Global Variables for SERVO #1


// All of the variables for SERVO #1 could be put in a STRUCT 

unsigned int gServo_1_StartTime;
unsigned int gServo_1_Delay;

boolean gServo_1_IsRunning = FALSE;

int gServo_1_Degrees = 0;

Servo gServo_1;


// All of the variables for SERVO #1 could be put in a STRUCT 


#define myButton 8;


void setup()
{
    gServo_1.attach(9); 
}

void loop()
{
    // Do something here that sets the ServoRunningFlag to TRUE
    // Like  Push Button or ?
    If ( ( HIGH == DigitalRead( MyButton ) ) && ( gServo_1_IsRunning == FALSE ) )
    {
        // Enable Servo #1
        gServo_1_IsRunning = TRUE;

        // Initialize the Starting Time
        gServo_1_StartTime = millis();

        // Set the Delay between Steps 
        gServo_1_Delay = 20;l

        // Initialize the current location to 0 Degrees
        gServo_1_Degrees = 0;

        // Call the Servo Move function only when the FLAG is set
        If ( Servo_1_IsRunning  == TRUE )
        {
            // Move Servo #1
            MoveServo( Servo_1, &gServo_1_IsRunning, &gServo_1_StartTime, gServo_1_Delay, &gServo_1_Degrees );
        }
    }
}

// Put your Servo Moving Code here
// All variables that are modified by this function are passed by ADDRESS
// This code be moved into the Main Loop if each Servo is controlled differently
void ( Servo myServo, int *MyRunningFlag, unsigned int *myStartTime, unsigned int myDelay, int *myDegrees )
{
    // Is this Servo DONE ?
    If myDegrees >= 180;
    {
       // Then TURN OFF the global Running Flag for this servo
       *myRunningFlag = FALSE;
    }
    else
   {
        // Is it time to advance this servo?
        If ( millis() - *myStartTime > myDelay )
        {
            // Capture the time of the step for this servo
            *myStartTime = millis();

            // Advance this Servo to the next angle
            myServo.pos( *myDegrees );

             // Compute the next angle for this servo
            *myDegrees++;
        }
    }
}

mrsummitville:
NON-blocking Servo

something like this ...

Cool example. I didn't know one could pass a servo label this way.

Don't you want the time parameters to be unsigned longs?

I think your code could have problems if it's left running for a long time.

I you change the line:

*myStartTime = millis();

to:

*myStartTime += myDelay;

Then the servo updates will be more precise. This could be very important if you were trying to coordinate the movements of multiple servos.

Thanks again for the example. Your code answered several questions I had about using servos with an Arduino.