selectively detaching servo to fix Jitter issue

Hello all, I’m looking to do something very simple. I want a servo to move based on an IR remotes input. Everything is functioning, but the servo jitters constantly in all positions. I have ruled out hardware and power faults, as well as any kind of interference. What I want to do is attach the servo when I make the input, have the servo move, and then have it detach to prevent Jitter. I am aware that detaching the servo will make it “go limp”, but that isn’t important in my situation. I’m having issues figuring out how exactly to make what I want happen. Any suggestions would be greatly appreciated. Here’s my code:

#include <Servo.h>
#include <IRremote.h>

unsigned long Value2 = 0xFE7887;
unsigned long Value1 = 0xFEFA05;
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results result;

Servo servo1;

void setup() {              

  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  servo1.attach(10);

}
void loop() {

  if (irrecv.decode(&result)) {
    Serial.println(result.value, HEX);
    irrecv.resume(); // Receive the next value
  }

  if(result.value == Value1) {
    servo1.write(170);
  }

  if(result.value == Value2) {
    servo1.write(10);   
  }

}

Thank you all in advance for your help!

I thought Servo & Serial didn't work together?

-jim lee

They do apparently, works just fine.

Look at the BlinkWithoutDelay example. When you set the servo, save the current time in a variable. then, monitor millis() and when an elapsed time has passed (check the speed of your servo, but I'm guessing 500 ms should be enough) then switch the servo off.

Something like this (please note, this is untested code)

long servo1Millis = 0;

void loop() {
// all your other loop code here

if(result.value == Value1) {
    servo1.write(170);
    servo1Millis = millis();
  }

long currentMillis = (long)millis();

  if((currentMillis - servo1Millis) > 500) {
  //Switch servo1 off
   servo1.detach();
  }

//the rest of your loop code here, repeat above for servo2
}

Yes! That worked flawlessly, thanks for your help.
The final code for those who are wondering:

#include <Servo.h>
#include <IRremote.h>

unsigned long Value2 = 0xFE7887;
unsigned long Value1 = 0xFEFA05;
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results result;
long servo1Millis = 0;

Servo servo1;

void setup() {              

  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  servo1.attach(10);
}
void loop() {
  servo1.attach(10);

  if (irrecv.decode(&result)) {
    Serial.println(result.value, HEX);
    irrecv.resume(); // Receive the next value
  }

  if(result.value == Value1) {
    servo1.write(170);
    servo1Millis = millis();
  }

  long currentMillis = (long)millis();

  if((currentMillis - servo1Millis) > 500) {
    servo1.detach();
  }

  if(result.value == Value2) {
    servo1.write(10);
    servo1Millis = millis(); 
  }

}

I think there's still a slight issue in the code... You attach the servo in the main loop, so every time the loop runs through it gets attached, and then probably detached in the timeout check. I would move the attach to wherever you want to write to the servo, as follows:

void loop() {
  //servo1.attach(10);  //Remove this statement

  if (irrecv.decode(&result)) {
    Serial.println(result.value, HEX);
    irrecv.resume(); // Receive the next value
  }

  if(result.value == Value1) {
    //attach over here
    servo1.attach(10);
    servo1.write(170);
    servo1Millis = millis();
  }

  long currentMillis = (long)millis();

  if((currentMillis - servo1Millis) > 500) {
    servo1.detach();
  }

  if(result.value == Value2) {
    //attach over here
    servo1.attach(10);
    servo1.write(10);
    servo1Millis = millis(); 
  }

}