how to set servo speed?

hello i need help with this code would i want to be able to somehow adjust the servo speed? but I don't know, I've been looking for it everywhere and I don't know how to thank it in advance for advice :smiley:

#include "Wire.h"
#include "I2Cdev.h"
#include "MPU6050.h"
#include "Servo.h"

MPU6050 mpu;

int16_t ax, ay, az;
int16_t gx, gy, gz;

Servo servo1;
Servo servo2;

int val1;
int val2;
int prevVal1;
int prevVal2;

void setup()
{

Wire.begin();
Serial.begin(38400);

Serial.println("Initialize MPU");
mpu.initialize();
Serial.println(mpu.testConnection() ? "Connected" : "Connection failed");
servo1.attach(9);
servo2.attach(10);
}

void loop()
{

mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

val1 = map(ax, -17000, 17000, 0, 179);
if (val1 != prevVal1)
{
servo1.write(val1);
prevVal1 = val1;
}
val2 = map(ay, -17000, 17000, 0, 179);
if (val2 != prevVal2)
{
servo2.write(val2);
prevVal2 = val2;
}

delay(200);
}

1 Like

The speed of a servo is determined by its internal electronics. You can give the impression of controlling the speed by moving the servo in small increments. Have a look at the servo code in Several Things at a Time and at the servo sweep example that comes with the Arduino IDE.

...R

@stromecek_555

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

thank you for the advice :slight_smile:

there is a library called controlledServo which does comfortise (is this a known english word?) slowing down a servo's speed. You can install it in the Arduino-IDE with the library manager

best regards Stefan

I guess looking everywhere didn't include Googling "arduino servo speed" or similar because there is a library called VarSpeedServo which pops up towards the top of the list. I think is rather better documented and easier to use than "controlledServo". It uses a standard write() command but with an extra speed parameter. Give it a try.

Steve

thanks for the library names, I tried the VarSpeedServo library, but it doesn't work, at least I'm not that good at programming, I'm a beginner and I don't know where to put it. so far I'm learning arduino, so that's why :slight_smile:

stromecek_555:
thanks for the library names, I tried the VarSpeedServo library, but it doesn't work, at least I'm not that good at programming, I'm a beginner and I don't know where to put it. so far I'm learning arduino, so that's why :slight_smile:

So show us the program you tried and tell us exactly what "doesn't work" means to you. I've used VarSpeedServo many times and it's always worked for me.

Steve

rethoric question what makes a library newbee-friendly?

giving a step by step installation-manual with screenshots

go to github

Click code and then download zip
store zipfile on your hardisk
in the arduino-IDE click sketch - include library - add zip-library

Download-VarSpeedServo-02.png

if you scroll down until letter "V" under file - examples you will find example-codes
best regards Stefan

Download-VarSpeedServo-02.png

Děkuji všem za odpovědi, jak jste se zeptali (slipstick), kód funguje pro mě, ale nevím, jak nastavit rychlost serva (chtěl jsem si na zádech rozsvítit světlo, aby se pohybovalo způsobem, jakým se pohybují s mojí hlavou). Tady je kód, je trochu upravený, ale nefunguje mi a nevím proč.

#include <VarSpeedServo.h>
#include „Wire.h“
#include „I2Cdev.h“
#include "MPU6050.h"
#include "Servo.h"

MPU6050 mpu;

int16_t ax, ay, az;
int16_t gx, gy, gz;

Servo servo1;
Servo servo2;
VarSpeedServo servo1;
VarSpeedServo servo2;

int val1;
int val2;
int prevVal1;
int prevVal2;

neplatné nastavení ()
{
Wire.begin ();
Serial.begin (38400);

Serial.println ("Inicializovat MPU");
mpu.initialize ();
Serial.println (mpu.testConnection ()? "Připojeno": "Připojení se nezdařilo");
servo1.attach (9);
servo2.attach (10);
}

