Millis - please help

Hello I am working on a project that includes moving servos. I would like to create a functions to move them (but that's kinda easy). What I am asking for is your help with using millis() to move Servo only once everytime when calling that function (for example goForward() which does moving forward using millis to be able to run some things in the background without delaying). I really need your help :-)

servo.write(angle) is non-blocking function. You can use it and then do other tasks

I would like to move wheels of my project that are using servos and the reason of millis() is that I want to move that object forward for a certain period of time. Is that possible with using <Servo.h> ?

Xarakas:
I would like to move wheels of my project that are using servos and the reason of millis() is that I want to move that object forward for a certain period of time. Is that possible with using <Servo.h> ?

Yes

Have you read Using millis() for timing. A beginners guide ?

What is the model name of the servo motor you are using?

UKHeliBob:
Yes

Have you read Using millis() for timing. A beginners guide ?

Yes I had. The problem with that is that it keeps doing repetitively and I just want to do it once I call that function, then move on another function e.x...

IoT_hobbyist:
What is the model name of the servo motor you are using?

For example another servo moving another part is Micro Servo SG90 (just 180 dregrees would do the thing, but I dont know how to move another part of project and keep moving this part at the same time)

UKHeliBob may have forgotten this, but he once posted the following pseudo-code which I copied into my Arduino "miscellany":

(edit: probably irrelevant since these must be continuous servos, but leaving it here anyway)

UKHeliBob:
....don't move the servos from their current position to their target position all in one command. Move them a little every now and again using millis() for timing. Use a boolean variable to stop them moving when the target position is reached

For one servo, something like this

start of loop()
  get current time from millis()
  if current time - previous move time >= wait period and the moving variable is true
     move a little
     save the time of the move
     change the target position a little
      if at the target position
        set the boolean to false
      end if
    end if
end of loop()

UKHeliBob:
You can extend this to multiple servos by using arrays for the values and an array of servos

Xarakas:
I would like to move wheels of my project that are using servos and the reason of millis() is that I want to move that object forward for a certain period of time. Is that possible with using <Servo.h> ?

I’m guessing that you are using “continuous rotation servos” as the drive motors for your wheels.
Yes, you can start moving forward, and then, at a specified time later, do something else.

enum States {Stop, Stopped, MoveForward, MovingForward} State = Stopped;
void loop() 
{
  // Process input here
  switch (State)
  {
  case Stop:
    LeftMotor.write(LeftStop);
    RightMotor.write(RightStop);
    State = Stopped;
    break;
  case Stopped:
    if (command == "f") // Move forward
    {
      State = MoveForward;
    }
    break;
  case MoveForward:
      stateStartTime = millis();
      LeftMotor.write(LeftMotorForeward);
      RightMotor.write(RightMotorForeward);
      State = MovingForward;
      break;
  case MovingForward:
    if (millis() - stateStartTime >= MovingForwardInterval)
    {
      State = Stop;
    }
    break;
  }
}

Xarakas:
I would like to move wheels of my project that are using servos and the reason of millis() is that I want to move that object forward for a certain period of time. Is that possible with using <Servo.h> ?

For example another servo moving another part is Micro Servo SG90 (just 180 dregrees would do the thing, but I dont know how to move another part of project and keep moving this part at the same time)

Of course yes, you can use <Servo.h> library without using delay() and millis() function unless you want to control the speed of motor. See servo tutorial

Thanks guys, I will definitely try this as soon as I come home again. Once again, thank you so much :slight_smile:
God bless

IoT_hobbyist:
See servo tutorial

About that:

  • Don’t power the servo (even a tiny one) from the Arduino 5V
  • “Some of Arduino pins can be programmed to generate PWM signal.” While that’s true for PWM via analogWrite(), it’s irrelevant for a servo, and (at least on the Uno pictured) any pin (even, in fact, the analog inputs Ax which are actually digital anyway) can control a servo

> sayHovis:
> About that:
>
>
> - Don't power the servo (even a tiny one) from the Arduino 5V
> - "Some of Arduino pins can be programmed to generate PWM signal." While that's true for PWM via analogWrite(), it's irrelevant for a servo, and (at least on the Uno pictured) any pin (even, in fact, the analog inputs Ax which are actually digital anyway) can control a servo
1. See circuit in https://www.arduino.cc/en/Tutorial/Knob. It also powers the motor via Arduino's 5V. It is not a big problem with low-torque motor for learning purpose
2. See the Additional Knowledge part

IoT_hobbyist:
1. See circuit in https://www.arduino.cc/en/Tutorial/Knob. It also powers the motor via 5V. It is not big problem with low-torque motor for learning purpose
2. See the Additional Knowledge part

  • Even an sg90 style servo draws about 750mA at stall so it’s never acceptable. Just because the official tutorial here shows it that way, is no reason to perpetuate a bad idea. And I know you said it in the additional info at the bottom: it should be at the top.
  • That the library supports 12 servos does of course imply that a pin needn’t be a ~PWM (ie analogWrite()) pin since there are only 6, but you also have that only as a footnote. The body of your tutorial gives the distinct impression that a PWM pin is required. It’s a pity that tutorials tend for some reason to use pin ~9 which helps perpetuate that thinking.

That silly example is a problem. Well over ninety percent of problems relating to servos posted here are power related and often because the person asking was mislead by that example or others like it.