Pages: [1]   Go Down
Author Topic: Servo control wont work when inside a function  (Read 422 times)
0 Members and 1 Guest are viewing this topic.
Plymouth, uk
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Edison Member
*
Karma: 56
Posts: 2184
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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();
}
Logged

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

Plymouth, uk
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

India
Offline Offline
Newbie
*
Karma: 0
Posts: 17
im a newbiee..... but has a beleif and trust to find something new....
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 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!!! smiley-wink
Logged

Pages: [1]   Go Up
Jump to: