 # X and Y integer loops

Hello,

Apologies if this is fairly simple, but I am trying to make sure I have my foundations right for a project I am working on which basically pans and tilts a camera to take images along the X and Y axis to later be combined into a gigapan style image.

So I calculate the xMax and xMin positions, as well as the yMax and yMin positions to end up with a yTotal and xTotal (the total degrees across the respective axis)

From there, I also calculate the number of degrees the given camera and lens (e.g. focal length) will cover accounting for some overlap.

Knowing the number of degrees the lens covers, I can do a simple yTotal / yLensDegrees to work out how many images I need to capture along the Y axis which is fine.

Say I need to capture 3 images across the Y axis, and 10 images along the X axis and I know the lens covers 20 degrees along the X, and 15 degrees along the Y what is the best way to loop these given I need to give absolute commands to the servo (e.g. move to position 45 degrees along x).

I thought about doing an integer loop which would still work, but just not sure how to handle the increasing servo position, should I just do something like a simple integer loop but start at 1 instead of zero, and move finish at the endpoint+1, then I could just multiply the integer loop number by the number of degrees the lens covers and send that to the servo as an absolute position?

Just wondering if there is a better way to do it than that, or if not does anyone foresee any problems?

Many thanks!

I believe you are (close to) describing... https://en.wikipedia.org/wiki/Bresenham's_line_algorithm

If I understand you correct you just want to photograph a matrix of x * y photo’s? That’s just math… So you mean something like:

``````Servo servoX;
Servo servoY;

const byte StartX = ;
const byte StartY = ;

const byte StepSizeX = ;
const byte StepSizeY = ;

const byte StepsX = ;
const byte StepsY = ;

void setup(){
//attach servo etc
}

void loop(){
for(byte x = 0; x < StepsX; x++){
servoX.write(x * StepSizeX + StartX);

for(byte y = 0; y < StepsY; y++){
if(x % 2){
servoY.write((StepsY - y) * StepSizeY + StartY);
}
else{
servoY.write(y * StepSizeY + StartY);
}

//take photo
}
}
}
``````

This will zigzag in the Y direction while sweeping the X.

Code is not complete, just for you idea.

Thanks for your help Septillion, and Coding badly, it was not so much the zig zagging I need as I can go left to right, then up one, back to the far left, then continue going left to right, moving up a line at a time.

The code put me in the right direction though, I made a few minor modifications and added some debug so I could see what was happening when I used some servo’s.

Thanks for the help, most appreciated!

``````void loop(){
for(byte y = 0; y < Stepsy; y++){
servoy.write(y * StepSizey + Starty);

for(byte x = 0; x < Stepsx; x++){
servox.write(x * StepSizex + Startx);
Serial.print("y position:");
Serial.print(y);
Serial.print("(");
Serial.print(y * StepSizey + Starty);
Serial.print(")");
Serial.print(", x position:");
Serial.print(x);
Serial.print("(");
Serial.print(x * StepSizex + Startx);
Serial.println(")");
delay(1000);
//take photo
}
}
}
``````

Sorry, another quick one but related, with I use the code above I pan right to left then up. How do I go about changing the direction of the x loop, so I go the other direction.

I will need to change the StartX to FinishX and it becomes a subtraction and then of course define the const byte finishx = 75; for example, but I couldn't get the servo to change direction, it either did not move, or worse it was like it was being skipped and just the Y loop was going crazy :(

I used the zigzag because that seems to me like the quickest way / shortest travel and the shortest possible time between to adjoining photo’s.

But then:

``````void loop(){
for(byte y = 0; y < Stepsy; y++){
servoy.write(y * StepSizey + Starty);

for(byte x = 0; x < Stepsx; x++){
servox.write((StepsX - x) * StepSizex + Startx);

Serial.print("y position:");
Serial.print(y);
Serial.print("(");
Serial.print(y * StepSizey + Starty);
Serial.print(")");
Serial.print(", x position:");
Serial.print(x);
Serial.print("(");
Serial.print((StepsX - x) * StepSizex + Startx);
Serial.println(")");
delay(1000);
//take photo
}
}
}
``````

And StepsY, StepSizeY etc is more readable then Stepsy and StepSizey. More clear that you mean something with Y instead of the word stepsy, because what is a stepsy?

Works fantastically, good point about my variable capitalization :)