Pages: [1]   Go Down
Author Topic: RC servo control  (Read 683 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, I am completely new to Arduino. I am trying to control a RC hobby servo with Arduino. I have pasted in the 180 sweep available from the library and it works fine. However, I am wanting the servo to sweep from various angles and at various speeds. I have modified the code so that it does this....but I also want it to pause for a few seconds every now and again so it is not constantly moving. I can't seem to get it to do this..any ideas? Thanks.
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Simplest way is to impliment delay. But for a more profesional looking code use what the technique shown on blink without delay. How abou u post yourcode for us to really help you
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the reply. I'm sorry but I have not got any code yet (except the 180 degree sweep from the library). Could you explain how to alter this code...or give me an example of say..0 to 180 degrees sweep with a 5 second pause at the 180, then to 90 with a 3 sec delay..then back to 0. I reckon I will then be able to make my own adjustments to the code to get the effect I'm after. Thanks.
Logged

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

Try something like this....

(See my comments in there....)

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
  }

delay(5000) //5000 = 5 sec  <<<<<<<<<<<<<<<<<<<<<<< new


  for(pos = 180; pos>=90; pos-=1)     // goes from 180 degrees to 90 degrees   <<<<<<<<<<<<< changed
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }

delay(3000) //3000 = 3 sec  <<<<<<<<<<<<<<<<<<<<<<< new


  for(pos = 90; pos>=0; pos-=1)     // goes from 90 degrees to 0 degrees   <<<<<<<<<<<<< changed
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}
Logged

The Elders of the Internet know who I am.....

0
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you JIMBOZA,
That looks just what I need....only I can't get it to 'verify' in the Arduino software. Any chance you could have a quick loo and see what the problem is? Thanks again for your time and knowledge.
Logged

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

Ooops... that's the trouble with typing and not testing.... I left the ; off the delays. They all ought to look like this one:

Code:
delay(3000); //<<<<<<<<< ; was missing

Edit... I put the ;'s in on mine and it compiles- not tested it on an actual Arduino with a servo though
« Last Edit: February 10, 2013, 08:37:39 am by JimboZA » Logged

The Elders of the Internet know who I am.....

0
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That's verified. Thanks I'll give it a go.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That has worked fine. Thanks for that.
Just another quick question...if you want to put a delay in between the 'loops' can you? So it runs through a sequence, then pauses for some seconds...?
Thanks.
Logged

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

Yep... you could add a delay() as the last line of loop(), before the closing }.

If you're keen on delays  smiley-cool you should have a look at the Blink without delay sketch: the delay() command is eschewed because while the Arduino is sitting in the delay, nothing else can happen. Blink without delay allows you to "delay" without really delaying.
Logged

The Elders of the Internet know who I am.....

0
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok. Thanks. I'll have a look.
Logged

Pages: [1]   Go Up
Jump to: