Go Down

Topic: Servo twitch on startup (Read 2 times) previous topic - next topic

zoomkat

Simple servo test code. I noticed in your code that a comment said pin 8 was being used, but the code used pin 9. Also, if the 5V BEC can be set to 6v, the servo will perform better. Does the BEC function the same as an UBEC that is used to power servos (In usually see the BEC associated with brushless motors)?

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
  }
}
Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

JimboZA


Have you tried to write the servo position before the attach?


My (probably wrong) assumption was that you could only do a write to an attached servo.... What's the thinking behind this please?
Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

AWOL

Quote
What's the thinking behind this please?
A write sets the timing for the pulse, but if there's no pin attached to the Servo object, it effectively changes the default position for the Servo.
Say, for example, your application always parks the servo at zero degrees.
At startup, the default behaviour of the Servo object would be to move the servo to 90°, and you would do an immediate write to set it to zero. It may well twitch.
Do the write before the attach, no twitch.
Or, you could just put the horns on with a ninety degree displacement.
"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.

JimboZA

Ah ok, thanks.

That makes sense, especially in the light of this part....

Quote
the default behaviour of the Servo object would be to move the servo to 90°


... which I didn't know in the first place.
Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

ticklechicken

zoomkat - Yes, its acutally a UBEC.  It's an old habit from the r/c car days to call them BEC's.  I switched the UBEC output from 5V to 6V and I still get the twitch.

AWOL - I changed my starting position from 50deg to 90deg.  I still get the twitch.

Let me give everyone more detail.  When the myservo.attach(servo) line is executed, the servo rotates 30deg clockwise.  It doesn't matter if I write a myservo.write(start) before this line or not.  The attach line always causes the 30deg cw rotation.  I've tried manually moving the servo horn to some random position before energizing the system.  Even then, the servo rotates 30deg cw before going to its start position.  I'm stumped.

Go Up