Go Down

Topic: Servo (Read 394 times) previous topic - next topic

Dane

I'm trying to use a servo to jingle a bell directly attached to the servo.

In my code I do a variation on this:-

Code: [Select]
void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(160);
  }
  else {
    servo1.write(20);
  }
}


Q1. How do I know the extremities of my servo? I've looked at the datasheet but I couldn't see any information there. I had to modify the above writes of 160 and 20 to other values before the servo was happy and didn't sort of hum or get stuck. I've also found I could make a bigger movement by increasing the 160 value - but how do I know where to stop?

Q2. If I use the code without a delay (which I cannot have in my project) then the servo darts only half way across the distance it would if there was a delay. Is there a way to get it to only begin moving back when it's reached the destination? Or do I have to use a millis style delay? Ideally, is there a library or function that would help with this?

Q3. Sometimes the servo chatters or moves a tiny bit independently of anything it's told to in the code. Is this normal for a cheap servo? Also, when powered from a battery it doesn't work as well as when powered from USB. This is with a brand new 9v battery. Confusing.

THANKS

AWOL

Q1: Use zoomkat's code to figure out the limits.
Q2: The servo library can only command the servo to a position, but it cannot know when it gets there (and servo slew rates vary), so yes, either a delay, or have a look at blink without delay to see how to eliminate delays.
Q3: Please don't encourage 9V battery proliferation. Get a proper supply.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Dane

#2
Feb 17, 2012, 03:24 pm Last Edit: Feb 17, 2012, 03:35 pm by Dane Reason: 1

Q1: Use zoomkat's code to figure out the limits.
Q2: The servo library can only command the servo to a position, but it cannot know when it gets there (and servo slew rates vary), so yes, either a delay, or have a look at blink without delay to see how to eliminate delays.
Q3: Please don't encourage 9V battery proliferation. Get a proper supply.


Hi AWOL, thanks very much for answers A.1-2. I'll see if I can find the zommkat code.

A.3, not so much.   (doesn't consider what is happening and why, just states an opinion) :D :P

zoomkat

Code you can use to test your servo limits.

Code: [Select]
// zoomkat 10-22-11 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// for IDE 0022 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h>
Servo myservo;  // create servo object to control a servo

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control
  Serial.println("servo-test-22-dual-input"); // so I can keep track of what is loaded
}

void loop() {
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string
    int n = readString.toInt();  //convert readString into a number

    // auto select appropriate value, copied from someone elses code.
    if(n >= 500)
    {
      Serial.print("writing Microseconds: ");
      Serial.println(n);
      myservo.writeMicroseconds(n);
    }
    else
    {   
      Serial.print("writing Angle: ");
      Serial.println(n);
      myservo.write(n);
    }

    readString=""; //empty for next input
  }
}


Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Go Up