neplatná smyčka ()
{
servo1.write (180, 30, true);
servo2.write (180, 30, true);

mpu.getMotion6 (& ax, & ay, & az, & gx, & gy, & gz);

val1 = mapa (ax, -17000, 17000, 0, 179);
if (val1! = prevVal1)
{
servo1.write (val1);
prevVal1 = val1;
}
val2 = mapa (ay, -17000, 17000, 0, 179);
if (val2! = prevVal2)
{
servo2.write (val2);
prevVal2 = val2;
}

zpoždění (50);
}

V souboru zahrnutém z C: \ Users \ PC-RTX2070 \ Documents \ Arduino \ libraries \ Servo \ src / Servo.h: 63: 0,
z C: \ Users \ PC-RTX2070 \ Downloads \ FVPKTDUIOK9O7SR \ FVPKTDUIOK9O7SR.ino: 5:
C: \ Users \ PC-RTX2070 \ Documents \ Arduino \ libraries \ Servo \ src / avr / ServoTimers.h: 57:16: error: redeclaration of '_timer1'
typedef enum {_timer1, _Nbr_16timers} timer16_Sequence_t;
^ ~~~~~~
V souboru zahrnutém z C: \ Users \ PC-RTX2070 \ Downloads \ FVPKTDUIOK9O7SR \ FVPKTDUIOK9O7SR.ino: 1: 0:
C: \ Users \ PC-RTX2070 \ Documents \ Arduino \ libraries \ VarSpeedServo-master / VarSpeedServo.h: 112: 16: poznámka: předchozí deklarace 'timer16_Sequence_t _timer1'
typedef enum {_timer1, _Nbr_16timers} timer16_Sequence_t;
^ ~~~~~~
V souboru zahrnutém z C: \ Users \ PC-RTX2070 \ Documents \ Arduino \ libraries \ Servo \ src / Servo.h: 63: 0,
z C: \ Users \ PC-RTX2070 \ Downloads \ FVPKTDUIOK9O7SR \ FVPKTDUIOK9O7SR.ino: 5:
C: \ Users \ PC-RTX2070 \ Documents \ Arduino \ libraries \ Servo \ src / avr / ServoTimers.h: 57:25: error: redeclaration of '_Nbr_16timers'
typedef enum {_timer1, _Nbr_16timers} timer16_Sequence_t;
^ ~~~~~~~~~~~~~
V souboru zahrnutém z C: \ Users \ PC-RTX2070 \ Downloads \ FVPKTDUIOK9O7SR \ FVPKTDUIOK9O7SR.ino: 1: 0:
C: \ Users \ PC-RTX2070 \ Documents \ Arduino \ libraries \ VarSpeedServo-master / VarSpeedServo.h: 112:25: poznámka: předchozí deklarace 'timer16_Sequence_t _Nbr_16timers'
typedef enum {_timer1, _Nbr_16timers} timer16_Sequence_t;
^ ~~~~~~~~~~~~~
V souboru zahrnutém z C: \ Users \ PC-RTX2070 \ Documents \ Arduino \ libraries \ Servo \ src / Servo.h: 63: 0,
z C: \ Users \ PC-RTX2070 \ Downloads \ FVPKTDUIOK9O7SR \ FVPKTDUIOK9O7SR.ino: 5:
C: \ Users \ PC-RTX2070 \ Documents \ Arduino \ libraries \ Servo \ src / avr / ServoTimers.h: 57: 41: chyba: konfliktní deklarace 'typedef enum timer16_Sequence_t timer16_Sequence_t'
typedef enum {_timer1, _Nbr_16timers} timer16_Sequence_t;
^ ~~~~~~~~~~~~~~~~~~
V souboru zahrnutém z C: \ Users \ PC-RTX2070 \ Downloads \ FVPKTDUIOK9O7SR \ FVPKTDUIOK9O7SR.ino: 1: 0:
C: \ Users \ PC-RTX2070 \ Documents \ Arduino \ libraries \ VarSpeedServo-master / VarSpeedServo.h: 112: 41: poznámka: předchozí deklarace jako 'typedef enum timer16_Sequence_t timer16_Sequence_t'
typedef enum {_timer1, _Nbr_16timers} timer16_Sequence_t;
^ ~~~~~~~~~~~~~~~~~~
V souboru zahrnutém z C: \ Users \ PC-RTX2070 \ Downloads \ FVPKTDUIOK9O7SR \ FVPKTDUIOK9O7SR.ino: 5: 0:
C: \ Users \ PC-RTX2070 \ Documents \ Arduino \ libraries \ Servo \ src / Servo.h: 97: 3: chyba: konfliktní deklarace 'typedef struct ServoPin_t ServoPin_t'
} ServoPin_t;
^ ~~~~~~~~~
V souboru zahrnutém z C: \ Users \ PC-RTX2070 \ Downloads \ FVPKTDUIOK9O7SR \ FVPKTDUIOK9O7SR.ino: 1: 0:
C: \ Users \ PC-RTX2070 \ Documents \ Arduino \ libraries \ VarSpeedServo-master / VarSpeedServo.h: 133: 3: poznámka: předchozí deklarace jako 'typedef struct ServoPin_t ServoPin_t'
} ServoPin_t;
^ ~~~~~~~~~
V souboru zahrnutém z C: \ Users \ PC-RTX2070 \ Downloads \ FVPKTDUIOK9O7SR \ FVPKTDUIOK9O7SR.ino: 5: 0:
C: \ Users \ PC-RTX2070 \ Documents \ Arduino \ libraries \ Servo \ src / Servo.h: 102: 3: chyba: konfliktní deklarace 'typedef struct servo_t servo_t'
} servo_t;
^ ~~~~~~
V souboru zahrnutém z C: \ Users \ PC-RTX2070 \ Downloads \ FVPKTDUIOK9O7SR \ FVPKTDUIOK9O7SR.ino: 1: 0:
C: \ Users \ PC-RTX2070 \ Documents \ Arduino \ libraries \ VarSpeedServo-master / VarSpeedServo.h: 141: 3: poznámka: předchozí deklarace jako 'typedef struct servo_t servo_t'
} servo_t;
^ ~~~~~~
FVPKTDUIOK9O7SR: 14:15: chyba: konfliktní deklarace 'VarSpeedServo servo1'
VarSpeedServo servo1;
^ ~~~~~
C: \ Users \ PC-RTX2070 \ Downloads \ FVPKTDUIOK9O7SR \ FVPKTDUIOK9O7SR.ino: 12: 7: poznámka: předchozí deklarace jako 'Servo servo1'
Servo servo1;
^ ~~~~~
FVPKTDUIOK9O7SR: 15:15: chyba: konfliktní deklarace 'VarSpeedServo servo2'
VarSpeedServo servo2;
^ ~~~~~
C: \ Users \ PC-RTX2070 \ Downloads \ FVPKTDUIOK9O7SR \ FVPKTDUIOK9O7SR.ino: 13: 7: poznámka: předchozí deklarace jako 'Servo servo2'
Servo servo2;
^ ~~~~~
C: \ Users \ PC-RTX2070 \ Downloads \ FVPKTDUIOK9O7SR \ FVPKTDUIOK9O7SR.ino: Ve funkci 'void loop ()':
FVPKTDUIOK9O7SR: 37: 29: error: no matching function for call to 'Servo :: write (int, int, bool)'
servo1.write (180, 30, true);
^
V souboru zahrnutém z C: \ Users \ PC-RTX2070 \ Downloads \ FVPKTDUIOK9O7SR \ FVPKTDUIOK9O7SR.ino: 5: 0:
C: \ Users \ PC-RTX2070 \ Documents \ Arduino \ libraries \ Servo \ src / Servo.h: 111: 8: note: candidate: void Servo :: write (int)
void write (hodnota int); // pokud je hodnota <200, považuje se to za úhel, jinak za šířku pulzu v mikrosekundách
^ ~~~~
C: \ Users \ PC-RTX2070 \ Documents \ Arduino \ libraries \ Servo \ src / Servo.h: 111: 8: poznámka: kandidát očekává 1 argument, 3 za předpokladu
FVPKTDUIOK9O7SR: 38: 29: error: no matching function for call to 'Servo :: write (int, int, bool)'
servo2.write (180, 30, true);
^
V souboru zahrnutém z C: \ Users \ PC-RTX2070 \ Downloads \ FVPKTDUIOK9O7SR \ FVPKTDUIOK9O7SR.ino: 5: 0:
C: \ Users \ PC-RTX2070 \ Documents \ Arduino \ libraries \ Servo \ src / Servo.h: 111: 8: note: candidate: void Servo :: write (int)
void write (hodnota int); // pokud je hodnota <200, považuje se to za úhel, jinak za šířku pulzu v mikrosekundách
^ ~~~~
C: \ Users \ PC-RTX2070 \ Documents \ Arduino \ libraries \ Servo \ src / Servo.h: 111: 8: poznámka: kandidát očekává 1 argument, 3 za předpokladu
Byly nalezené násobné knihovny "Wire.h"
Použitý: C: \ Users \ PC-RTX2070 \ Documents \ ArduinoData \ packages \ arduino \ hardware \ avr \ 1.8.3 \ libraries \ Wire
Nepoužitý: C: \ Users \ PC-RTX2070 \ Documents \ Arduino \ libraries \ Wire-master
Byly nalezené násobné knihovny "Servo.h"
Použitý: C: \ Users \ PC-RTX2070 \ Documents \ Arduino \ libraries \ Servo
Nepoužitý: C: \ Program Files \ WindowsApps \ ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt \ libraries \ Servo
stav ukončení 1
konfliktní prohlášení „VarSpeedServo servo1“

