fluctuating PulseIn values while using Servo (arduino micro)

Hello everybody,

A few weeks ago I started making a quadcopter. I've got all the pieces that I need, that is not a problem. The main problem that I'm actually facing is that when I use pulseIn function (to read values from my Rx) and send them to my motors, values are fluctuating :frowning: .

My code is (for now) quite simple:

SETUP:

I attatch my 4 servos (they are callibrated)

LOOP:
I read the value from my Rx (pulseIn)
I restrict it to a specific domain (1050-1900)
I write them to my 4 ESCs (using servo.writeMicroseconds())

#include <Servo.h>

//connection to the ESCs' pins
#define PIN_ESC_ARG 10
#define PIN_ESC_ARD 11
#define PIN_ESC_AVD 9
#define PIN_ESC_AVG 6

//connection to the Rx
#define THROTTLE_PIN A0

//declaration des differents esc
Servo ESC_ARG; //Back-left
Servo ESC_ARD; //Back-Right
Servo ESC_AVD; //Front-Right
Servo ESC_AVG; //Front-Left


int time_high;

void setup() {

  pinMode(THROTTLE_PIN,INPUT);

  Serial.begin(9600);

  //On lie les servos aux pins corrects et definition des intervalles de temps passé haut

  //Linking the Servos to the correct pins and definitions for the time_high interval
  ESC_ARG.attach(PIN_ESC_ARG,1060,1895);
  ESC_ARD.attach(PIN_ESC_ARD,1060,1895);
  ESC_AVD.attach(PIN_ESC_AVD,1060,1895);
  ESC_AVG.attach(PIN_ESC_AVG,1060,1895);
}

void loop() {

  //Recuperation du temps passé haut du signal recu (tant que maintenant, juste le THROTTLE)

  //getting the time_high of the signal from the Rx (for now, only reading THROTTLE line)


  time_high=pulseIn(THROTTLE_PIN,HIGH);

  //Forces the time_high to be in interval [1050-1900]
  time_high=constrain(time_high,1050,1900);

  Serial.println("Time_high: ");
  Serial.println(time_high);


  ESC_ARG.writeMicroseconds(time_high);
  ESC_ARD.writeMicroseconds(time_high);
  ESC_AVD.writeMicroseconds(time_high);
  ESC_AVG.writeMicroseconds(time_high);


}

The results in the serial console are:

time_high: 
1461
(60ish times)

time_high: 
1443
time_high: 
1443
time_high: 
1444
time_high: 
1443
time_high: 
1443
time_high: 
1432
time_high: 
1434
time_high: 
1433
time_high: 
1433
time_high: 
1432
time_high: 
1434
time_high: 
1440
time_high: 
1432
time_high: 
1434
time_high: 
1434
time_high: 
1433
time_high: 
1433
time_high: 
1435
time_high: 
1434
time_high: 
1434
time_high: 
1433
time_high: 
1443
time_high: 
1450
time_high: 
1442
time_high: 
1437
time_high: 
1444
time_high: 
1445

time_high: 
1460 (and back to the beginning, it's always the same ;) )

I should say that I tried to disable interrupts before reading values then re-enabling them, but the motors do not rotate at all. >:(

Here we go, that's all I have got here and so I'm I Calling for some help because even if I can code in C, I'm not that familiar with Arduino and do not know what to do (even after weeks of researches on the subject! :confused: )

Thank you for having read and thank you in advance for your help :slight_smile:

How are you powering the ESCs and the Arduino?

and back to the beginning, it's always the same

Do you get exactly the same values?

What happens if you don't connect the ESCs?

Pulsein will likely work okay for a single input channel but if you want to capture more than one channel, then you're very likely to miss some of the pulses when using pulsein.

Here's a method of capturing pulse data from multiple channels. I tested this method and it appears to capture all the pulses. I only used the code to capture three channels but it wouldn't be hard to expand on the code to capture as many channels as you wish.