Go Down

Topic: Controlling servo motor using serial commands (Read 2306 times) previous topic - next topic

amp279

I'm wondering if anyone can help me get a servo motor running via serial commands using Arduino and the Servo.h library. I've written some code but not sure if this will get the motor running...can somoene please feedback and advise? Thanks  :)

----------------

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
               // a maximum of eight servo objects can be created

int pos = 0;    // variable to store the servo position

void setup()
{
 myservo.attach(9);  // attaches the servo on pin 9 to the servo object
 myservo.write(90); //set servo to midpoint as default
 Serial.begin(9600);
 Serial.print("Ready");
}

void loop()
{
  if (Serial.available() > 0)
 {
   int value = Serial.read(); //reads the incoming byte

 
   switch(value)
   {
     case 'a':
     pos = 1023;
     pos = map(pos, 0, 1023, 0, 179);
     myservo.write(pos);
     break;
   
     case 'b':
     pos = 0;
     pos = map(pos, 0, 1023, 0, 179);
     myservo.write(pos);
     break;
   
     case 'c':
     pos = 1023/2;
     pos = map(pos, 0, 1023, 0, 179);
     myservo.write(pos);
     break;
   
     case 'd':
     pos = 1023/4;
     pos = map(pos, 0, 1023, 0, 179);
     myservo.write(pos);
     break;
   
     case 'e':
     pos = 1023/8;
     pos = map(pos, 0, 1023, 0, 179);
     myservo.write(pos);
     break;
   
     case 'f':
     pos = -1023;
     pos = map(pos, 0, -1023, 0, 179);
     myservo.write(pos);
     break;   // serial input read and stored
            //in shaft to be used as input value for position
   }
 }
 
   //Default movement without serial input
   
     for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees
 {                                  // in steps of 1 degree
   myservo.write(pos);              // tell servo to go to position in variable 'pos'
   delay(15);                       // waits 15ms for the servo to reach the position
 }
 for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
 {                              
   myservo.write(pos);              // tell servo to go to position in variable 'pos'
   delay(15);                       // waits 15ms for the servo to reach the position
 }
   }

PaulS

Code: [Select]
    pos = 1023;
    pos = map(pos, 0, 1023, 0, 179);

Well, this is a little silly. Why not simply set pos to 179, and eliminate the call to map?

How does case f differ from case a?

Quote
//Default movement without serial input

Lets all repeat together. "In the event of a conflict between the comments and the code, the code is ALWAYS right".

This comment is wrong. The for loops will wave the server all over the place, whether there was serial data received, or not.

amp279

I'm actually trying to write code so that when a user sends a value between 0-1023, the program would be able to map it accordingly and rotate the motor. I'm not sure how to use the serial commands for me to be able to do this without using 1023 cases :-/

With case f, I'm trying to make the motor move in the opposite direction...

I know that time control is neccessary to make the motor move in different direction...but with the Servo.h library, is this necessary?

Any help would be great!

amp279

I haven't had a chance to test this code yet, so I was wondering what exactly this does to the motor? Does this roate the motor from 0-180 degrees?

How do I get the motor running on full roation:/

Thanks for the feedback...you are right. I've just started learning coding   :o

for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees
{                                  // in steps of 1 degree
  myservo.write(pos);              // tell servo to go to position in variable 'pos'
  delay(15);                       // waits 15ms for the servo to reach the position


PaulS

Quote
I'm actually trying to write code so that when a user sends a value between 0-1023, the program would be able to map it accordingly and rotate the motor. I'm not sure how to use the serial commands for me to be able to do this without using 1023 cases

When the user enters 1000 in the Serial Monitor, for example, the Arduino will receive '1', '0', '0', and '0'. You need to read all the data sent, storing the data in a character array (NULL terminated, of course), and convert the string to an int (atoi() might prove useful).

Quote
With case f, I'm trying to make the motor move in the opposite direction.

Code: [Select]
    case 'f':
    pos = -1023;
    pos = map(pos, 0, -1023, 0, 179);
    myservo.write(pos);
    break;   // serial input read and stored

The input value to the map function is at one end of the from range. It will be mapped to the corresponding end of the to range, which will result in an output of 179 for the input of -1023.

This is exactly the same output you got when you sent an 'a'.

You don't make servos rotate one way or the other. You tell the servo to go TO a position, and it figures out where the new position is relative to where it is now.

Quote
I know that time control is neccessary to make the motor move in different direction...but with the Servo.h library, is this necessary?

Time has nothing to do with which direction a motor moves.

Quote
Any help would be great!

Even mine?  ;D

amp279

Thanks, starting to make better sense. :)

So what is the writemicroseconds() function for if time has nothing to do with it :-?

So what would it take to make the motor rotate continuously... :-/

Groove

The writeMicroseconds sets the length of the PPM pulse used to drive the servos. This pulse repeats every 20ms, and its width determines the position of the servo.

Are you using modded (aka 'ex-serovs') devices?
Per Arduino ad Astra

amp279

#7
Nov 19, 2010, 02:33 pm Last Edit: Nov 19, 2010, 02:47 pm by amp279 Reason: 1
umm i dont think so since i dont know what that is:D, it's just a standard servo..

so would it make a difference if i used servo.write(value) to servo.writeMicroseconds(value)?...

if i did a servo.write(25), this would set the shaft to 25 degrees right?
but would this make my motor rotate continuously? or would there be something else i would need to be doing to make it rotate continously...i'm guessing i'd need to set a time value to make it rotate continuously...not sure if this is correct, but if this is correct, how would i do that :o

help is much appreciated...thankyou :)

Groove

The 'degrees' is only an approximation.
Many servos won't/can't turn 180 degrees.
I don't uderstand when you make the distinction between a motor and a servo.
The motor in a servo cannot turn continuously, otherwise it would strip the gears when it hits the end-stop.
Can you decribe your servo and where you got them?
Per Arduino ad Astra

amp279

#9
Nov 19, 2010, 03:11 pm Last Edit: Nov 19, 2010, 03:27 pm by amp279 Reason: 1
It's just a servo that will be used in an application. i'm wondering how a servo could control a rotor....guess a servo doesn't need to move continuously to drive anything for movement....

Groove

Are we talking about an industrial servo?
Per Arduino ad Astra

amp279

Okay, it might be an industrial servo. But either way, it took me till now to realize that i'm not going to be able to make the servo rotate a full revolution without physically altering it to to make it a continous rotation servo.

So, using the servo library, any value i pass into servo.write() function will control the speed at which it rotates....

i'm learning how to store a data (1000) in an array and convert it to an int...so i can pass it to as a parameter to control the serv0 :-?

any tips.....links???? :-/



Go Up