Problems with pulseIn()

This code is designed to read from four channels from an RC receiver (PWM), and output that to 4 servos and 2 brushless motor ESC’s. I read from the receiver using PulseIn(), and output using 4 servo classes.

I am having trouble with reading the 4th channel from the receiver (yaw), using these lines:

  pitchValue    = pulseIn(pitchPin, HIGH, 25000);
  rollValue     = pulseIn(rollPin, HIGH, 25000);
  throttleValue = pulseIn(throttlePin, HIGH, 25000);
  yawValue      = pulseIn(yawPin, HIGH, 25000);

If I swap the third and 4th lines, suddenly I cannot read the throttle. What is going on?

Full Code:

#include <Servo.h>
/*
Motor Layout(From the top, 1&2 are the front):
  1 2
  5 6
1&6 spin couterclockwise, 2&5 clockwise
*/

//Yaw Sensitivity Variable(The factor at which yaw is factored into the motor speed
int yawsen = 0.2;

//Declares Channels for Transmitter Inputs
int pitchPin    = 8;      //Channel 1
int rollPin     = 4;      //Channel 2
int throttlePin = 2;      //Channel 3
int yawPin      = 7;      //Channel 4

//Declares Output pins for Servo Control (3, 5, 6, 9, 10, 11 ONLY)
int servo1 = 3;
int servo2 = 5;
int servo5 = 6;
int servo6 = 9;

Servo Servo1;
Servo Servo2;
Servo Servo5;
Servo Servo6;

//Declares motor ESC PWM locations (Yaw and Throttle control) (3, 5, 6, 9, 10, 11 ONLY)
int motorsCW  = 10;   //motors 2 & 5
int motorsCCW = 11;   //motors 1 & 6

Servo motorsCWPWM;
Servo motorsCCWPWM;

//Variables to hold input values 
int pitchValue    = 0; 
int rollValue     = 0;
int throttleValue = 0;
int yawValue      = 0;


void setup() {
  Serial.begin(9600);
  
  //Declares 2 channels as motor speed outputs for Yaw and throttle control
  motorsCWPWM.attach(motorsCW);
  motorsCCWPWM.attach(motorsCCW);
  
  //Declares Outputs for Servos
  Servo1.attach(servo1);
  Servo2.attach(servo2);
  Servo5.attach(servo5);
  Servo6.attach(servo6);
  
}

void loop() {
  //Reads the values from the transmitter and writes them to the respected variables
  //pulseIn returns a value from 1000~2000
  pitchValue    = pulseIn(pitchPin, HIGH, 25000);
  rollValue     = pulseIn(rollPin, HIGH, 25000);
  throttleValue = pulseIn(throttlePin, HIGH, 25000);
  yawValue      = pulseIn(yawPin, HIGH, 25000);  
  
  //Debugging
  Serial.print("ThrottleValue: ");
  Serial.println(throttleValue);
  Serial.print("YawValue: ");
  Serial.println(yawValue);
  
  //Keeps inputs from transmitter inside the bounds 1000-2000 using basic if/elif loop- compacted to save space
  if(pitchValue < 1000){pitchValue = 1000;}         else if(pitchValue > 2000){pitchValue = 2000;}
  if(rollValue < 1000){rollValue = 1000;}           else if(rollValue > 2000){rollValue = 2000;}
  if(throttleValue < 1000){throttleValue = 1000;}   else if(throttleValue > 2000){throttleValue = 2000;}
  if(yawValue < 1000){yawValue = 1000;}             else if(yawValue > 2000){yawValue = 2000;}
  
  //Converts the raw PWM to degrees, 0-180
  pitchValue    = (pitchValue - 1000) * 0.18;
  rollValue     = (rollValue - 1000) * 0.18;
  throttleValue = (throttleValue - 1000) * 0.18;
  yawValue      = (yawValue - 1000) * 0.18;
  
  //Main servo control logic
  Servo1.write(((pitchValue + rollValue) / -2) + 180);      //additive interferance, inverted
  Servo2.write(((pitchValue - rollValue + 180) / 2));   //destructive interferance, inverted
  Servo5.write(((pitchValue - rollValue + 180) / -2) + 180);               //additive interferance
  Servo6.write((pitchValue + rollValue) / 2);          //destructive interferance

  //Motor speed Control logic, used for Yaw control (Can be improved to not allow motors to spin for when throttle is zero, currently a proof of concept)
  motorsCWPWM.write(throttleValue + (yawsen * yawValue));
  motorsCCWPWM.write(throttleValue + (yawsen * yawValue));
  
  delay(1);
}
//Declares Output pins for Servo Control (3, 5, 6, 9, 10, 11 ONLY)

The Servo library does not require PWM pins.

Could it be that your radio is not sending servo pulses every 20 milliseconds, as is traditional? If the interval was longer it is possible that you hit the 25 millisecond timeout before the next pulse arrives. I would try 30000 microseconds (or more?) for the timeout to see if that works around the problem.

Eventually you will want to measure the pulses with pin change interrupts so you don't spend so much processor time in pulse timing loops.

Thanks for your reply!

How would I go about coding pin change interrupts?

http://playground.arduino.cc/Code/ReadReceiver
And another article Reading Remote Control Receiver Values with Arduino | Ryan Boland
(I had originally posted the wrong link. This is a better article)

But what receiver do you have? If it is capable of PPM, then you can get the entire set of channels on one pin.
I have not tried it yet, but I purchased some inexpensive hobby king spektrum receivers that support PPM. 6 channels on a single arduino pin.