Go Down

Topic: Moving two servos incrementally every second (Read 774 times) previous topic - next topic


Hi,  I am controlling the position of two servo's, (say A and B)

Let's say servo A and B both start at 1000ms position:


I want to be able to add to the position incrementally every second so say servo A starts at 1000 and moves 100ms every second and servo B starts at 1000 and moves 10ms every second.

I know I have to define two values, add to them every pass, then send that value to the servo position but I am slightly unsure on how to code this.

I think it is something like this:

Int APulse;
Int BPulse;

Void setup() {

APulse = 1000;
BPulse = 1000;

Void loop() {


(this is the bit I don't understand, adding to APulse, BPulse by 100ms and 10ms, then feeding it back in
To the next loop)

Delay 1000; delays by a second then back around again.


From looking into example code I am assuming I need to use the map command but don't quite understand it.



Jul 08, 2012, 05:31 pm Last Edit: Jul 08, 2012, 05:34 pm by drone Reason: 1
Break it down into several, smaller problems -- you need to:

1) Measure the passing of time (so you can do something once every second)
2) Change a value
3) Write the changed value

So, consider the following pseudo-code and go from there..

Code: [Select]

if currentTime - lastTime greater than threshold
      increment value
      write to motor
      set lastTime to now

To change the value in a variable, just set its new value - you can even use its old value to set it, e.g.:

Code: [Select]

  var1 = var1 + 10;

The scope of the variable determines its life...  If you declare it global (outside of a function), any changes you make are global, you can access it again from anywhere else, e.g.:

Code: [Select]

int foo = 1;

void setup() {

void loop() {

  foo = foo + 1;



Code: [Select]

Void loop() {


/* (this is the bit I don't understand, adding to APulse, BPulse by 100ms and 10ms, then feeding it back in
To the next loop) */


You can use:
Code: [Select]

   APulse = APulse + 100;
   BPulse = BPulse + 10;

Or the shorthand version:
Code: [Select]

   APulse += 100;
   BPulse += 10;

You may want to add code for what to do when you reach 2000.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp


Thankyou both very much. Very helpful and a lot simpler than I thought.

All the best


Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131