1

Ok so I've used nicks code and have the code running once ok.... I the noticed however I had a fast 180 sweep and then it returned under control of code. I then noticed the comments wrt pulselen of 1000 being zero degrees and using 1500 as 90 degrees.... Wow so I get a single movement which holds but it doesn't go 90 degrees...

I'm using an Adafruit servo shield.

I can make my servo move. However, I want to stop it at 90 degrees without it returning to 0 degrees each time.

What am I missing?

The code I've used is as follows:

void setup()
{
  pwm.begin();
  pwm.setPWMFreq(60);  // Analog servos run at ~60 Hz updates
}

void loop()
{
  for (uint16_t pulselen = SERVOMIN; pulselen < SERVOMAX; pulselen++)
  {
    pwm.setPWM(0, 0, pulselen);
  }
  delay(500);
}
6
  • If you don't want it to keep going back to zero, don't have a loop that resets to zero when it reaches 90. Commented Aug 1, 2015 at 11:21
  • You are right by putting it in the setup the code does only runs once. However my problem is the servo arm sweeps 90deg as coded and returns to where it started (not coded). Putting the code outside Void Loop () or using a count based approach makes no difference. To recap my issue is how do I hold the servo at 90degrees ???
    – Andy
    Commented Aug 1, 2015 at 13:18
  • Please post the amended code. You tell it to go to 90 degrees and then you do not tell it to do anything else.
    – Nick Gammon
    Commented Aug 1, 2015 at 22:11
  • @NickGammon please note that this is in the main loop(), so it will wait half a second and then reset to SERVOMIN. Commented Aug 3, 2015 at 23:01
  • 1
    please note that this is in the main loop(), so it will wait half a second and then reset to SERVOMIN - incredibly quickly, yes. The loop executes very quickly so it would go back up to SERVOMAX within a short time. In fact my testing indicates that it executes that loop in 72 ms.
    – Nick Gammon
    Commented Aug 4, 2015 at 1:15

2 Answers 2

1

The code in loop is called repeatedly. Thus it does this:

  • Go from SERVOMIN to SERVOMAX very rapidly (I measured 72 ms)
  • Pause half a second
  • Repeat the above

To make it pause at SERVOMAX you need to either not repeat the loop, or put it all in setup, eg.

#include <Adafruit_PWMServoDriver.h>
#include <Wire.h>

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

#define SERVOMIN  150 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX  600 // this is the 'maximum' pulse length count (out of 4096)
void setup()
{
  pwm.begin();
  pwm.setPWMFreq(60);  // Analog servos run at ~60 Hz updates
  for (uint16_t pulselen = SERVOMIN; pulselen < SERVOMAX; pulselen++)
  {
    pwm.setPWM(0, 0, pulselen);
  }

}

void loop()
{
}
4
  • Thanks Nick, using your code I've added a delay just after. I then noticed when executing the code the servo arm rapidly swings 180 degrees and then it slowly sweeps back the other direction due to the delay as 'pulselen' increases.....
    – Andy
    Commented Aug 15, 2015 at 20:11
  • Thanks Nick, using your code I've added a delay just after pwm.setPWM(). I then noticed when executing the code the servo arm rapidly sweeps 180 degrees and then it slowly sweeps back the other direction due to the delay and as 'pulselen' increases..... But what is causing the initial fast sweep as I only need the slow sweep?
    – Andy
    Commented Aug 15, 2015 at 20:20
  • Hi nick I noticed if I changed servo max and min to 1500 and 1000 respectively I loose the fast sweep..
    – Andy
    Commented Aug 15, 2015 at 20:33
  • Servos differ, and the pulse width for the sweep at max and min positions may well vary. You have it solved then? Good.
    – Nick Gammon
    Commented Aug 15, 2015 at 21:29
0

The variable pulselen in your code corresponds to the angle you want the servo to turn to. For most servos the pulselen is supposed to be a number from 1000 to 2000, where 1000 corresponds to 0 degrees and 2000 corresponds to 180 degrees, but some servos may be different. It's probably safe to assume that you want a value close to 1500.

Of course the servo takes a while to get to its position, so the best approach usually involves changing pulselen gradually until you get to the desired position.

Once you get to 90 degrees stop changing pulselen. At that point you should still be sending the PWM but keep it at 1500. You'll want to keep sending the PWM signal, or the servo will have no resistance to external forces.

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.