Go Down

Topic: Servos (Read 604 times) previous topic - next topic

slipstick

Btw can I just add the potentiometer like in the knob example and use the code from there too?
Yes that will get you one potentiometer controlling one servo. Once that is going you can fairly easily work out  how to add the second potentiometer and servo.

Steve

vinceherman

The knob tutorial is a great piece to use to learn about servos.
It is also good for helping you find physical limits of movement in the servo itself.

Samuel72718

Can I use a 9V battery instead if I use 2 servos?

vinceherman

2 problems with that.

1. Most servos, especially the small cheapies, die a quick death on 9v.
Get a power supply appropriate for the applicaiton.  2 AA's is often a good choice.

2. 9v PP3 or smoke detector batteries are very poor at providing more than a tiny amount of current.  The current that actual motors pull (your servos each have a motor inside) quickly pulls the voltage in the 9v down and the whole system shuts down.

Samuel72718

After reading the last post I realize that my servos are probably fried, I'll get new ones. Any recommendations for good ones that aren't too expensive?

vinceherman

We have had a lot of good conversation about the technical side of your project, but I did not see much on what you are actually trying to do.  Recommendations on what is best will rely a lot on the intended application.

I bought a bag of 20 sg90 servos for $27.  That is cheap.

I bought $80 servos for my 1/4 scale RC plane.  Those were pretty good.  But there are better servos out there, I was staying 'cheap'. (look at OMG servos)





Samuel72718

Great news! its working! however when the potentiometer reaches the end on one side the servo starts spinning around like crazy. it says 180 degerees on the servo but it just keep spinning round and around! is there a way to stop them from doing that?

Heres the code im using:
Code: [Select]
#include <Servo.h>  // add servo library

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

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

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

void loop() {
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val);                  // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there
}

groundFungus

#22
Apr 15, 2018, 06:16 pm Last Edit: Apr 15, 2018, 06:17 pm by groundFungus
Not all servos will go to 0 and/or 180.  Modify the the sweep example with serial prints in the for loops and increase the delay.  Watch the values that are written to the servo to find the limits that the servo will handle and don't send angles outside of the limits (adjust the values in the map function).

slipstick

Let me guess. The servo you're using is an SG90? There seem to have been quite a few of those around lately that don't stop correctly at their end points but spin round and round. It's actually a faulty servo and there's not much you can do about it except get it replaced or, as groundFungus says, limit the range of commands you send to it.

Steve

Samuel72718

#24
Apr 16, 2018, 08:40 pm Last Edit: Apr 16, 2018, 08:41 pm by Samuel72718
thanks, ill fix the servo  so that they will stop. Another question: How do i add another servo i tried modifying the code but im not very good at ptogramming.
heres what i wrote:

Code: [Select]
#include <Servo.h>  // add servo library

Servo servo1;  // create servo object to control a servo
Servo servo2

int potpin1 = 0;  // analog pin used to connect the potentiometer
int val1;    // variable to read the value from the analog pin
int potpin2 = 1;
int val2;

void setup() {
  servo1.attach(9);  // attaches the servo on pin 9 to the servo object
  servo2.attach(8);
}

void loop() {
  val1 = analogRead(potpin1);            // reads the value of the potentiometer (value between 0 and 1023)
  val1 = map(val1, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  servo1.write(val1);                  // sets the servo position according to the scaled value
  val2 = analogRead(potpin2);
  val2 = map(val2, 0, 1023, 8, 180);
  servo2.write(val2);
  delay(15);                           // waits for the servo to get there
}

slipstick

Once you've put the missing ; in so it compiles does it work? If not what does it do that isn't what you want it to do?

Steve

Samuel72718

now it works! thanks alot, now all thats left is using the map() thing to make the servos stop spinning and i think i can do that myself! Thanks for all the help.

Go Up