Thread: Arduino Programming for Servo motor

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    5

    Arduino Programming for Servo motor

    Hi all. I am working on a project that requires using an arduino microcontroller to control the motion of a servo motor.
    The arduino language is pretty much C.


    The original code is of the form:
    Code:
    #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 
    } 
     
     
    void loop() 
    { 
      for(pos = 0; pos <= 40; 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 = 40; pos>=0; 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 
      } 
    }

    but the problem is I don't want the motor's motion to behave linearly (constant increments of the pos variable). My desired motion is a sinusoidal like function.
    Does anyone have any suggestions on how to approach this?


    I am quite new to programming, especially with microcontrollers.

    My guess is that instead of the pos+=1, I would need something like pos+=sin(argument). I am not sure what that argument should be.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    > I am quite new to programming, especially with microcontrollers.
    Good thing this is mostly a trigonometry problem. Once the math is solved, coding it should be simple.

    > My guess is that instead of the pos+=1, I would need something like pos+=sin(argument). I am not sure what that argument should be.
    You must understand what it is you want and how to solve the problem on paper, before you can program a solution to it. Perhaps you do know what you want, but you aren't sure how to explain it.

    As it stands, I'm not totally clear on what you want. Do you want the position to be sinusoidal, varying with time? So it appears to move slowly from (for example) positions 0 to 5, then begins to move more quickly from positions 6-17, reaches a maximum speed-between-positions at around positions 18-22, then begins to slow the speed-between-position until position 40, and then repeats the same in reverse, moving slowly, accelerating toward position 20, then decelerating toward 0?

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Not sure either, but you can do a mock test by printing the pos value in spaces, then a '.'
    Should look like a sine wave.


    Don't forget to #include <math.h>

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    This appears to be a stepper motor like interface (as opposed to a brushless dc motor controller using high frequency pulse width modulation and phase changes). What is the minimum delay between steps that you can send to the motor? You may need a better type of timer interface to provide more accurate delays as opposed to using delay().

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Hi all. I am working on a project that requires using an arduino microcontroller to control the motion of a servo motor.
    Servomotor - Wikipedia, the free encyclopedia
    It would seem that your servo (not an expensive one) would only be capable of a constant angular rate of change.

    So if for example,
    myservo.write(0);
    myservo.write(10);

    took 1 second to compete, then
    myservo.write(0);
    myservo.write(20);

    would take 2 seconds.

    I don't think changing the step interval will make it move any quicker.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arduino project programming question
    By qtip2293 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2011, 03:57 PM
  2. Arduino programming
    By Terese in forum C++ Programming
    Replies: 5
    Last Post: 12-11-2010, 01:03 PM
  3. Servo rotation
    By buzmay in forum C Programming
    Replies: 1
    Last Post: 04-27-2010, 08:11 AM
  4. Making Servo rotate 180 degrees
    By streamline in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2008, 11:19 AM
  5. function to run stepper motor
    By sunnycyboid in forum C Programming
    Replies: 1
    Last Post: 11-01-2002, 05:43 AM