I'm trying to read values from an RC receiver into an ardruino uno r3 using this code:

int input[4] = {0, 0, 0, 0};
int *elevator = &input[0],
    *aileron  = &input[1],
    *rudder   = &input[2],
    *throttle = &input[3];
int rawInput[4] = {0, 0, 0, 0};
int *rawElevator = &rawInput[0],
    *rawAileron  = &rawInput[1],
    *rawRudder   = &rawInput[2],
    *rawThrottle = &rawInput[3];
// Data from joystick, takes data in every 25000 microseconds
*rawElevator = pulseIn(elevatorIn,HIGH,25000);
*rawAileron  = pulseIn(aileronIn,HIGH,25000);
*rawRudder   = pulseIn(rudderIn,HIGH,25000);
*rawThrottle = pulseIn(throttleIn,HIGH,25000);

// If raw values are valid copy to input array
for(int i = 0; i < 4; i++)
    if(rawInput[i] > 500)
        input[i] = rawInput[i];

I know that's it would probably be better to learn how to ue interrupts for this, but I want to figure out why pulseIn() won't work right now. I've tried a variety of pins (both PWM and not) for the input and I keep running into the problem that I can't get all 4 to read correctly at once. Each works in isolation, but when more pins are plugged in there is some sort of interference between them. For example, throttle alone works fine and when pitch is plugged in they both work, but when the yaw or roll pins are connected the new inputs will work but throttle and pitch begin returning 0. There are also some cases where instead of 0 it returns very large and erratic values. No matter what order pins are plugged in a similar situation happens with seemingly no pattern. How would I go about finding the issue here?

Edit: I played with the timeout parameter on pulseIn and it seems that if I decrease it to 5000 or remove it so that it sets to the default (1 second?) everything works properly, but I'm not sure why.

  • It is not easy to say something about it. It could be software or hardware or both. When you have a problem, we like to see the sketch that causes that problem. The code that you show it not a full sketch and it does not print the values, therefor we do not know how you can see erratic values. What is the frequency of the signals? Set the timeout to captures 3 to 10 pulses. Please don't use those pointers. Use a for-statement to read the pulses and another for-statement to print the values. Keep it as simple as possible to avoid problems: en.wikipedia.org/wiki/KISS_principle – Jot Jan 20 at 4:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.