Servo: write all the time?


Once I determine the desired servo position, do I need to write the value once and again in the loop()? Or can I just send the write() command only when the device that controls the servo (a pot, a switch, etc.) changes its value?


Once you write a value, the servo will stay at that value until a new command comes in.

If you use

then it will change by itself based on that variable if it's tied to a pot or switch or whatever.

When you say "write the value once and again in the loop", I'm assuming you do it once in setup to give it a starting position? In which case, yes, you need to put one of them in the loop, otherwise it won't update.

The servo library itself loops continuously once you send a position command. So, the servo is always "running" and if you try to push it out of position it should push-back.

OK, so I don't need to Myservo.write() once and again in the loop(). I should use a flag that indicates that nothing changed, i.e., the switch remains on or off so to avoid writing the servo angle again if not necessary.


Or just write it again. It doesn't cost much in terms of execution time and it's simpler to program.

void loop() {
  int ServoOutputValue = getNewServoValueSomehow();
  Myservo.write(ServoOutputValue); //don't care if the value changed or not - just write it