Stepper motor coding help

so i do railway modelling, and im trying to make a functional bridge
i've got my stepper motor to rotate constantly clockwise, but how could i make it:

-go clockwise
-stop for a certain amount of time
-go anti-clockwise
-repeat after a certain amount of time

Timing is usually done with the millis(), micros() or delay() functions.

Please post your code, and don't forget the code tags.

Tell us a bit more of your stepper.

I like the MobaTools library. There are classes for stepper motors (MoToStepper) and also for timers (MoToTimer). See the documentation and examples to see how to use the library and those classes.

Here are some tutorials on the basics of using millis() (and micros() for timing.
Blink without delay().
Beginner's guide to millis().
Several things at a time.

this is the code currently

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
  // set the speed at 60 rpm:
  myStepper.setSpeed(0);
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {
  // step one revolution  in one direction:
  Serial.println("clockwise");
  myStepper.step(-stepsPerRevolution);
  delay(10);

  // step one revolution in the other direction:
  Serial.println("clockwise");
  myStepper.step(-stepsPerRevolution);
  delay(10);
}

Wouldn't you just change the sign on

-stepsPerRevolution

so it's positive?

i was messing around with that but it didn't seem to be changing anything

You should mention any messing around you've done, so we don't waste time asking.

the only messing about i've done but since changed back was trying to change direction, and with the rpm

But we don't know what exactly you did, or the precise results of that.

rpm was just seeing what speeds would be the best for what i am planning to use the stepper motor for, and trying to change direction didn't get anywhere, always is going clockwise

Please try

  myStepper.step(stepsPerRevolution);

and report back. Change only one, not both.

alright, just tried it - still going clockwise

Post the code. Also try library example sketches. There must be one that alternates direction.

Also, you never replied to #4.

i dont know much about the stepper as i didn't order it myself; the only infomation i can give is one the sticker: ROHS 28BYJ-48 5VDC 15031801

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
  // set the speed at 60 rpm:
  myStepper.setSpeed(0);
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {
  // step one revolution  in one direction:
  Serial.println("clockwise");
  myStepper.step(-stepsPerRevolution);
  delay(10);

  // step one revolution in the other direction:
  Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);
  delay(10);
}

Can't you post an image? Also please post a wiring diagram.

How often are the serial debug prints coming out? Please change one so they aren't identical.

i don’t have a diagram for what I’ve done, but I do have pictures




myStepper.setSpeed(0);

That stops the motor.

I have that same stepper motor and driver. The steps per revolution of the output shaft is 2038 steps due to the gear reduction.

Wiring
in1 >> pin 8
in2 >> pin 10
in3 >> pin 9
in4 >> pin 11

I modified your code a bit and it works with my stepper to go one revolution CW, a pause, one revolution CCW, a pause and so on.

#include <Stepper.h>

const int stepsPerRevolution = 2032;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup()
{
   // initialize the serial port:
   Serial.begin(9600);
   Serial.println("starting stepper bounce");

   // set the speed at 10 rpm:
   myStepper.setSpeed(10);  // fastest that I have seen is 15 RPM.

}

void loop()
{
   // step one revolution  in one direction:
   Serial.println("clockwise");
   myStepper.step(stepsPerRevolution);
   delay(1000);

   // step one revolution in the other direction:
   Serial.println("counter clockwise");
   myStepper.step(-stepsPerRevolution);
   delay(1000);
}

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