Go Down

Topic: RC servo control (Read 826 times) previous topic - next topic

Giles WALKER

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.

ash901226

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

Giles WALKER

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.

JimboZA

Try something like this....

(See my comments in there....)

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 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
 }
}
Arduino ethernet server here.... http://jimboza.gotdns.com:8085/

No PMs for help please

Giles WALKER

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.

JimboZA

#5
Feb 10, 2013, 02:33 pm Last Edit: Feb 10, 2013, 02:37 pm by JimboZA Reason: 1
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: [Select]
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
Arduino ethernet server here.... http://jimboza.gotdns.com:8085/

No PMs for help please

Giles WALKER

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

Giles WALKER

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.

JimboZA

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

If you're keen on delays  8) 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.
Arduino ethernet server here.... http://jimboza.gotdns.com:8085/

No PMs for help please

Giles WALKER

Ok. Thanks. I'll have a look.

Go Up