3
#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(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 >= 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
  }
}

I have found this code that makes servo go 180 and back, but when I switch 180 degrees with 90, which is what I need, my servo doesn't do that (I tried the code in Arduino simulator).

Do I have to change something elese in the code (like delay time) so it whould go 90 degrees up and then 90 down?

Here is the link of circuit:

https://circuits.io/circuits/3597808-servo-90-degrees

7
  • 1
    when I swtich 180 degrees with 90 witch is what I need, my servo doesn't do that - So what does it do? ... (I tried the code in Arduino simulator) - What happens with a real Arduino and a real servo? Simulators are merely an approximation. They can never be as good as the real thing.
    – Majenko
    Commented Dec 28, 2016 at 10:42
  • @Majenko What he does is go 180 degrees up and starts sweeping between 180 and 90 degrees. It is really strage actually, beacuse I didnt inculde 180 degrees in code at all. You think Arduino simulator isn't accurate with servo's?
    – hehexd
    Commented Dec 28, 2016 at 13:32
  • It sounds like the simulator is rubbish. Get a real servo for your Arduino. They are dirt cheap.
    – Majenko
    Commented Dec 28, 2016 at 13:33
  • 1
    Make video of the output and then post it.
    – Dat Ha
    Commented Dec 28, 2016 at 14:12
  • 1
    @DatHa I edited the question.
    – hehexd
    Commented Dec 28, 2016 at 18:38

3 Answers 3

2

No you don't have to change any time delay in order to get it rotating by 90 degrees, delay would just change the speed of rotation of the servo. The simulator you are using is at fault here as the sketch with 180 changed to 90 works flawlessly.

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

MAKE SURE THAT YOU CHANGE BOTH OF THE 180'S TO 90.

1
  • 1
    That is what I did, and still nothing..
    – hehexd
    Commented Dec 28, 2016 at 18:40
2

I just created a simulation on Wokwi Arduino simulator based on your code and it works perfectly fine. Here is the permanent link enter image description here

1
  • When you first click the "play button", the servo animation jumps around wildly. Why is that happening?
    – VE7JRO
    Commented Feb 15, 2023 at 4:10
0

This line of code will properly set up the PWM timers to output the correct signal to move the servo to the value set in 'pos'

myservo.write(pos);

It has nothing do with speed. The servo will try to reach 'pos' as fast as possible.

The delay(15) in your code is the delay time between each "myservo.write(pos);"

The for loop works like this:

myservo.write(0); // Go to 0 degrees
delay(15); // Wait 15ms
myservo.write(1); // Go to 1 degrees
delay(15); // Wait 15ms
myservo.write(2); // Go to 2 degrees
delay(15); // Wait 15ms
myservo.write(3); // Go to 3 degrees
delay(15); // Wait 15ms

etc...

The time in seconds it takes to make a rotation is (range_in_degrees x 15)/1000

e.g. 180deg x 15ms/1000 = 2.7 seconds 90deg x 15ms/1000 = 1.35 seconds

Increasing the 15ms delay will make the servo rotate slower.

Also, make sure that you change the 180 degrees to 90 in BOTH for loops. If you change the value in one, the servo will slowly sweep from 0 to 90, then jump to 180 and slowly swing back to 0 and repeat. (or vice versa, depending on which one you forgot to change)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.