Random Stepper Code

Hi Guys,

I've really hit a roadblock on this one. I have a NEMA17 stepper motor and an A4988 stepper motor driver. The goal is to program the stepper to move in a random motion left and right.

First, i need to know if i'm using the right stuff (the driver and 4wire motor)

I'm also using and arduino mega2650 if thats any help. I tried using other codes with the accelstepper library but the motor and driver used for that are different from mine.

The best way is to try one of the examples from the AccelStepper library. Get that working first.

Then you need a little more definition on what the random motion should do. If you flip a coin to move backwards or forward one step at a time then it will appear to be stationary because one step is very small.

I used the simple Stepper.h to move a stepper using a 4988 board.
Attach an old test code… You can read Arduino.reference to find out about the function Random.

#include <Stepper.h>

#define Low 0
#define High 1023
#define fwd HIGH
#define bwd LOW
#define motorX 1
#define motorY 2
#define motorZ 3

void setup()
{
// put your setup code here, to run once:
  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(7,OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(13, OUTPUT);
}

void stepmotor(int, int);

void stepmotor(int (motor), int(dir))
{
  int count, adir;
  if ( dir == fwd )
  {
    adir = High;
  }
  else
  {
    adir = Low;
  }
  for (count = 0; count < 400; count++)
  {
    if (motor == motorX )
    {
//      digitalWrite( 5, HIGH);//digital dir does not owerride analoWrite(5, dir)
      analogWrite( 5, adir);// x-dir KLAR High == CW Low == CCW
//      digitalWrite( 2, HIGH);// digital x-step
      analogWrite( 2, High);// analog x-step
//      digitalWrite( 2, LOW);// digital x-step
      analogWrite( 2, Low);// analog x-step
      delay(5);
    }
    if (motor == motorY)
    {
      digitalWrite( 6, dir);
      digitalWrite( 3, LOW);
      digitalWrite( 3, HIGH);
      delay(5);
    }
    if (motor == motorZ)
    {
      digitalWrite( 7, dir);
      digitalWrite( 4, LOW);
      digitalWrite( 4, HIGH);
      delay(5);
    }
  }
}
void loop() {
  // put you()r main code here, to run repeatedly:

//  analogWrite( 0, Low);//tst
//  analogWrite( 1, Low);//tst
//  analogWrite( 2, Low);// x-step analog KLAR
//  analogWrite( 3, Low);// y-step analog KLAR
//  analogWrite( 4, Low);// z-step analog¨KLAR
//  analogWrite( 5, Low);// x-dir  analog KLAR
//  digitalWrite(2, LOW);// x-step digitalt
//  digitalWrite(3, LOW);// y-step digitalt
//  digitalWrite(4, LOW);// z-step digitalt
//  digitalWrite(5,LOW); // x-dir digitalt
//  digitalWrite(6,HIGH);// y-dir KLAR HIGH == CW LOW == CCW
//  digitalWrite(7,HIGH);// z-dir KLAR HIGH == CW LOW == CCW KLAR
//  digitalWrite(8,LOW);//
//  digitalWrite(9,LOW);//
//  digitalWrite(10,LOW);//
//  digitalWrite(11,LOW);//
//  digitalWrite(12,LOW);
  digitalWrite(8, LOW);//enable motorpower
  stepmotor( motorX, fwd );
//  pulse (motorY, fwd );
//  pulse (motorZ, fwd );
  digitalWrite(8, HIGH);//disable motorpower
  digitalWrite(13, LOW);
  delay(1000);

//  analogWrite( 0, High);//
//  analogWrite( 1, High);//
//  analogWrite( 2, High);//x-channel KLAR
//  analogWrite( 3, High);//y-channel KLAR
//  analogWrite( 4, High);//z-channel KLAR
//  analogWrite( 5, High);//x-dir     KLAR
//  digitalWrite(2, HIGH); //x-step!!!
//  digitalWrite(3, HIGH); //y-step!!!
//  digitalWrite(4, HIGH); //z-step!!!
//  digitalWrite(5, HIGH); //X-DIR CW ON HIGH CCW ON LOW
//  digitalWrite(6, HIGH); //Y-DIR CW ON HIGH CCW ON LOW KLAR
//  digitalWrite(7, LOW); //Z-DIR CW ON HIGH, CCW ON LOW KLAR
//  digitalWrite(8,HIGH);//
//  digitalWrite(9,HIGH);//
//  digitalWrite(10,HIGH);//
//  digitalWrite(11,HIGH);//
//  digitalWrite(12,HIGH);//
  digitalWrite(8, LOW);//enable motorpower
  stepmotor( motorX, bwd );// går finfint +200, -200 steg
  digitalWrite(8, HIGH);//disable motorpower
//  pulse (motorY, bwd );
//  pulse (motorZ, bwd );
  digitalWrite(13,HIGH);//

  delay(1000);

}

The standard Stepper library is not designed for a driver that takes step and direction signals. Either use the AccelStepper library or just write your own code, its not difficult for an A4988.

...R
Stepper Motor Basics
Simple Stepper Code

AccelStepper library

Thank guys, i ran into an error because i accidentally selected the wrong board. fixed that. random stepper code from accelstepper library works fine.

Now can I connect the stepper motor to an A4988 stepper and get similar results