Servo trouble (mooving at each loop)

Good evening to all

I am using a small servo (with a independant power source) however at each main loop it is slightly mooving without to have received any new angle order. The servo is useinside a code for a radio command .

I have test the servo , with a simple code , it is ok.

Any idea where it can comes from ?

thanks in advance

My code is :

#include <Servo.h>
Servo myservo;

#include <SoftwareSerial.h> // bibliothèque pour la voie série
SoftwareSerial mySerial(7, 6); // RX, TX // les broches de la voie série
#include <Wire.h> // bibliothèque
#include <Adafruit_Sensor.h> //bibliothèque pour le compas magnétique
#include <Adafruit_HMC5883_U.h>
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);

// Déclaration des variables
int message [19] = {}; //tableau à transmettre
double x = 0; //x brut du capteur
double y = 0; //y brut du capteur
double z = 0; //z brut du capteur
double checksomme =0; //Variable de vérification de la trame
String xstring = " ";
String ystring = " ";
String zstring = " “;
String check =” ";
String received;
int cardispo = 0;

String bit1;
String bit2;
String bit3;
String bit4;
String bit5;
String bit6;
String bit7;

int bit1_mod;
int bit2_mod;
int bit3_mod;
int bit4_mod;
int barre;
int barre_enr;


void setup() {

if (!mag.begin()) {
Serial.println(“Ooops, no HMC5883 detected … Check your wiring!”);
while (1);


void loop() {

sensors_event_t event;

x = event.magnetic.x;
y = event.magnetic.y;
z = event.magnetic.z;

checksomme = round(x)+round(y)+round(z) ;
//Serial.print (x);Serial.print (" “);Serial.print (y);Serial.print (” “);Serial.print (z);Serial.print (” ");Serial.println (checksomme);

xstring = String(x);
ystring = String(y);
zstring = String(z);

mySerial.print(xstring) ; mySerial.write(58) ;
mySerial.print(ystring) ; mySerial.write(59) ;
mySerial.print(zstring) ; mySerial.write(60) ;
mySerial.print(checksomme) ; mySerial.write(61) ;

if (mySerial.available() > 0) {bit1 =;} else {goto loopIt; };


bit1_mod = bit1.toInt();
bit1_mod =bit1_mod -48;

bit2_mod = bit2.toInt();
bit2_mod =bit2_mod -48;

bit3_mod = bit3.toInt();
bit3_mod =bit3_mod -48;

bit4_mod = bit4.toInt();
bit4_mod =bit4_mod -48;

//if (barre_enr!=barre){myservo.write(barre);barre_enr=barre; }


Serial.print(bit1);Serial.print(" “);Serial.print(bit2);Serial.print(” “);Serial.print(bit3);Serial.print(” “);;Serial.print(bit4);Serial.println(” ");

//while(Serial.available()>0 ){received =;}

///Serial.println (received);

delay (20);



How do you know there’s something to read here?

Please remember to use code tags when posting code

And please, goto is so 1950s

I imagine it's because you keep attaching and detaching the servo. When it is attached it tries to go to a default position and then moves to where your write sends it.

So why do you think you need to do that attach/detach over and over again? Normally you do the attach() once in setup() and there's rarely any need ever to use detach().


Good evening

in fact the part of code :


is only the way I found to avoid this trouble. (but I am loosing 50 ms)

why it is not working if I amusing only : myservo.write(barre);

I have try several things , and i am thinking the trouble comes from the reading on I2C