Increasing a Loop value

I’m not a top notch programmer here and I’m getting stuck on an issue.

I have a small servo x-y turret set up (up/down and left to right). It’s for a photo-stitching project that I’m working on. I want to make it run through 0-180 degrees, on the x axis, then increase the y axis by 5 degrees and repeat the loop. At least, I think I need a loop. Here’s what I’ve got so far and I’ve tried a bunch of things (based on the sweep code):

// Sweep
// by BARRAGAN <http://barraganstudio.com>
// This example code is in the public domain.
 

#include <Servo.h>
 
Servo myservo1;  // create servo object to control a servo
Servo myservo2;                // a maximum of eight servo objects can be created
 
int pos1 = 0;    // variable to store the servo position
int pos2 = 0;

void setup()
{
  myservo1.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo1.write(0); 
  myservo2.attach(10);
  myservo2.write(0);// sets the servo position according to the scaled value 
 delay(500);

}



 
void loop()
{
  for(pos1 = 0; pos1 < 180; pos1 += 5) // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    myservo1.write(pos1);     // tell servo to go to position in variable 'pos'
    delay(150);
    myservo2.write(++pos2);
  }
     
}

I’ve tried setting myservo2.write to pos1, but that moves the servos together at the same time (obviously). In some of the switching around I’ve done I’ve got the turret to adjust to a value on the y axis but it always resets as the loop does. I’ve tried to make pos2 a different variable but it always resets to 0, despite this. I’ve tried a nested loop and using the pos2 on the same loop, but they make the myservo2 increase as myservo1 does-which is not what I want-I need it to increase at the end of the loop to grab a different line of the variable.

I’m just wondering, if I shouldn’t use a loop here, what should I use?

thanks,

hadwahnos

You should use two loops nested, on controls the X movement and the other the Y.
This is not real code but you should get the idea about how to nest loops:-

for(int y = 0; y<90; y++) { // outer loop
// move the y servo
   for (int x = 0; x <90 ; x++){ // inner loop
       // move the x servo
   } // end of x loop
} // end of y loop

It’s not clear what you want. Do you want your turret to turn through 180o then return to 0o, raise the angle by 5o and sweep again (out, back, up, out, back, up) or do you want it to turn through 180o then raise and do a reverse sweep (out, up, back, up, out, up, back, up)?

EDIT: Obviously, at the end of your routine, you’ll need to return to 0o in both axes, ready for the next time.

I tried the nested loop, but it moved both servos at the same time.

Sorry if it was unclear-I want it to run the full 180 degrees, return to 0, change the y axis by a few degrees and then run the x axis 180 again. The y axis should be still while the x axis is moving. It would only change after it's done the full 180, then move again.

Thanks for the replies

I didn’t test this, but it should give you an idea…

int yTarget ;      // Assume 18 passes in Y direction
int x;
// setup()

void loop()
{
   while (yTarget < 90) {
      for (x = 0;x < 180; x++) {
         myservo1.write(yTarget);     // tell servo to go to position in variable 'pos'
         delay(150);
         myservo2.write(++x);
      }
      yTarget += 5;
   }
}

Then you’ll want something like this:

void loop(){
  for(elevation =0; elevation<40;elevation+=5){  //raise elevation from 0
                  // to 40 degrees in steps of 5 degrees
      for(pos1 = 0; pos1 < 180; pos1 += 5) {// goes from 0 degrees to 180 degrees
                             // in steps of 5 degrees
          myservo1.write(pos1); // tell servo to go to position in variable 'pos'
          delay(150);
       }
       myservo1.write(0);     // tell servo to go back to 0
       delay(150);   // do you need this delay?
       myservo2.write(elevation);
  }
}

I tried the nested loop, but it moved both servos at the same time.

Then you didn't do it right. You need a nested loop for what you want.

Thanks for the responses guys, I really appreciate the help.

Econjack-that’s similar to what I had before-the problem is that it doe not move the y axis. For some reason the y axis remains the same and it just moves the x axis.

Henry_Best-for some reason, this doesn’t work much at all. It just freezes everything, but I think this is closer. I’ll keep playing with variables

Grumpy_Mike-they don’t call you grumpy for nothing, I guess (playful sarcasm).

Anyway, thanks for the tips guys. I’ll keep trying with the help I have and if anyone else has any more tips, I’d appreciate them.

thanks

hadwahnos:
Thanks for the responses guys, I really appreciate the help.
Henry_Best-for some reason, this doesn’t work much at all. It just freezes everything, but I think this is closer. I’ll keep playing with variables

Sorry. My error. Change elevation >40 to elevation < 40 :blush:

Grumpy_Mike-they don't call you grumpy for nothing,

They don't call me Grumpy Mike .... I call me Grumpy Mike. :)

You see you are heading towards a nested loop soloution, those last two codes were both nested loops. Henry's has two nested for loops and jack's was a for loop nested inside a while loop. Exactly what I showed in reply #1.

Just to let anyone else looking for this know:

The code I originally wrote was correct, as were the examples donated by the others on the thread (as far as I tested anyway and thanks for the help). The problem lied in the servos-the arduino didn’t have enough power to run both servos in the loop-at the end of the loop the power would cause it to reset. Using a separate power supply allowed the loop to run and advance the y axis and it now works as it’s supposed to. I figured the arduino could power 2 small servos but go figure.

My dad is a programmer and while I wanted to figure this one out on my own, he came along and added some outputs to troubleshoot where the loop stopped working.

The code is here, if anyone wants to recreate the circuit/project, I’ll be doing a write up soon on a separate website. In the meantime, here’s the code:

#include <Servo.h>
 
Servo myservoX;  // create servo object to control a servo
Servo myservoY;                // a maximum of eight servo objects can be created

int y;      // Assume 18 passes in Y direction
int x;

void setup()
{
  Serial.begin(9600);
  myservoY.attach(10);  // attaches the servo on pin 9 to the servo object
  myservoY.write(0); 
  myservoX.attach(9);
  myservoX.write(0);// sets the servo position according to the scaled value 
 delay(500);
  Serial.println( "Reset" );
}


void loop()
{
   for ( y = 30; y < 150; y+=20 ) {
      for ( x = 0; x < 180; x++ ) {
         myservoX.write(x);     // tell servo to go to position in variable 'pos'
         delay(50);
      }
      myservoY.write(y);
      Serial.print( "y is " );
      Serial.println( y );
   }
   Serial.println( "End of loop" );
   /*
   y = 10;
   x = 0;
   myservo1.write(x);
   myservo2.write(y);
   */
}

Thanks,

hadwahnos