I am new to arduino and I am having problems with the movement of two stepper motors.
I am trying to control two stepper motors with the help of two pololu drivers dvr8825 and an arduino uno.
To do this I have a circuit board with the drivers and the connections for the motors that plug into the arduino board. I have already checked that the circuit is correct but the code is my problem.
I have also a platform with the two motors. Motor1 rotates on a vertical plane and Motor2 rotates in an horizontal plane. For each 1º that the vertical plane rotates I need to rotate -60º to 60º with the other motor in an horizontal plane.
Each motor does rotate, however just with the first setup of putting the Motor2 at -60º (as it is my starting point) I have problems.
->Motor2 doesn't stop at -60º it keeps going until I unplug it.
->Each time I connect the board to the computer it starts running even before I upload the program.
I am using also the CoolTerm software for mac to see the serial.println I have put in the code to be able to find the problem. However, I don't know if I am using it correctly but it doesn't work. Is there any other tool I could use to see the serial.print???
I have searched throught the internet for days now and I have tried everything I have found and everything I have come up to without any luck...
I appreciate all answers and any help I can get.
Thanks in advance!
AliciaPae
PD: I am attaching my arduino code and other pictures that might help.
and the code from the OP so others don't have to download it
#include <AccelStepper.h>
AccelStepper stepper1(1, 4, 5); //(mode DRIVER, pin4 step, pin5 dir) (motor1 inside, connection closer to power, steps 1.8)
AccelStepper stepper2(1, 2, 3); //(mode DRIVER, pin2 step, pin3 dir) (motor2 outside, with wheels, connection farther from power, steps 0.6)
const float stp1 = 1.8; //motor1 unside
const int range1 = 32;
const float micro_steps1 = 1 / (stp1 / range1); // 18 steps to do 1º
const float stp2 = 0.6; //motor2 outside
const int range2 = 32;
const float micro_steps2 = 1 / (stp2 / range2); //53 steps to do 1º
int i = 0; // position motor2 outside, 121º( it is +-60º going through 0º)
int k = 0; // position motor1 inside, 180º
int j = -61; //starting position of motor2
int pos1 = 0, pos2 = 0;
//This code moves each time 1º the inside motor1 and 121º the outside motor
//starting point motor1=0º, motor2=-60º
//moves 1º inside, moves 1º outside, stops(do some measures), moves 1º outside, stops(do some measures).... 121º, moves inside 1º
void setup()
{
Serial.begin(9600); //it conects throught the serial port from board to computer
stepper1.setMaxSpeed(50);
stepper1.setAcceleration(5000);
stepper1.setCurrentPosition(0);
stepper2.setMaxSpeed(50);
stepper2.setAcceleration(5000);
stepper2.setCurrentPosition(0);
}
void loop()
{
while (k < 180) {
//starting point motor1: sets the position of inside motor to 0ª
pos1 = round(micro_steps1 * k); //measures pos1
if (stepper1.distanceToGo() == 0)//distance from position actual to the destination
{
delay(1000);
pos1 = - pos1;
stepper1.moveTo(pos1);//fixes destination position
}
stepper1.runToPosition(); //moves until destination position is reached and blocs
delay(500);
Serial.println("Inside motor has moved");
i = 0;
while (i < 121) {
//starting point motor2: position of outside motor to -60º
if (i==0){
j=-61;
while (j<0){
Serial.println(j);
pos2 = round(micro_steps2 * j);
if (stepper2.distanceToGo() == 0)
{
delay(100);
pos2 = - pos2;
stepper2.moveTo(pos2);
stepper2.setSpeed(50);
}
//stepper2.moveTo(pos2);
stepper2.runToPosition();
Serial.println(j);
j++;
}
//after starting point, it moves 1º each time
pos2 = round(micro_steps2 * (i));
if (stepper2.distanceToGo() == 0)
{
delay(1000);
pos2 = - pos2;
stepper2.moveTo(pos2);
}
stepper2.runToPosition();
delay(500);
Serial.println("outside motor has moved");
i++;
}
k++;
}
}
}
Your pictures are not very informative. A photo of a pencil drawing of all the connections is the best way to convey that information.
I suspect your problem is a combination of the blocking stepper.runToPosition() (rather than the non-blocking stepper.run() ) and the way you have two WHILEs inside each other. In general don't use WHILE because that also a blocks other activity until it finishes. Use IF and allow loop() to do the iterations.
Thank you! I have tried the code with if instead of while as you suggested and it works.
However, now I have a problem with the direction of the rotation. Even if I change the sign of the position (I am using the AccelStepper library) which should change the direction from clockwise to anticlockwise the motors don't change direction and continue rotating as if nothing was changed.
I have tried also using the Stepper library from arduino and all the examples from it and still nothing.
You need to post the latest version of your program so we can see what you can see.
As far as getting AccelStepper to move a motor in either direction I suggest you write a short program with nothing else in it until you have mastered the technique. I suspect the AccelStepper website has examples.
I have tried all of the examples in the webpage and others as well and nothing works. I have checked that the motors I am using are bipolar and all of the connections work as well.
I have done a program that only moves 1 motor and I tried changing the direction and still didn't work.
I don't know what else to try so any suggestions are welcome!
The AccelStepper library says the following about the position:
/// Set the target position. The run() function will try to move the motor
/// from the current position to the target position set by the most
/// recent call to this function.
/// \param[in] absolute The desired absolute position. Negative is
/// anticlockwise from the 0 position.
void moveTo(long absolute);
/// Set the target position relative to the current position
/// \param[in] relative The desired position relative to the current position. Negative is
/// anticlockwise from the current position.
void move(long relative);