Go Down

Topic: Servo control wont work when inside a function (Read 487 times) previous topic - next topic

nedlin

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

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

Runaway Pancake

Don't call the function with "void".
void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  void servo_move();
}

void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  servo_move();
}
"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

nedlin

Thanks ever so much, it had to be something stupid!!!  :smiley-roll-blue:

deepu1994

hi>>>>
void tell the mc (microcontroller) to return none value to its calling function>>>>
if u use it here... it wont return anything which in turn doesnot move the knob of u r servo...... even if u place anyother code instead of servo u wont get it working!!! ;)

Go Up