How to control speed and forward/beckward direction

Dear Forum members,
I would like to ask you for a help.
I need a code for Nema 34 Stepper motor, to do the following:

  1. move forward 180 degrees with one speed, and immediately move backward 180 degrees with
    another speed
  2. repeat 1) every 10 seconds
    Movement should be like car windshield wipers.

Big thanks in advance.

Hello octavia2009

Welcome to the worldbest Arduino forum ever.

Why don´t you use a servo for such a limited movements.

Have a nice day and enjoy coding in C++.

Please post the specifications of the motor, motor driver, motor power supply, and a wiring diagram.

Hint: stepper code libraries, like Accelstepper, come with examples, which you can run, and documentation, which you can read.

From all the information you have given I will recommend that one, it will meet all of your specifications.

How many steps is 180 degrees on your motor?
Which motor driver are you using?
How fast is the first sweep (0 to 180 in seconds? The second sweep (180 to 0)?

Assuming 200-step motors:

#include <AccelStepper.h>

// Define a stepper and the pins it will use
AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5

void setup()
{  
  // Change these to suit your stepper if you want
  stepper.setMaxSpeed(100);
  stepper.setAcceleration(50);
}

void loop()
{
  static unsigned long lastTime = 0
  if (millis() - lastTime >= 10000) // every 10 seconds
  {
    lastTime = millis();

    stepper.setSpeed(100); // 100 steps per second
    stepper.runToPosition(100); // 1/2 turn

    stepper.setSpeed(20);
    stepper.runToPosition(0);  //back to beginning.
  }
}

Dear johnwasser,
sorry for the late reply. The code you proposed is exactly what I am looking for (if I understood correctly). So, with this code, I can change the parameters in order to change the acceleration, speed, and number of steps. Perfect.
Big big thanks, and I whish you a nice weekend...
Also, following your posts in the future :slight_smile:

Dear JCA34F,
sorry for the late reply. Actually, it seems the code that @johnwasser proposed is exactly what I am looking for....what I need is actually option to control (and adjust) acceleration, speed, and number of steps of the stepper motor, when moving forward for 180 degrees, than stop, and going backwards again for 180 degrees.
Thanks a lot for advice, and I thing that the topic is also interesting for all of us....
Kind regards,

Dear gilshultz,
sorry for the late reply. Actually, it seems the code that @johnwasser proposed is exactly what I am looking for....what I need is actually option to control (and adjust) acceleration, speed, and number of steps of the stepper motor, when moving forward for 180 degrees, than stop, and going backwards again for 180 degrees.
Thanks a lot for advice, and I thing that the topic is also interesting for all of us....
Kind regards,

Dear jremington,
sorry for the late reply. Actually, it seems the code that @johnwasser proposed is exactly what I am looking for....what I need is actually option to control (and adjust) acceleration, speed, and number of steps of the stepper motor, when moving forward for 180 degrees, than stop, and going backwards again for 180 degrees.
Thanks a lot for advice, and I thing that the topic is also interesting for all of us....
Kind regards,

Dear paulpaulson,
sorry for the late reply. Actually, it seems the code that @johnwasser proposed is exactly what I am looking for....what I need is actually option to control (and adjust) acceleration, speed, and number of steps of the stepper motor, when moving forward for 180 degrees, than stop, and going backwards again for 180 degrees.
Thanks a lot for advice, and I thing that the topic is also interesting for all of us....
Kind regards,