Rotate stepper motor randomly

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

BlinkWithoutDelay_-_random_motor_2.ino (1.26 KB)

More members will see your code if you post the code as described in the forum guidelines.

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

#include <AccelStepper.h>

#define STEPPER1_DIR_PIN 2
#define STEPPER1_STEP_PIN 3

AccelStepper stepper(AccelStepper::DRIVER, STEPPER1_STEP_PIN, STEPPER1_DIR_PIN);

unsigned long previousMillis = 0;

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;
   
    Serial.println("after 10s");
  }
  }
 
  //stepper.move(20);
  stepper.run();
}

//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)

When will previousMillis ever equal 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.

Something like this?

#include <AccelStepper.h>

const byte stepPin = 2;
const byte dirPin = 5;
const byte enablePin = 8; // for my CNC shield

int position = 10000;

AccelStepper stepper(AccelStepper::DRIVER, 2, 5);

void setup()
{
   Serial.begin(115200);
   pinMode(enablePin, OUTPUT);
   digitalWrite(enablePin, LOW);
   stepper.setMaxSpeed(200);
   stepper.setAcceleration(100);
}

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 30000;
   if (millis() - timer >= interval)
   {
      timer = millis();
      int sign = random(0, 2);
      if (sign == 0)
      {
         sign = -1;
      }
      position = 10000 * sign;
      stepper.moveTo(position);
      Serial.println(position);
   }   
   stepper.run();
}

Yes, Thank you so much, just one doubt, why is enable pin needed?

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.

OK thanks a lot that helped.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.