Go Down

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

#### bill2009

##### Jun 16, 2011, 11:12 pm
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

#1
##### Jun 16, 2011, 11:54 pm
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

#2
##### Jun 17, 2011, 12:38 am
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

#3
##### Jun 17, 2011, 03:03 am
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="";  } }`
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