netlabtoolkit / VarSpeedServo

Arduino library for servos that extends the standard servo.h library with the ability to set speed, and wait for position to complete
GNU Lesser General Public License v2.1
291 stars 127 forks source link

isMoving can't return "false" #19

Closed andreas-seiderer closed 6 years ago

andreas-seiderer commented 6 years ago

The "isMoving" function misses a return "false" if the conditions for "true" are not met.

"Flowing off the end of a value-returning function (except main) without a return statement is undefined behavior." source

Please add "return false;" at the end of the function and it should work like expected.

bool VarSpeedServo::isMoving() {
  byte channel = this->servoIndex;
  int value = servos[channel].value;

  if (value < MIN_PULSE_WIDTH) {
    if (read() != value) {
      return true;
    }
  } else {
    if (readMicroseconds() != value) {
      return true;
    }
  }

  return false;
}
GillHawk commented 6 years ago

Indeed, I missed that.. the shorter version would be:


bool VarSpeedServo::isMoving() {
  byte channel = this->servoIndex;
  int value = servos[channel].value;

  if (value < MIN_PULSE_WIDTH) {
    return (read() != value);
  } else {
    return (readMicroseconds() != value);
  }
}
pvanallen commented 6 years ago

I went with the original suggested fix for clarity. Thanks @andreas-seiderer