Controlling specific servo speed and range

Hey,

came across script, which works great, but need some toggling. This script let you decide where you where you want your servo to go as well as what speed it should use while in use.

void moveTo(int position, int speed){
    char mapSpeed = map(speed, 0, 30, 30, 0);
    if(position > pos){
        for (pos = pos1; pos <= position; pos += 1){
            servo1.write(pos);
            pos1 = pos;
            delay(mapSpeed);
        }
    } else {
        for(pos = pos1; pos >= position; pos -=1){
        servo1.write(pos);
        pos1 = pos;
        delay(mapSpeed);
        }
    }
}

You can call this script: moveTo(180, 10);
180 - range;
10 - speed;
So basically: moveTo(range, speed);
But it seems that if i'd like to use few servo at once, I have to create new function for it. Is it possible to optimize this function so I can call which servo I'd like to use, in example:

moveTo(180, 10, servo1);
moveTo(50, 30, servo2);

Thanks in advance!

Welcome. There are no scripts in the Arduino environment. Only "sketches".

You can call this script: moveTo(180, 10);

That is not a script, either. It's a function.
https://www.w3schools.com/cpp/cpp_functions.asp
and, yes, you can write your own function as you describe above.

You didn't post the entire sketch so I can't tell you the name of the servo object type, but your function definition would look somewhat like

void moveTo(int range, int speed, ServoObject servo) {
...
}
moveTo(123, 33, servo1);
moveTo(333, 22, servo2);

but as you can easily make servo objects, it probably makes more sense to add it to the class as a method, then you can

servo1.moveTo(123, 33);
servo2.moveTo(333, 22);

The ability to share methods is the whole point of making servo objects.

Consider, would you rather say
Serial.print("hello");
or
print("hello", Serial);

The fact that 'print' belongs to Serial, is encoded with the dot notation. That means that many classes could have a print method but they could and would usually be all different.

Without the dot notation, 'print' has only one definition and cannot function differently for different classes, e.g. 'print("hello", lcd);'. Not automatically, anyway. It would be extremely awkward as the print function would have to have a series of selection statements to choose the correct actions for the class that was passed to it.

So, although you can do what you said, it may not make your program simpler, clearer, or more efficient whichever you expect.

a good starting point would be to create an array of servos, then you can easily refer to eacjph individually with an index.

Servo myServo[10];

Your function call might then look like …

do stuff to myServo[index] as needed.

Sorry, habbit from other programming langages. My whole sketch looks like this:

#include <Servo.h>

static const int servoPin = 13;

Servo servo1;
int pos1 = 0;
int pos, posi;

void setup(){
servo1.attach(servoPin);
}

void loop(){

    moveTo(180, 10);    

}


void moveTo(int position, int speed){
    char mapSpeed = map(speed, 0, 30, 30, 0);
    if(position > pos){
        for (pos = pos1; pos <= position; pos += 1){
            servo1.write(pos);
            pos1 = pos;
            delay(mapSpeed);
        }
    } else {
        for(pos = pos1; pos >= position; pos -=1){
        servo1.write(pos);
        pos1 = pos;
        delay(mapSpeed);
        }
    }
}


The idea of choosing servo before function looks amazing & clean to read. How am I able to achieve this? First answer makes sense, how to accomplish it. The second one with:

servo1.moveTo(123, 33);
servo2.moveTo(333, 22);

seems complicated, could you cooperate how to write it ?

How good is your OOP?

I'm back end php dev, js so anything with any kind of C,C#,C++ is difficult.

Well you have some studying to do. But the specific way is to make a new class that includes your function, and inherits from the Servo class.

I sure do. Will try it. Thank you for quick response!

It's worth knowing how to do that one, even if the rest of the OO seems foggy. I say that because

  1. It's surprisingly easy
  2. It's actually easier than coding it "the old way".
2 Likes

Here's something to try.

  • Uses float for position and speed control.
  • Speed is controlled by adjusting the servo position by smaller or larger increments, determined by the pot settings.

and there is no need to use classes.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.