1. ## How would I gradually decrement and increment?

I have this code that I'm using to control a Servo and I'd like to modify it so that it gradually decreases the degrees of sweep by a set amount (say 1 degree per pass). Right now it sweeps back and forth from ZERO to 180. I can manually change that by changing the numbers but I'm guessing there's an easier way to do this. Any ideas?

I'd like the sweep range range to decrease (i.e. say zero to 180, 1 to 179, 2 to 178, etc then eventually turning around and increasing to full width passes again). Thanks in advance for ideas!

Code:
```// Sweep
// by BARRAGAN <http://barraganstudio.com>
// This example code is in the public domain.

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

2. First off... is this a DC servo or a stepper motor?

Sounds like you nee some serious for() loops....

Code:
```for (int x = 0; x < 90; x++)
for (int y = 0; y < 90; y++)
{ myservo.write(x - y);
delay (15)
myservo.write(x+y); }```

3. Originally Posted by CommonTater
First off... is this a DC servo or a stepper motor?

Sounds like you nee some serious for() loops....

Code:
```for (int x = 0; x < 90; x++)
for (int y = 0; y < 90; y++)
{ myservo.write(x - y);
delay (15)
myservo.write(x+y); }```
Thanks for the input and cool username lol

As far as the motor it is a DC Servo. I have it working with the above code that I'd posted (taken from a library for this microcontroller) but would like to modify the code to fit my application.

4. Actually I just realized, my code sample has a bug in it but I'm betting you'll find it....

5. Originally Posted by CommonTater
Actually I just realized, my code sample has a bug in it but I'm betting you'll find it....

Don't bet your life on it! I'm a beginner when it comes to programming

but is it a set of brackets missing for the first "for" loop to encapsulate the second?

6. Originally Posted by 777funk
Don't bet your life on it! I'm a beginner when it comes to programming

but is it a set of brackets missing for the first "for" loop to encapsulate the second?
Nope... that's legal if you are only executing one statement, in this case the inner loop.

First, you can do it with a single loop... but there's one other thing...

Code:
```for ( int x = 90; x > 0; x--)
{ SetServo(90 - x);
sleep(30);
SetServo(90 + x);
sleep(30); }```
Get it now?

7. I think that makes sense to me. I'll try it. What if I want to offset it by say + or - 10 with a maximum still capping at 180?

8. I just tried the following and it did some weird things. Definitely not what I was thinking it'd do (work from the outside bounds (67 and 107 in my case) and gradually get smaller as the numbers head toward the midpoint of 87 degrees). Here's my source:

Code:
```#include <Stepper.h>

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int x = 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(int x = 20; x > 0; x--)
{
myservo.write(87 - x);
delay(9);
myservo.write(87 + x);
delay(9);

}

}```

9. Ok I changed it to x -= 1 and it's actually more within the range but it's not doing what I had hoped (going the full range and decrementing a degree per side i.e. 67 to 107, 68 to 106, 69 to 105 and so on).

10. Add some diagnostic prints in there so you can see the values of x when it moves...

Also I see you are working in what looks like C++ ... perhaps you would get better help in the C++ forum...

11. It's arduino C/C++. A very strange hybrid.

Since you have the arduino library, you can just use delay().
Arduino - Delay

EDIT: oh nevermind. I see you figured that out.

12. I finally got it to do what I've been trying to get it to do. But it seems to not be sweeping the entire degree range (around 40 degrees from 67 to 107).

Code:
```void loop()
{
for (a = 0; a < 15; a++)
{
for(pos = 87 + a; pos < 107 - a; pos += 1)
{
myservo.write(pos);
delay(15);
}

for(pos = 107 - a; pos>=88 + a; pos-=1)
{
myservo.write(pos);
delay(15);
}
}
for (a = 15; a >= 0; a--)
{
for(pos = 87 + a; pos < 107 - a; pos += 1)
{
myservo.write(pos);
delay(15);
}

for(pos = 107 - a; pos>=88 + a; pos-=1)
{
myservo.write(pos);
delay(15);
}
}
}```

13. Ok... those loops are looking ominously complex and might well get you into unpredictable behavior...

If you gave it the sequence of:
Code:
```myservo.write(106);
delay(15);
myservo.write(31);
delay(15);
myservo.write(87);
delay(15);
myservo.write(159);```
exactly what happens?

14. Originally Posted by 777funk
I think that makes sense to me. I'll try it. What if I want to offset it by say + or - 10 with a maximum still capping at 180?
In that case make your center position a variable.

To keep it in bounds use some pre-move testing...
Code:
```pos = (pos > 180) ? 180 : pos;
pos = (pos < 0) ? 0 : pos;

or if your language doesn't support that...

if (pos > 180) pos = 180;
if (pos < 0) pos = 0;```

15. Originally Posted by CommonTater
Ok... those loops are looking ominously complex and might well get you into unpredictable behavior...

If you gave it the sequence of:
Code:
```myservo.write(106);
delay(15);
myservo.write(31);
delay(15);
myservo.write(87);
delay(15);
myservo.write(159);```
exactly what happens?
Well shoot! Looks like my servo burned itself out so I'll have to continue the research later this week (got more ordered).