Pages: [1]   Go Down
Author Topic: Moving two servos incrementally every second  (Read 334 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 46
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

0
Offline Offline
Sr. Member
****
Karma: 3
Posts: 323
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
  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:

int foo = 1;

void setup() {
}

void loop() {

  foo = foo + 1;
}

!c
« Last Edit: July 08, 2012, 10:34:51 am by drone » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8108
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:
   APulse = APulse + 100;
    BPulse = BPulse + 10;

Or the shorthand version:
Code:
   APulse += 100;
    BPulse += 10;

You may want to add code for what to do when you reach 2000.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

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

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

All the best

F
Logged

Pages: [1]   Go Up
Jump to: