Pages: [1]   Go Down
Author Topic: Servo  (Read 323 times)
0 Members and 1 Guest are viewing this topic.
Wales
Offline Offline
Full Member
***
Karma: 0
Posts: 244
Don't take things too seriously
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25713
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

Wales
Offline Offline
Full Member
***
Karma: 0
Posts: 244
Don't take things too seriously
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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) smiley-grin smiley-razz
« Last Edit: February 17, 2012, 09:35:05 am by Dane » Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code you can use to test your servo limits.

Code:
// 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
  }
}


Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Pages: [1]   Go Up
Jump to: