# Servo code invert trouble

**URL:** <https://forum.arduino.cc/t/servo-code-invert-trouble/627720>\
**Category:** Programming\
**Created:** [December 24, 2019, 9:18pm UTC](https://forum.arduino.cc/t/servo-code-invert-trouble/627720 "2019-12-24T21:18:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Classified123](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@Classified123](https://forum.arduino.cc/u/Classified123)\
**Post date:** [December 24, 2019, 9:18pm UTC](https://forum.arduino.cc/t/servo-code-invert-trouble/627720/1 "2019-12-24T21:18:31Z")

</div>

Hello,

I am using the sweep code in from ardunio and I would like the servo to start from 180 Degrees and go to 90 Degrees. (It can't start at 0 Degrees because of the way my servo is mounted. Therefore it must start at 180 Degrees) I modified he sweep code (see code below) but when I run it, it doesn't work. It just goes to 180 Degrees and stays there.  
How do I fix this?

Thanks.

This is the code:

/\* Sweep  
by BARRAGAN [http://barraganstudio.com](http://barraganstudio.com)  
This example code is in the public domain.

modified 8 Nov 2013  
by Scott Fitzgerald

> **[Servo Motor Basics with Arduino | Arduino Documentation](https://docs.arduino.cc/learn/electronics/servo-motors)**
>
> Learn how to connect and control servo motors with your Arduino board.

\*/

#include \<Servo.h\>

Servo myservo; // create servo object to control a servo  
// twelve servo objects can be created on most boards

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

void setup() {  
myservo.attach(10); // attaches the servo on pin 9 to the servo object  
}

void loop() {  
for (pos = 180; pos \<= 90; 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 = 90; pos \>= 180; 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  
}  
}

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [December 24, 2019, 9:28pm UTC](https://forum.arduino.cc/t/servo-code-invert-trouble/627720/2 "2019-12-24T21:28:07Z")

</div>

```cpp
for (pos = 180; pos <= 90; pos += 1)

```

Think.

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [December 24, 2019, 9:31pm UTC](https://forum.arduino.cc/t/servo-code-invert-trouble/627720/3 "2019-12-24T21:31:14Z")

</div>

> TheMemberFormerlyKnownAsAWOL:
> 
> ```arduino
> for (pos = 180; pos <= 90; pos += 1)
> 
> ```
> 
> **Think.**

Thought could be a useful concept.

What about

```arduino
for (pos = 180; pos >= 90; pos -= 1)

```

...R

---

<div class="post-metadata">

**Author:** ![slipstick](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@slipstick](https://forum.arduino.cc/u/slipstick)\
**Post date:** [December 24, 2019, 9:37pm UTC](https://forum.arduino.cc/t/servo-code-invert-trouble/627720/4 "2019-12-24T21:37:22Z")

</div>

And the other one. If you start at 90 do you really think it will be greater than 180 even for the first loop?

Steve

---

<div class="post-metadata">

**Author:** ![zoomkat](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/zoomkat/32/1190177_2.png) [@zoomkat](https://forum.arduino.cc/u/zoomkat)\
**Post date:** [December 24, 2019, 10:41pm UTC](https://forum.arduino.cc/t/servo-code-invert-trouble/627720/5 "2019-12-24T22:41:58Z")

</div>

Maybe to get the servo started off on the right foot, do something like the below in the code setup.

```cpp
int pos = 90;    // variable to store the servo position

void setup() {
  myservo.attach(10);  // attaches the servo on pin 9 to the servo object
  myservo.write(pos);
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 12:12am UTC](https://forum.arduino.cc/t/servo-code-invert-trouble/627720/6 "2021-05-06T00:12:38Z")

</div>

Closed on May 6, 2021, 12:12am UTC


