# Problem with Stepper Motor For Loop: Help with Sweep directions in Pitch and Yaw

Hi,

I am currently working on coding 2 stepper motors to sweep in an X direction and Y direction in order to process and scan a slope using a LiDAR scanner. I would like to program to make the X (Yaw) stepper motor rotate 120 degrees. After which programming the Y stepper (Pitch) to move up 5 degrees. After this I want the X stepper to return from its previous 120 degree rotation back to the start position before repeating the process in 5 degree pitch increments until reaching 45 degrees in total, moving up and sweeping 120 degrees at a time in Yaw increments.

Here is the code I am currently working on and having troubles with:

``````/* Sweep Lidar Scanner
*  Lidar Lite sensor sending cm distance while servos sweep
* Dincer Hepguler 2015
* http://borsaci06.com
*/

#include <Stepper.h>
//#include <I2C.h>
//#define    RegisterMeasure     0x00          // Register to write to initiate ranging.
//#define    MeasureValue        0x04          // Value to initiate ranging.
//#define    RegisterHighLowB    0x8f          // Register to get both High and Low bytes in 1 call.
// Number of steps per output rotation
const int stepsPerRevolution = 200;

Stepper myXstepper(stepsPerRevolution, 4, 5, 6, 7);  // create stepper object to control X stepper
Stepper myYstepper(stepsPerRevolution, 8, 9, 10, 11);  // create stepper object to control Y stepper

int posX = 0;    // variable to store X servo position
int posY = 0;    // variable to store Y servo position

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
myXstepper.setSpeed(60);
myYstepper.setSpeed(60);
}

void loop() {
// put your main code here, to run repeatedly:

for(posX = -4167; posX <= 167; posX += 167) // goes from 30 degrees to 150 degrees
{
for(posY = 111; posY <= 444; posY += 111){  //Y axis sweep
myXstepper.step(posX);              // tell servo to go to position in variable 'pos'
myYstepper.step(posY);
//delay(1);                       // waits for the servo to reach the position

}

for(posY = 111; posY >=999; posY -=111){ //Y axis sweep
myXstepper.step(posX);              // tell servo to go to position in variable 'pos'
myYstepper.step(posY);
//delay(1);                       // waits for the servo to reach the position

}

//Stepper myXstepper(stepsPerRevolution, 7, 6, 5, 4);    // Attempt to swtich the excitement of the h-bridge and stepper to swap direction of sweeping motion but doesnt work
for(posX = 167; posX >=4167; posX = 167)     // goes from 150 degrees to 30 degrees
{
for(posY = 3333; posY <= 7778; posY += 111){     //Y axis sweep
Stepper myXstepper(stepsPerRevolution, 7, 6, 5, 4);
myXstepper.step(posX);              // tell servo to go to position in variable 'pos'
myYstepper.step(posY);
//delay(1);                       // waits for the servo to reach the position

}
for(posY = 7778; posY >=3333; posY -=111){     //Y axis sweep
myXstepper.step(posX);              // tell servo to go to position in variable 'pos'
myYstepper.step(posY);
//delay(1);                       // waits for the servo to reach the position

}
}
}
//Stepper myXstepper(stepsPerRevolution, 7, 6, 5, 4);    // Attempt to swtich the excitement of the h-bridge and stepper to swap direction of sweeping motion but doesnt work
for(posX = 1000; posX >=1111; posX = 167)     // goes from 150 degrees to 30 degrees
{
for(posY = 3333; posY <= 7778; posY += 111){     //Y axis sweep
Stepper myXstepper(stepsPerRevolution, 7, 6, 5, 4);
myXstepper.step(posX);              // tell servo to go to position in variable 'pos'
myYstepper.step(posY);
//delay(1);                       // waits for the servo to reach the position

}
for(posY = 7778; posY >=3333; posY -=111){     //Y axis sweep
myXstepper.step(posX);              // tell servo to go to position in variable 'pos'
myYstepper.step(posY);
//delay(1);                       // waits for the servo to reach the position

}
}

myXstepper.step(5000);
myYstepper.step(5000);
delay(25);
}
``````

and having troubles with:

Could you be more descriptive. That really conveys no information that we can use to help you.

The code does something. What does it do? How is that different from what you want?

Karma for code tags in first post.

james_emett:
I would like to program to make the X (Yaw) stepper motor rotate 120 degrees. After which programming the Y stepper (Pitch) to move up 5 degrees. After this I want the X stepper to return from its previous 120 degree rotation back to the start position before repeating the process in 5 degree pitch increments until reaching 45 degrees in total, moving up and sweeping 120 degrees at a time in Yaw increments.

Make two separate functions, one which moves the X motor 120 degrees and another that moves the Y motor 5 degrees. Then your code in loop() could be something like this

``````void loop() {
moveXstepper();
if (yAngle < 45) {
moveYstepper();
yAngle += 5;
}
}
``````

The code in the moveXstepper() function can choose the direction depending on where the motor last stopped - something like

``````void moveXstepper() {
if (xPosition >= 7778) {
// code to move back to 3333
}
else {
// code to move to 7778
}
}
``````

Dividing code into separate single purpose functions makes it much easier to manage.

You tell the stepper library that your steppers are 200 steps per revolution that would make 5° equal to 2.777... steps. But you then use 167 steps for 5° on one axis and 111 steps for 5° on the other axis.

``````   for(posY = 111; posY >=999; posY -=111){ //Y axis sweep
``````

This loop does nothing because when you start posY at 111 it is already less than 999.

I would count by integers (instead of degrees or steps) and then :

``````const int yEndDegrees = 45;
const int yDegreeInterval = 5;
const int yStepsPerDegree = 167;

const int xEndDegrees = 120;
const int xDegreeInterval = 5;
const int xStepsPerDegree = 111;

// Scan from bottom to top
for (int y = 0; y < yEndDegrees/yDegreeInterval; y++)
{
int yDegrees = y * yDegreeInterval;
int ySteps = yDegrees * yStepsPerDegree;

yStepper.moveTo(ySteps);

// Scan left to right
for (int x = 0; x < xEndDegrees/xDegreeInterval; x++)
{
int xDegrees = x * xDegreeInterval;
int xSteps = xDegrees * xStepsPerDegree;

xStepper.moveTo(xSteps);

// Wait for both motions to complete

}
}
``````

Hi John

Thanks for the reply and help. A question I have is what Library are you using for the code you put in the comment. The .moveTo function is not recognised with the <stepper.h> Library.

Many Thanks
Jimmy.

james_emett:
Thanks for the reply and help. A question I have is what Library are you using for the code you put in the comment. The .moveTo function is not recognised with the <stepper.h> Library.

That is from the very capable AccelStepper library. It avoids jerking by ramping the speed up and down at the starts and ends of moves. It can do absolute and relative moves.
For maximum speed, I would suggest scanning alternating left-to-right and right-to-left. That way you don't have to wait for the long horizontal moves when each line is scanned left-to-right.