how to use millis instead of delay for servo control?

#include <Servo.h>
Servo myservo;
int pos = 0;

void setup() {
myservo.attach(12);
}

void loop() {
for (pos = 47; pos < 140; pos+=1)
{
myservo.write(pos);
delay(30);
}
delay(150);
for (pos = 140; pos >=47; pos-=1)
{
myservo.write(pos);
delay(30);
}
delay(150);
}

I’m controlling my servo with this code, but I need to change delay to millis so I can also control the readings of ultrasonic range finder and capacitive touch sensors. How do I create same servo movement with millis??

To not use the delay() function: Have you read any threads that say, use the technique of "blink without delay" found in the examples that come with the IDE ?

The code in several things at a time moves a servo using millis()

...R

I usually do it with a state machine. A state machine library would probably make it more readable.

#include <Servo.h>
Servo myservo;
int pos = 0;
int state;
unsigned long lastTime;

void setup() {
  myservo.attach(12);
  pos = 47;
}

void loop() {
  switch (state) {
    case 0:
      //for (pos = 47; pos < 140; pos += 1)
      if (pos >= 140) {
        state = 1;
        lastTime = millis();
        break;
      }
      //  delay(30);
      if (millis() - lastTime >= 30) {
        myservo.write(pos++);  // pos += 1)
        lastTime += 30;
      }
      break;

    case 1:
      // delay(150);
      if (millis() - lastTime >= 150) {
        state = 2;
        lastTime = millis();
        pos = 140;
      }
      break;

    case 2:
      // for (pos = 140; pos >= 47; pos -= 1)
      if (pos < 47) {
        state = 3;
        lastTime = millis();
        break;
      }
      //  delay(30);
      if (millis() - lastTime >= 30) {
        myservo.write(pos--);
        lastTime += 30;
      }
      break;

    case 3:
      // delay(150);
      if (millis() - lastTime >= 150) {
        state = 0;
        lastTime = millis();
        pos = 47;
      }
      break;
  }
}

Having a flag to allow initialization code for each state makes the state machine a little more readable:

#include <Servo.h>
Servo myservo;
int pos = 0;
int state;
unsigned long lastTime;

void setup() {
  myservo.attach(12);
  newState(0);
}

void newState(int new_state) {
  state = new_state;
  firstTime = true;
}

void loop() {
  switch (state) {
    case 0:
      //for (pos = 47; pos < 140; pos += 1)
      if (firstTime) {
        pos = 47;
        lastTime = millis();
        firstTime = false;
      }
      if (pos >= 140) {
        newState(1);
        lastTime = millis();
        break;
      }
      //  delay(30);
      if (millis() - lastTime >= 30) {
        myservo.write(pos++);  // pos += 1)
        lastTime += 30;
      }
      break;

    case 1:
      // delay(150);
      if (firstTime) {
        lastTime = millis();
        firstTime = false;
      }
      if (millis() - lastTime >= 150) {
        newState(2);
      }
      break;

    case 2:
      // for (pos = 140; pos >= 47; pos -= 1)
      if (firstTime) {
        pos = 140;
        lastTime = millis();
        firstTime = false;
      }
      if (pos < 47) {
        newState(3);
        break;
      }
      //  delay(30);
      if (millis() - lastTime >= 30) {
        myservo.write(pos--);
        lastTime += 30;
      }
      break;

    case 3:
      if (firstTime) {
        lastTime = millis();
        firstTime = false;
      }
      // delay(150);
      if (millis() - lastTime >= 150) {
        newState(0);
      }
      break;
  }
}