Moving Servomotors

Hi everyone,

I’m student and it’s my first Arduino project.
I need to move 3 Servomotors (they all have an ID : 1, 2 and 3).
I’d like to write a code that allow me to :

  • write first the ID of the Servo
  • write the angle (position) of the Servo

Examples : in the serial monitor, I write “1180” → the servo 1 moves to the angle 180° (and returns me “1180”).
I write “2090” → the servo 2 moves to the angle 90°.

Below the code I use to help me :

This code let me write the position for only ONE servo and returns me the position of the angle.

Thanks for your help.

 #include <Servo.h> 
 
Servo myservo; 

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete
int pos = 0;

void setup() {
  Serial.begin(115200);
  inputString.reserve(200);
 
}

void loop() {
  serialEvent(); 
  if (stringComplete) {
    pos=int((inputString[0]-48)*100)+int((inputString[1]-48)*10)+int(inputString[2]-48);
   Serial.println(pos);
    myservo.write(pos);
    inputString = "";
    stringComplete = false;
       
  }
}

void serialEvent() {
  while (Serial.available()) {
    char inChar = (char)Serial.read();
    inputString += inChar;
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

You gave a decent amount of info about your project.
You posted you code (Using code tags! YAY!)

Do you have a question?
It often helps if you describe how the sketch actually behaves, and how that differs from your expectations.

Does the servo move at all? I do not see anywhere that you attach the servo.
Have you successfully got the servos to move? You might start with looking at the servo sweep example here

Thanks for your answer.

You’re right, I didn’t describe the difference between how this code works and my expectations :

The code I put in the previous message moves one small servo (see picture attached) by sending the position in the serial monitor.

The code I would like to write should move three AX-12A servos (see picture attached) by sending the number of the ID and the position wanted in the serial monitor (as the examples “1180” and “2090”).
These servos are not “attached”

For the moment, I arrived to move the AX-12A servos with another code that doesn’t allow me to choose the position.

Actual sketch corresponding to the code in my first message.png

My expectation.png

Oeildelys:
The code I put in the previous message moves one small servo (see picture attached) by sending the position in the serial monitor.

Did you actually run this sketch and see the servo move?
I do not understand how this could be. When I use the servo library, I instantiate the servo object:

Servo myservo;

Then I attach the servo:

myservo.attach(9);

Then I write to the servo:

myservo.write(pos);

In your sketch, I do not see where you run the attach function.

Sorry, you’re completely right.
When I copied ans pasted this code I made a mistake and removed the line “myservo.attach(9);”

In fact, the code runs with this line !
Thank you to have noticed me of that.

To run my AX-12A servos, I attach them to the white plug (as picture attached).
So the code should be different, right ?

Configuration of attachment.png

Hi,

I moved forward with this topic :

Now I receive back what I write in the serial monitor → for example I send “1180” or “2045” and the serial monitor write it.

But my servos still don’t move …
Could you help me ?

Below the code I wrote :

#include <Servo.h> 
 #include <SPI.h>
#include <ServoCds55.h>
ServoCds55 myservo;

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete
int ID = 0;
int pos = 0;

void setup() {
  Serial.begin(115200);
  inputString.reserve(200);
 
}

void loop() {
  serialEvent(); 
  if (stringComplete) {
  
  ID=int((inputString[0]-48)*1000);
    pos=int((inputString[1]-48)*100)+int((inputString[2]-48)*10)+int(inputString[3]-48);
   Serial.println(ID+pos);
  myservo.write(ID, pos); 
    inputString = "";
    stringComplete = false;
       
  }
}

void serialEvent() {
  while (Serial.available()) {
    char inChar = (char)Serial.read();
    inputString += inChar;
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}



void controlServo(int val) {
  switch (val) {
    case '1':
      inputString[0]=val;
      case '2':
      inputString[0]=val;
      case '3':
      inputString[0]=val;
      break;
  }
}

ID=int((inputString[0]-48)*1000);Oops

BTW "- '0' " Is much more intuitive than "- 48"

Quote

Code: [Select]

ID=int((inputString[0]-48)*1000);

Oops

What's wrong ?

Thanks for the help regarding "-'0'" ; I didn't know how to deal with the ASCII code !

What's wrong?

Too big.

In fact, it's to put the ID at the first place in front of the position.
To write "1180" or "2045 or even "3090"...

In fact, it's to select the servo to be written to. :wink:

AWOL:
In fact, it’s to select the servo to be written to. :wink:

Yes :slight_smile: But my servos are still not moving …

 myservo.write(ID, pos);

AWOL:

 myservo.write(ID, pos);

This is wrong ?

I don't know, because I don't know what a "ServoCds55" is.

But it looks a bit odd, to me.

AWOL:
I don't know, because I don't know what a "ServoCds55" is.

I have not used it, but a library by that name is used to drive a servo shield for Dynamixel servos. These are not standard RC hobby servos, but higher end robotics servos with more complex communication.

...but do they need IDs in the range 1000 ... 2000 ... 3000 ?

Does the ServoCds55 library have any examples or test sketches in it? Have you tried them?

I like to break troubleshooting down into its smallest parts.
Can I get the servo to move?
Can I read input and display it to the serial monitor?
Can I use that input to command the servos?

Start out with making the servos move using the test in the library.

Thank you for your quick answers.
Yes, there is an test example called ServoCds55_Test, that allows to move AX-12A Servos.

I tried it successfully.
But this test example doesn't allow me to give the instruction to a particular servo to move to a position of angle (I send "1180", I want the Servo 1 to move to the position 180°).

Instead of printing (ID+pos) try printing ID and pos separately just before the myservo.write(). I think you’ll see your problem instantly.

Steve