Move servo slowly without delay() at scheduled time

So, I have figured out the scheduled time piece. For the application I need to move the servo slowly 0 -> 180 -> 0. 0 to 180 should be done in 5 seconds and then 180 to 0 should be done in 5 seconds.

I see examples using timer() using the following which works but I want a delay not in between but when it moves from 0 to 180 to control the speed.

servo.write(180); //this moves really fast and I want it slower
//wait using timer
servo.write(0); //this moves really fast and I want it slower

Then there are examples of using delay but I see that delay should not be used with blynk

for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }

Any other ideas to control the speed when the servo moves from 0 to 180?

@DIYTerminator a single delay of 15ms wouldn’t be a problem for Blynk but because it’s contained within a 180 iteration loop the delay will be 2700ms.

All you have to do is set up a 15ms timer to step to the next pos, like the for loops you have but without using a for loop.

@DIYTerminator Try this variable servo speed library:

UPDATE - I tested it, and with only minor modifications to my existing servo sweep test (include library, myservo declaration and adjustments to existing timer and servo commands, to set and account for the reduced movement speed), it works great!!

Smooth movement and no apparent issues with Blynk that I have noticed… and my testbench is full of timers :stuck_out_tongue:

This library is a keeper for me :smiley:

That is very interesting. Thats for your responses. Testing that library now!

The VarSpeedServo library didn’t work for me as I am using NodeMCU board. I ended up using the following… hope this helps someone.

 void loop()
 {
   // do not add any commands here. Add everyting to my_loop since that will get called with with delay and not hualt the processing
   my_loop();
 }

 // whatever code should go in the main loop() should go there. This is called by Blynk_Delay so the delay call does not hault thread processing
 void my_loop()
 {
   ArduinoOTA.handle();
   Blynk.run();
   timer.run();
 }

 // this delay will not hold up the thread
 void Blynk_Delay(int milli) {
    int end_time = millis() + milli;
    while(millis() < end_time){
        my_loop(); //keep processing the loop functions
        yield();
    }
 }

 void moveServo()
 {
   Serial.println("trying to move servo");
   for (pos = 20; pos <= 160; pos += 1) { // goes from 0 degrees to 180 degrees
     // in steps of 1 degree
     myservo.write(pos);              // tell servo to go to position in variable 'pos'
     Blynk_Delay(15);                       // waits 15ms for the servo to reach the position
   }
   for (pos = 160; pos >= 20; pos -= 1) { // goes from 180 degrees to 0 degrees
     myservo.write(pos);              // tell servo to go to position in variable 'pos'
     Blynk_Delay(15);                       // waits 15ms for the servo to reach the position
   }
 }

Ya, sorry… I had tested it on my MEGA with ESP shield, and knew it didn’t conflict with Blynk or any of the ESP libraries I was using… but had not gone further into it yet with my Wemos boards. AVR uses interrupts a bit differently than the ESP I guess. Too bad, it is very simple to use.

I have been testing different methods of quick delays using BlynkTimer, but I will look closer at your solution.