The idea was that you REPLACE Servo.h with VarSpeedServo.h. You can't have servo1 and servo2 defined as being BOTH Servo AND VarSpeedServo. Try this which compiles but is not tested:

#include <VarSpeedServo.h>
#include "Wire.h"
#include "I2Cdev.h"
#include "MPU6050.h"

MPU6050 mpu;

int16_t ax, ay, az;
int16_t gx, gy, gz;

VarSpeedServo servo1;
VarSpeedServo servo2;

int val1;
int val2;
int prevVal1;
int prevVal2;

void setup()
{
  Wire.begin();
  Serial.begin(38400);
  Serial.println("Initialize MPU");
  mpu.initialize();
  Serial.println(mpu.testConnection() ? "Connected" : "Connection failed");
  servo1.attach(9);
  servo2.attach(10);
}
void loop()
{
  servo1.write(180, 30, true);
  servo2.write(180, 30, true);

  mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

  val1 = map(ax, -17000, 17000, 0, 179);
  if (val1 != prevVal1)
  {
    servo1.write(val1);
    prevVal1 = val1;
  }
  val2 = map(ay, -17000, 17000, 0, 179);
  if (val2 != prevVal2)
  {
    servo2.write(val2);
    prevVal2 = val2;
  }
  delay(50);
}

And note that it is in code tags and I've removed all the extra blank lines that make it so hard to read. Please learn how to do this, you get much better answers if people can read your code easily.

Steve

oh my god,
this is about the longest emptyline sketch i have seen.
do this forum a favor
press ctrl-T inside the ide for autoformat code
delete the empty lines
do a right click and choose copy for forum
then edit your post
delete the normal text
then paste the clipboard
using the copy for forum fearure prepares the content to become a code-section
best regards Stefan

thanks for the modified code slipstick that works well but doesn't work because the servo rotates back and forth, but not according to the gyroscope, I don't know how to set it up.

You need to put some Serial.prints in to show you what values you are getting from the "gyroscope".

But do you really mean to send both servos to 180 every time round loop()? If so why?

Steve

48 hour timeout for stromecek_555

Failure to use the links provided which help with posting code.
Now the user has the time to use them.