Pages: [1]   Go Down
Author Topic: Controlling servo motor using serial commands  (Read 2015 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley

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

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 613
Posts: 49270
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 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

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 613
Posts: 49270
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
    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
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, starting to make better sense. smiley

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... :-/
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Per Arduino ad Astra

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
« Last Edit: November 19, 2010, 08:47:30 am by amp279 » Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Per Arduino ad Astra

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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....
« Last Edit: November 19, 2010, 09:27:51 am by amp279 » Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Are we talking about an industrial servo?
Logged

Per Arduino ad Astra

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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???? :-/


Logged

Pages: [1]   Go Up
Jump to: