Hi everybody, this is my first post as a newbie. I couldn't see this topic in the forum so I though I would start this thread.
I hooked up a standard 180deg servo last night, and loaded up the servo sweep example sketch that comes with the IDE.
everything worked a treat with the servo doing its 180 degree forwards and back again.
I then decided to take the code out of loop() and dump it straight into a new function, and then call that function from setup(). As soon as I do this, the servo decides to move about for a few seconds then stop at 90 degrees. I am at a loss as to why this is happening. I'm not sure if it is a variable scope issue (int pos?), or something I am completely missing!
Code below for any suggestions etc. Thanks in advance.
Neil
// Sweep
// by BARRAGAN <http://barraganstudio.com>
// This example code is in the public domain.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
void servo_move();
}
void loop()
{
}
void servo_move()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}