Hi, I am trying to rotate a NEMA 23 stepper motor using TB6560 driver and Arduino Mega 2560 using the code given below. The stepper motor is supposed to rotate randomly changing from clockwise to anticlockwise after every 30s.
The problem is 1) motor is rotating only once and waits until 30s are over without the for loop. 2) doesn't rotate at all when I added the for loop.
Please help, I don't know where I,m wrong
suzi1910:
Hi, I am trying to rotate a NEMA 23 stepper motor using TB6560 driver and Arduino Mega 2560 using the code given below. The stepper motor is supposed to rotate randomly changing from clockwise to anticlockwise after every 30s.
The problem is 1) motor is rotating only once and waits until 30s are over without the for loop. 2) doesn't rotate at all when I added the for loop.
Please help, I don't know where I,m wrong
const long interval = 30000;
int stepsize;
int thisTime;
int total;
int i;
void setup()
{
Serial.begin(9600);
Serial.println("start");
}
void loop()
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval || previousMillis==0)
{
if (stepper.distanceToGo() >= 0 || stepper.distanceToGo() <= 0)
{
stepsize = 6400;
thisTime = randomWalk(stepsize); // call function
total += thisTime; // aggregate values to see if function is really random
stepper.moveTo(total);
stepper.setSpeed(106.6777);
stepper.setAcceleration(1.7778);
previousMillis = currentMillis;
//function definition
int randomWalk(int moveSize)
{
static int place;
place = (random(0,2));
if (place == 1){
place = place * moveSize;
}
else if(place == 0){
place = place - moveSize;
}
return place;
}
if (stepper.distanceToGo() >= 0 || stepper.distanceToGo() <= 0)
How can that ever be false?
Did you know that the run() function returns true as long as the motor is moving to the commanded (target) position and false (0) when the position is reached (motor stops)?
From the AccelStepper library reference:
Poll the motor and step it if a step is due, implementing accelerations and decelerations to achieve the target position. You must call this as frequently as possible, but at least once per minimum step time interval, preferably in your main loop. Note that each call to run() will make at most one step, and then only when a step is due, based on the current speed and the time since the last step.
Returns
true if the motor is still running to the target position.
if (currentMillis - previousMillis >= interval || previousMillis == 0)
Oh I got my mistake in if distanceToGo() condition. But I don't want the motor to reach a particular position, I want it to keep rotating at 2rpm until 30s are over, after which it has to randomly change the direction of rotation. [the motor is set to 1/16 microstepping]
if (currentMillis - previousMillis >= interval || previousMillis == 0)
I added previousMillis = 0 in if condition, as the loop was not starting until the interval time, is reached.
suzi1910:
Oh I got my mistake in if distanceToGo() condition. But I don't want the motor to reach a particular position, I want it to keep rotating at 2rpm until 30s are over, after which it has to randomly change the direction of rotation. [the motor is set to 1/16 microstepping]
if (currentMillis - previousMillis >= interval || previousMillis == 0)
I added previousMillis = 0 in if condition, as the loop was not starting until the interval time, is reached.
How can the motor change directions randomly if there are only two possible direction?
Paul
Paul_KD7HB:
How can the motor change directions randomly if there are only two possible direction?
Paul
The motor is supposed to change direction clockwise to Counterclockwise or vice versa or remain the same after every 30s following no particular pattern, hence the randomness.
To test the code I used an Uno with a CNC shield using DRV8825 stepper drivers. The shield pulls the driver's enable pin (pin 8) HIGH, disabling the drivers by default. So l have to set the enable LOW to be able to use the motors.