help servo motors

hi there, i dont hnow where to post this, so I post it here :slight_smile:

i want to control this servo motor Hextronik HXT900 Servo Specifications and Reviews with Arduino board -uno. i was trying library Servo.h (just copy&paste code that was there). It doesnt work.
than i thought that servo motors behave just like potenciometer , only middle is not output but input, so I connect to digital 9(PWM) and i thought that it's working - it was moving , but cant control it - for example with potentiometer and servo was very hot after my experimetns :slight_smile:
any idea how to conquer that beast ? :slight_smile:
sorry for english.

i try this before I establish? this topic. it's not working at all.
I try this code:

int t=0;
int d=5;
void setup(){
  pinMode(9,OUTPUT);
}

void loop(){
   analogWrite(9,t);
   t = t + d;
   if (t == 0 || t == 255)
     d=-d;
   delay(21);
}

it's better than nothing , but it's not fully working.

Some simple servo test code you can try.

// 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.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(10);  
    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="";
  } 
}

Thanks for your example.

How it can be modified to rule more than just one servomotor?

I'm using Arduino Uno and I don't find any example about how to rule up to 5 or 6 servomotors with the serial monitor.

Have a nice day

How it can be modified to rule more than just one servomotor?

Some simple test code for two servos:

// zoomkat 11-22-10 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 0019 and later
// Powering a servo from the arduino usually DOES NOT WORK.
// two servo setup with two servo commands
// send eight character string like 15001500 or 14501550

#include <Servo.h> 
String readString, servo1, servo2;
Servo myservo1;  // create servo object to control a servo 
Servo myservo2;

void setup() {
  Serial.begin(9600);
  myservo1.attach(6);  //the pin for the servo control 
  myservo2.attach(7);
  Serial.println("servo-test-21"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(10);  
    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); //see what was received
      
      // expect a string like 07002100 containing the two servo positions      
      servo1 = readString.substring(0, 4); //get the first four characters
      servo2 = readString.substring(4, 8); //get the next four characters 
      
      Serial.println(servo1);  //print ot serial monitor to see results
      Serial.println(servo2);
      
      int n1; //declare as number  
      int n2;
      
      char carray1[6]; //magic needed to convert string to a number 
      servo1.toCharArray(carray1, sizeof(carray1));
      n1 = atoi(carray1); 
      
      char carray2[6];
      servo2.toCharArray(carray2, sizeof(carray2));
      n2 = atoi(carray2); 
      
      myservo1.writeMicroseconds(n1); //set servo position 
      myservo2.writeMicroseconds(n2);
    readString="";
  } 
}

I'm using Arduino Uno and I don't find any example about how to rule up to 5 or 6 servomotors with the serial monitor

You have to deal with this project one issue at a time. First one can't power 5 or 6 servos from the +5vdc pin on an Arduino, there is just not enough current to handle that much load. So you first have to come up with how you will power the servos, large enough battery or a external regulated +5vdc 6 amp power supply? Then work on how you are going to wire all that up as there is a requirement to have any external DC power share a ground connection with the Arduino. When you have that all sorted out then you can revisit the software questions.

To control 6 servos in software will require a servo library that can support that many: Arduino Playground - MegaServo

Lefty

Thanks for your answers. I feed the servos directly with the PC cutting some USB cables to get the +5V.

I'm going to work always with Arduino connected and ruled by the PC.
Can¡t wait to try your examples.

I thoght that the pde that let us control so many servos was going to be something like a charachter and a number sended via the COM port (Something like analogwrite($pinnumber, "a120"). So if the character is the letter "a" and the number is "120" it will move the first servo to position 120º, if the character is "b" and the number is 100 the move the second servo to position 100º...

I'm going to study your examples to try to understand how do they works (comments on the code are really apreciatted for newbies like me).

Cheers

zoomkat:
Some simple servo test code you can try.

// 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.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(10); 
    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="";
  }
}

thanks for that code, very much :slight_smile: