Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a attribute for the maximum angle for a servo motor. #914

Open
Koepel opened this issue Nov 12, 2024 · 1 comment
Open

Add a attribute for the maximum angle for a servo motor. #914

Koepel opened this issue Nov 12, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@Koepel
Copy link

Koepel commented Nov 12, 2024

It was mentioned in the Discord channel that the servo motor can only do 0...180 degrees.

Could a attribute be added to change the maximum angle?

Well known webshops have often servo motors for special situations with other angles. I see them from as low as 0...90 degrees up to 0...360 degrees (not continuous).
From what I read, the same pwm signal results into the full swing for that servo motor.

@Koepel Koepel added the enhancement New feature or request label Nov 12, 2024
@djedu28
Copy link

djedu28 commented Nov 14, 2024

The limitation of 0..90 you can do via software.
Encapsulate the function that controls the servo, passing the angle and limiting the angle.

You can use the Arduino constrain function.

void setAngle(int angle){ 
  int pos = constrain(angle, 0, 90);  // limits values ​​between 0 and 90
  myservo.write(pos);
} 

or complete:

#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 setAngle(int angle){ 
  int newPos = constrain(angle, 0, 90);  // limits values ​​between 0 and 90
  myservo.write(newPos);
} 

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
    setAngle(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
    setAngle(pos);                // tell servo to go to position in variable 'pos'
    delay(15);                    // waits 15ms for the servo to reach the position
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants