Go Down

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

flotilla

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:

ServoA.writeMicroseconds(1000);
ServoB.writeMicroseconds(1000);

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

ServoA.writeMicroseconds(APulse);
ServoB.writeMicroseconds(BPulse);

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

Thanks!

drone

#1
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
  then
      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;
}


!c

johnwasser


Code: [Select]

Void loop() {

   ServoA.writeMicroseconds(APulse);
   ServoB.writeMicroseconds(BPulse);

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



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: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

flotilla

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

All the best

F

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!

Arduino
via Egeo 16
Torino, 10131
Italy