A linear servo that oscillate with moving center .

Hi Ladies and Gents, this is my first Arduino servo project!
First I want to thank the leaders and professional Arduino guru for taking the time to help me out.

I would like to get a servo to oscillate at a fixed amplitude, but it also have a moving center point.

I have seen the sweep example, however, how would you include that oscillation in there?
Let’s say move, 10 degree forward and 5 degree backwards untill it hit 180 degree…?

so far I have this…

hoping to set it up so that when the program see that the pos(position) is even moves forward, while when it is odd, moves backwards.

Right now, this code just moves forward really quickly, and go back to it’s original position

          if (pos % 2) {
            pos += 1;
          }
          //          for (pos = 0; pos < 180; pos += 10) // goes from 0 degrees to 180 degrees
          //          { // in steps of 10 degree1
          else {
            pos -= 0.5;
          }
          {
            myservo.write(pos);            // tell servo to go to position in variable 'pos'
            delay(100);                       // waits 15ms for the servo to reach the position
          }

winstondon: First I want to thank the leaders and professional Arduino guru for taking the time to help me out.

What about us poor grunts in the trenches ?

I think all you need is two variables. One represents the centre position and the other represents the oscillation. Add the two together to get the current servo position.

Suppose the centre position is 100 and the oscillation is -5 then the actual position would be 95.

...R

Hey Robin! Thanks so much for the quick reply.
What I hope to accomplish is to ask the servo to move through all these positions

50 to 60, (+10)
60 to 55, (-5)
55 to 65, (+10)
65 to 60, (-5)
60 to 70, (+10)

base on your insight I have edited the code this way.

         int pos = 1                                 // Set initial pos = 1 
         int newpos = 1                            // Set initial newpos = 1
         int mov = 0                                // Define mov = 0

          if (pos % 2) {                             //Test if pos is an odd number
           mov = 11;                               //If pos is odd -> move positive direction 11 degree
         }
         else {                                         //If pos is even -> move negative direction 5 degree
           mov = -5;
         }
         newpos = pos +mov;
         myservo.write(newpos);                // tell servo to go to position in variable 'pos'
         pos = newpos;                              // <- is this ok?

Testing my logic here… if we start with 1, myservo.write(newpos); would write

12 ( 1 +11)
7 (12 -5)
18 (7 +11)
13 (18-5)

looks good to me. (if you see something doesn’t make sense, please let me know… I will try this out when I am next to my servo this afternoon :D)

I hope this works! I will get back to you guys later :slight_smile:


Something by HarzardMind
he mentioned this line, from previous posts seems like it might do the same-thing?

void ServoMove(int position) 
{
 if(position > lastPosition)
 {
  // servo rotate left so many degrees
 }

else 
 {
  // rotate right so many degrees
 }
lastPosition = position;
}

But, wouldn’t lastPosition = position always makes them equal to each other so that it will keep moving towards the right?
:o

winstondon:
(if you see something doesn’t make sense, please let me know… I will try this out when I am next to my servo this afternoon :D)

Tell us the result of the tests first :slight_smile:

…R

Hmm, just tried this out..

          if (pos % 2) {                             //Test if pos is an odd number
            mov = 11;                               //If pos is odd -> move positive direction 11 degree
          }
          else {                                         //If pos is even -> move negative direction 5 degree
            mov = -5;
          }
          newpos = pos + mov;
          myservo.write(newpos);                // tell servo to go to position in variable 'pos'
          delay(100);
          pos = newpos;

          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
          }

It just drove the servo to one end quickly.. without the oscillation :o

I deleted the last part

         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

And run it again, this time nothing happens!

Puzzling :o

winstondon: Hmm, just tried this out..

Snippets are no good. Post the latest version of your complete program.

...R