Pages: [1]   Go Down
Author Topic: Servo Coding Convention  (Read 386 times)
0 Members and 1 Guest are viewing this topic.
ottawa, canada
Offline Offline
God Member
*****
Karma: 5
Posts: 989
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
}
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12483
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?)
Logged

Rob Tillaart

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

ottawa, canada
Offline Offline
God Member
*****
Karma: 5
Posts: 989
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Tesla Member
***
Karma: 118
Posts: 8962
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
// 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="";
  }
}
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Pages: [1]   Go Up
Jump to: