newbie question regard servo sweep example

Hi,

I am very new to the arduino world and have just started playing around with tweaking the example programs for practice, but I need some help. I would like to modify the code below (its the code from the servo sweep example) so that I can hook up an additional servo to pin 10 to simultaneously sweep in the opposite direction. If anyone could provide example code for that I would greatly appreciate it.

// 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
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
}

Just four additional lines of code. Make a start.

One further hint: The opposite position is 180 - the first servo's position.

Here what I tried. I’m sure I probably added way too much code. I highlighted the code i added.

#include <Servo.h>

Servo myservo; Servo myservo2; // creates servo objects to control

int pos = 0; int pos2 = 180; // variables to store the servo position

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo2.attach(10); // attaches servo to pin 10
}

void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
for(pos2 = 180; pos2 < 0; pos2 +=1)
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable ‘pos’
myservo2.write(pos2);
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
for(pos2 = 0; pos2 > 1; pos2 -=1);
{
myservo.write(pos); // tell servo to go to position in variable ‘pos’
myservo2.write(pos2);
delay(15); // waits 15ms for the servo to reach the position
}
}

You don't need any more for loops. Use PaulS's hint:

myservo2.write(180 - pos);

When I said four, I really did mean it. One to declare the new object, one to attach it and two to write to it

Man I was really over thinking is. Got is working with myservo2.write(180 - pos); which I assume reads 180 degrees subtracted form pos, which was defined as 0? Why is it subtracting 180 degrees and not adding?

In the first for loop, pos starts at 0 and goes up to 180, so subtracting pos from 180 means your second servo moves from 180 (180 - 0) to 0 (180 - 180).

Why is it subtracting 180 degrees and not adding?

180 - 0 = 180 180 - 1 = 179 180 - 2 = 178 . . . 180 - 178 = 2 180 - 179 = 1 180 - 180 = 0

Strictly speaking, the loops don't go 0 to 180 or 180 to 0, but close enough for government work ;)

I know that, but was trying to keep it simple.

Okay. I think I understand the 180 - concept now, but I want to make sure I understand the for control structure correctly.

for(pos = 0; pos < 180; pos += 1)

In simple terms is this how you could explain the code above:

pos = 0 is defining the position in degrees of pos

pos < 180 is testing if the pos is less than 180 degrees, and if it is then…

pos += 1 tells it to move in increments of 1 degree to 180

That's about right, specific to your code. Look in the reference section at the 'for' structure.

The general case is given as:

for (initialization; condition; increment) {

//statement(s);

}

and explained as:

The initialization happens first and exactly once. Each time through the loop, the condition is tested; if it's true, the statement block, and the increment is executed, then the condition is tested again. When the condition becomes false, the loop ends.

Not quite, think of it like this

pos = 0;
while (pos < 180) {
  // do the body of the loop
 pos = pos + 1;
}

I don’t want to sound rude, but I don’t understand this explaination at all.
pos = 0;
while (pos < 180) {
// do the body of the loop
pos = pos + 1;
}

Also, why is the increment portion written as pos += 1 and not simply pos + 1

The explanation is exactly equivalent to the first "for" loop.

pos + 1;

Adds one to pos, then does nothing with the result (nominally, it tests the value if the result, but that is discarded anyway). pos++; increments pos, as does pos += 1;, as does pos = pos + 1;

So pos + 1 would just move 1 degree and stop?

And pos++; pos +=1; and pos =pos + 1 all do the same thing?

No, pos + 1 would just cause the for loop to continue for ever with pos set to zero.

As for the other question, yes, they're equivalent for the example given. There is a subtle difference between pos++ and ++pos, but as the increment in a for loop, they're equivalent.

So pos + 1 would actually make pos = 1 instead of pos = 0 as defined earlier in the code?

No, pos + 1 would make pos have the same value it started with. There is no assignment, either explicit or implicit.