Go Down

Topic: Servo Coding Convention (Read 524 times) previous topic - next topic

bill2009

I'm using a HITEC HS-325HB http://www.hitecrcd.com/products/analog/standard-sport/hs-325hb.html which I've read has a rotation range of 120 degrees with pulses from 900 to 2100 usec.  I can supply the min and max on the servo.attach(pin,min,max) but then I will have to servo.write(180) to move it to 120 degrees.  I guess I can write a little function to map it but it feels clunky - any suggestions?

Code: [Select]

void setaltitude(degrees){ //set the altitude servo
  altitudeservo.write(map(constrain(degrees,0,120),0,120,0,180); //servo degree requests from 0 to 120 have to be mapped to 0-180 for the servo library
}

robtillaart

Quote
map(constrain(degrees,0,120),0,120,0,180)


What is the range (min / max) of the variable degrees?

map(degrees, degreesMIN, degreesMAX, 0, 180) might be an alternative as the full range is used? (finer control?)
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

bill2009

range of degrees is 0-120.

actually, I think my question is becoming dumb.  I'll have to mount the servo whichever way it fits and adapt my coding to the reality.  I was just over-thinking it.

zoomkat

Below is some servo test code you can use to test your servo to find its actual rotation limits.

Code: [Select]

// zoomkat 10-4-10 serial servo test
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// for IDE 0019 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(2000); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control
  Serial.println("servo-test-21"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(1); 
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    }
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string
    int n;
    char carray[6]; //converting string to number
    readString.toCharArray(carray, sizeof(carray));
    n = atoi(carray);
    myservo.writeMicroseconds(n); // for microseconds
    //myservo.write(n); //for degees 0-180
    readString="";
  }
}
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Go Up