Go Down

Topic: Moving Servomotors (Read 3472 times) previous topic - next topic

Oeildelys

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.



Code: [Select]

 #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;
    }
  }
}


vinceherman

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

Oeildelys

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.

vinceherman

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:

Code: [Select]
Servo myservo;
Then I attach the servo:
Code: [Select]
myservo.attach(9);
Then I write to the servo:
Code: [Select]
myservo.write(pos);

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

Oeildelys

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 ?

Oeildelys

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 :

Code: [Select]


#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;
  }
}

AWOL

#6
Dec 04, 2017, 02:56 pm Last Edit: Dec 04, 2017, 02:57 pm by AWOL
Code: [Select]
ID=int((inputString[0]-48)*1000);Oops

BTW "- '0' " Is much more intuitive than "- 48"
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

Oeildelys


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 !

AWOL

Quote
What's wrong?
Too big.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

Oeildelys


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

AWOL

In fact, it's to select the servo to be written to. ;)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

Oeildelys

In fact, it's to select the servo to be written to. ;)
Yes :) But my servos are still not moving ...

AWOL

Code: [Select]
myservo.write(ID, pos);
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

Oeildelys


AWOL

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

But it looks a bit odd, to me.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

Go Up