Arduino and Spektrum receiver to servo

I an trying to get a servo to be controlled through an Arduino with the control signal coming from a Spektrum receiver. The code posted below is the closest I have come to something that works. Unfortunately the signal coming from the receiver is not smooth and causes the servo to jitter. If the servo is not attached through code (servo.attach) the signal is smooth. I’m stumped on how to smooth out the signal while still being able to control a servo. I have tried serval different software filters that do help, but cause the response time to diminish greatly. Any suggestions? Thanks.

  #include <Servo.h>
  Servo servo;
  
  void setup() {
    Serial.begin(9600);
    pinMode(8, INPUT);
    servo.attach(9);
  }
  
  void loop() {
    int val = pulseIn(8, HIGH);
    if (val == 0 || val < 1000 || val > 2000) {
      return;
    }
    val = map(val, 1000, 2000, 0, 180);
    val = constrain (val, 0, 180);
  
    servo.write(val);
    Serial.println(val);
  }
   if (val == 0 || val < 1000 || val > 2000) {

Last time I checked, 0 was less than 1000.

    val = map(val, 1000, 2000, 0, 180);
    val = constrain (val, 0, 180);

The only values that map will be called for are those between 1000 and 2000, resulting in an output value between 0 and 180.

You then need to constrain this why?

If the servo is not attached through code (servo.attach) the signal is smooth.

What is the servo doing? If there is not significant load on the servo, and the time between calls to servo functions are relatively short, you might be able to detach the servo, call pulseIn, and reattach the servo before writing to it.

You might also use a pin on a different port (2 through 7) for the servo, specifically a non-PWM pin.

Check of how much the jitter is. My guess is, that the jitter is always an increase of time of similar value. You might be able to observe worse jitter at certain servo positions. This is usually caused by the interrupt used by the Servo library to refresh the servos. If that happens towards the end of your pulseIn call, it will read a longer time that you would have measured at the pin. But don't think about disabling interrupts during pulseIn, that would only produce Jitter because the servos aren't handled correctly.

What you can do? For one you can smooth out the read values and discard readings that seem unlikely. That works fairly well if done properly. If you're up to it and have to handle only a few servos, you can refresh them while you don't expect another input.

Korman

@PaulS Yes, 0 is less than 1000. It was originally just val == 0, but later I added the rest and didn't remove that piece of code. I tried moving the servo to pin 4 and I still have the same problem. I've tried detaching and reattaching servos like that in the past without much success.

@Korman I thought it might have something to do with interrupts. I don't know if this is what you are talking about, but the problem I am having isn't with the servo moving randomly. Rather that the signal from pulsein is not smooth. (When everything is centered the value fluctuates between 98-102 with an occasional drop to 2-9) This is what causes the servo to jitter.

Would it be better to use delayMicroseconds and digitalWrite rather than the Servo library?