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
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.
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.
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.
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°).