Mapping range for servo.write()

Hi

In the knob example on the tutorial site the analog in signal is mapped from 0 to 179. Shouldn´t this be from 0 to 180?

Here is the tutorial example:

void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}

And here is the extract form the servo library:

void Servo::write(int value)
{
if(value < MIN_PULSE_WIDTH)
{ // treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds)
if(value < 0) value = 0;
if(value > 180) value = 180;
value = map(value, 0, 180, SERVO_MIN(), SERVO_MAX());
}
this->writeMicroseconds(value);
}

Also a servo can turn from 0 to 180 and not 1 to 180 or 0 to 179.
Or do i missunderstand something?

1 Like

Not all servos are capable of 180 degree rotation. The code does correctly map from 0 to 180 to the appropriate servo limit values.

1 Like

I have modified my previous post. I guess there was a missunderstanding. Now it should be clear what i mean. (English is not my native language)
I mean the error is in the tutorial example. They map form 0/1023 to 0/179. And this should be 0/180?

The servo range 0-179 is just a mapped us (microsecond) range in the servo library, as the control signal is really a us range of ~500 to ~2500. The default range is set in the library probably to help keeping people from damaging their servos. You can use servo.attach(pin, min, max) to set your own default us range. Below is some servo test code you can try to compare us vs the degree commands.

// zoomkat 10-22-11 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// for IDE 0022 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control 
  Serial.println("servo-test-22-dual-input"); // so I can keep track of what is loaded
}

void loop() {
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string 
    int n = readString.toInt();  //convert readString into a number

    // auto select appropriate value, copied from someone elses code.
    if(n >= 500)
    {
      Serial.print("writing Microseconds: ");
      Serial.println(n);
      myservo.writeMicroseconds(n);
    }
    else
    {   
      Serial.print("writing Angle: ");
      Serial.println(n);
      myservo.write(n);
    }

    readString=""; //empty for next input
  } 
}