Go Down

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


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



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)


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.


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() {
  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()) {
    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
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131