Pages: [1]   Go Down
Author Topic: servo code  (Read 1430 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 25
Who you gonna call?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

with the Sparkfun inventor kit there is a circuit that controls a single servo with arduino. based on this code i want to build a robot, But i am having trouble understanding the underlined part of the code  at the very bottom

/*
SparkFun Inventor's Kit
Example sketch 08


Servo servo1;  // servo control object


void setup()
{
  // We'll now "attach" the servo1 object to digital pin 9.
  // If you want to control more than one servo, attach more
  // servo objects to the desired pins (must be digital).

  // Attach tells the Arduino to begin sending control signals
  // to the servo. Servos require a continuous stream of control
  // signals, even if you're not currently moving them.
  // While the servo is being controlled, it will hold its
  // current position with some force. If you ever want to
  // release the servo (allowing it to be turned by hand),
  // you can call servo1.detach().

  servo1.attach(9);
}


void loop()
{
  int position;
 
  // To control a servo, you give it the angle you'd like it
  // to turn to. Servos cannot turn a full 360 degrees, but you
  // can tell it to move anywhere between 0 and 180 degrees.

  // Change position at full speed:

  servo1.write(90);    // Tell servo to go to 90 degrees

  delay(1000);         // Pause to get it time to move

  servo1.write(180);   // Tell servo to go to 180 degrees

  delay(1000);         // Pause to get it time to move

  servo1.write(0);     // Tell servo to go to 0 degrees

  delay(1000);         // Pause to get it time to move
 
  // Change position at a slower speed:

  // To slow down the servo's motion, we'll use a for() loop
  // to give it a bunch of intermediate positions, with 20ms
  // delays between them. You can change the step size to make
  // the servo slow down or speed up. Note that the servo can't
  // move faster than its full speed, and you won't be able
  // to update it any faster than every 20ms.

  // Tell servo to go to 180 degrees, stepping by two degrees
 
  for(position = 0; position < 180; position += 2)
  {
    servo1.write(position);  // Move to next position
    delay(20);               // Short pause to allow it to move
  }

  // Tell servo to go to 0 degrees, stepping by one degree

  for(position = 180; position >= 0; position -= 1)
  {                               
    servo1.write(position);  // Move to next position
    delay(20);               // Short pause to allow it to move
  }
}
Logged

Belgium
Offline Offline
Edison Member
*
Karma: 68
Posts: 1920
Arduino rocks; but with my plugin it can fly rocking the world ;-)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The sparkfun kit comes with lots of documentation and this is extremely well documented code. So I really wouldn't know what to add.
Logged

Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9631
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

#7 below would be a good place to start.

http://forum.arduino.cc/index.php/topic,148850.0.html
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Newbie
*
Karma: 0
Posts: 25
Who you gonna call?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I THINK I NEED TO REPRASE THIS QUESTION. MY NEW QUESTION IS, WITH THE UNDERLINED CODE I DONT UNDERSTAND THE CODE AND WHAT MEANS WHAT TO ARDUINO. I AM MAKING A ROBOT AND I WANT TO USE SERVOS, HOWEVER THAT UNDERLINED CODE IS WHAT CONTROLS SPEED BUT I DONT UNDERSTAND HOW TO CONTROL THE SPEED WITH THAT CODE FORMAT. I HOPE YOU CAN UNDERSTAND MY QUESTION THANKS IN ADVANCE
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26332
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I THINK I NEED TO REPRASE rewrite THIS QUESTION without all the shoutiness.
Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 25
Who you gonna call?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

sorry i didnt realize caps lock was on smiley-red
Logged

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9631
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Below is the servo sweep example from the arduino IDE. There is a delay between the small servo moves so the overall movement of the servo is slowed down.

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 loop()
{
  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

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4400
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What your undelined code is doing is going from 0 to 180 in steps of 2 degrees... Each servo1.write(position) is 2 degrees further than the last write, since the for's position += 2 is incrementing in 2's. (Zoomcat's is similar, but in 1's). BUT, each pass through the for also includes a delay(20) (or 15 in zoomcat's example) which causes a pause of 20ms between each 2 degree movement step.

See here to read up on how "for" works.

Without that loop and delay, if the servo was at say 0 degrees and you sent it a servo1.write(180) it would have gone from 0 to 180 as fast as it was able. Servo speed is quoted by the makers as the time to move through 60 degrees, such as 0.16 seconds for this one. (That's the speed with no load, obviously slower when it's trying to move something.)

HTH?
Logged

Roy from ITCrowd: Have you tried turning it off and on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Offline Offline
Newbie
*
Karma: 0
Posts: 25
Who you gonna call?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks guys you are awsome
Logged

Pages: [1]   Go Up
Jump to: