hold the postion

hey...i want to control the position of the servo motor...i want it to turn then hold the position..but why the servo keep turn back to 0 degree then rotate it back.
here the coding

// Controlling a servo position using a potentiometer (variable resistor) 
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott> 

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
 
 // 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=140;    // 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 
}

how to make the servo hold the postion and never rotate it back?

You only ever send it to 140 "degrees" - when does it return to zero?

yes it will go to 140 degree..and the back to postion 0 degree..and it keep looping..

it spouse hold the position 140 degree right?

OK, I'll try again.
When does it return to zero?

when it go to 180, in second it will back to 0 again.the keep repeating

OK, sounds like your board is resetting.
How are you powering all of this?

arduino is from usb, servo from external 6.3V

hw to hold the servo

hw to hold the servo

In English?

Some servo test code you can try to see if the servo behaves the same.

// zoomkat 10-4-10 serial servo test
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// for IDE 0019 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.attach(7);  //the pin for the servo control 
  Serial.println("servo-test-21"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(1);  
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    } 
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string 
    int n;
    char carray[6]; //converting string to number
    readString.toCharArray(carray, sizeof(carray));
    n = atoi(carray); 
    myservo.writeMicroseconds(n); // for microseconds
    //myservo.write(n); //for degees 0-180
    readString="";
  } 
}

I tried your code from the original post and it worked perfectly. The motor did not return. I suspect a hardware problem.

You might want to expand. We are trying to help you and you give us minimal information.

  • What board are you using?
  • What motor? (Description, model number)
  • What is the rating of the motor? (eg. current, voltage needed to power it)
  • How is it wired?
  • What is this external 6.3V supply? Sounds an odd voltage to have lying around.
  • How much current can the 6.3V supply deliver?
  • What board or other gadget (eg. transistors) are you using to power the motor from the Arduino pins? Please provide a wiring diagram.
  • How about a photo of the setup?

yes it will go to 140 degree..and the back to postion 0 degree..and it keep looping..

it spouse hold the position 140 degree right?

...

when it go to 180, in second it will back to 0 again.the keep repeating

Well does it go to 140 or 180 degrees? Does it pause for a second exactly?

You know you have the code in "loop" right? Try putting it in setup instead.

thanks all..and Nick Gammon
it seem my hardware have the problem..when i try it using my MEGA it work perfectly, but when i try it on my Duemilanove the problem occur.
i think the problem is on my duemilanove