how do i replace delay with millis in this code

hi. can anyone show me how to replace delay used in the code for random movements of servo with millis.

unsigned int duration = 0;
int lasttime1 = 0;
int lasttime2 = 0;
int randstart = 0;
int randend = 0;
int pos = 0; 

void setup()
{
 for(pos = randstart; pos < randend; pos += 1)
 {
 myservo.write (pos);
 delay(5000);
 }
 for(pos = randend; pos>=(randstart+1); pos-=1)
 {
 myservo.write(pos);
 delay(5000);
 }
 randstart = random(200, 800);
 randend = random(800, 2400);
}

We see you have 50 posts here.

What attempt have you made to use the millis() BWD (blink without delay) technique?

You have looked a the example that comes with the IDE?

.

yes . i saw the code but couldnt understand

Is this the code that you say you don't understand?

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the 
  // difference between the current time and last time you blinked 
  // the LED is bigger than the interval at which you want to 
  // blink the LED.
  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

Anvay: yes . i saw the code but couldnt understand

Save the millis() value at the time that you move the servo to the next position and increment the position variable. Then, each time through loop(), check whether the required wait period has elapsed by subtracting the start time from the millis() value now. If the period has elapsed then move the servo to the next position, update the position variable and save the start time again.

If the period has not elapsed then go round loop() again, perhaps taking other actions and/or reading inputs, but don't block the free running of loop().

Keep going until you reach the target servo position.

Anvay: yes . i saw the [bwod] code but couldnt understand

What it does is this:

Initialise an interval; this is what in the old way was the delay value

Initialise a variable called previousMillis to 0. This will in future be the record of the last time we did the thing we're controlling. It hasn't been done at the start, so hence it's 0.

Each time through loop(), make a timestamp of the now time, currentMillis.

First time through loop(), check to see if it's time to do the thing. Do that by subtracting the time of the last time we did the thing (previuosMillis, which first time through is 0), from the time now (currentMillsi) and see if that difference is bigger than or equal to the interval.

  • If it IS greater than or equal, then it's time we did the thing. So do the thing, and record the time we did that thing, ie copy currentMillis into previuosMillis for next time.
  • If it's NOT greater than or equal, do nothing.

Each time through loop(), do exactly the same, except after we've done the thing once, previousMillis will not be 0, it will be the actual time at which we did the thing.

ok. thanks for the detailed explanation. i will try

i tried using millis. however the servo is not moving randomly after a gap of 2 seconds

int iOldPos, iNewPos = 0;
long previousMillis = 0;
const int interval = 20;

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

void loop(){
iNewPos = random(200, 800);
iOldPos = random(800, 2400);
unsigned long currentMillis = millis();

if(iOldPos != iNewPos && currentMillis - previousMillis > interval) 
{
   previousMillis = currentMillis;
    iOldPos = iNewPos;
     myservo.write(iNewPos); 
}
}

You're using servo.write() which expects degrees, but feeding it what look more like pulse lengths.

So you want servo.writeMicroseconds()

i am suin servotimer2 library. when i compile the code it says 'class ServoTimer2' has no member named 'writeMicroseconds'

Anvay: 'class ServoTimer2' has no member named 'writeMicroseconds'

If that's the case, or there's no equivalent, you'll have to produce random degrees then I suppose.

okay. so what can i do now ?

Anvay: okay. so what can i do now ?

Like I said, produce random degrees.

But I'm surprised that library doesn't have .writeMicroseconds() or equivalent.

The ServoTimer2 library write() function takes microseconds in the range 0 to 2000 as its position paramater unlike the Servo library write function that takes degrees from 0 to 180

Hence the ServoTimer2 write() function is equivalent to the Servo library writeMicroseconds() function

UKHeliBob: The ServoTimer2 library write() function takes microseconds in the range 0 to 2000 as its position paramater unlike the Servo library write function that takes degrees from 0 to 180

Hence the ServoTimer2 write() function is equivalent to the Servo library writeMicroseconds() function

Oh ok, then disregard what I said;)

thanks .everything fine now

Anvay: i am suin servotimer2 library.

Which would have been evident had you posted your entire sketch.

You’re producing new values for ‘iNewPos’, and ‘iOldPos’ on EVERY iteration of ‘loop()’. Is that what you really want?

Anvay: thanks .everything fine now

Congratulations, you are an expert now :)

.