Stepper motor, controlling the speed

hey guys. I really need help with my code.
I am trying to control the speed of the first code just like the second one. I was wondering if you guys have a suggestion. This is my first time using Arduino, I am a little bit lost
Thank you

// Define pins
 
int reverseSwitch = 2;  // Push button for reverse
int driverPUL = 7;    // PUL- pin
int driverDIR = 6;    // DIR- pin
int spd = A0;     // Potentiometer
 
// Variables
 
int pd = 500;       // Pulse Delay period
boolean setdir = LOW; // Set Direction
 
// Interrupt Handler
 
void revmotor (){
 
  setdir = !setdir;
  
}
 
 
void setup() {
 
  pinMode (driverPUL, OUTPUT);
  pinMode (driverDIR, OUTPUT);
  attachInterrupt(digitalPinToInterrupt(reverseSwitch), revmotor, FALLING);
  
}
 
void loop() {
  
    pd = map((analogRead(spd)),0,1023,2000,50);
    digitalWrite(driverDIR,setdir);
    digitalWrite(driverPUL,HIGH);
    delayMicroseconds(pd);
    digitalWrite(driverPUL,LOW);
    delayMicroseconds(pd);
 
}

Into this code:

#include <Stepper.h>

const int stepsPerRevolution = 360;  // change this to fit the number of steps per revolution
int t = 0;
int q = 0;
int r = 0;

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

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

void loop() {

  while(q < 5) {
    // step one revolution  in one direction:
    Serial.println("counterclockwise(push)");
    myStepper.step(-75);
   // delay(500);
  
  //  step one revolution in the other direction:
   Serial.println("counterclockwise(relese)");
   myStepper.step(75);
    delay(1000);
    q++;
  }
  while(r < 5) {
  // step one revolution  in one direction:
  Serial.println("counterclockwise(push)");
  myStepper.step(-50);
  delay(10);

//  step one revolution in the other direction:
 Serial.println("counterclockwise(relese)");
 myStepper.step(50);
  delay(10);
  r++;
}
  while(t < 5) {
    // step one revolution  in one direction:
    Serial.println("counterclockwise(push)");
    myStepper.step(-100);
   // delay(500);
  
  //  step one revolution in the other direction:
   Serial.println("counterclockwise(relese)");
   myStepper.step(100);
    delay(1000);
    t++;
  }
}

Please edit your post to add code tags. See the "How to get the best out of the forum" post.

Also, explain what you mean by

My project is to be able to change the first code into the second

Please read the first topics telling how to get the best from this forum.
We want to help You with advice for the code and electronics but helping You to write an understandable question.... No.

The first code is for a step/dir driver and the second is for a 4 wire driver. What driver are you using?

Hi @anon69574797 ,
is this a school project?

RV mineirin

No, It is not a school project

I am using Stepper Motor Driver TB6600. The first code, which is the one I have to make the adjustments.
The second code is using ULN2003 Stepper Motor Driver, I was told that my code should function in the same way as the second code.

I apologize for the miswritten question and formatting. I have just joined this platform and I was trying to learn how to use it. I honestly did not expect a fast reply.
Thank you for the advice.

Let me rephrase that question. What driver do you want to use? I highly recommend the TB6600 driver for a modern bipolar stepper as it has coil current control.

Please describe, in detail, what your code is to do.

I want to use the TB6600 driver. I want to control the speed that my motor should rotate. I used a potentiometer but, my supervisor said that I should change my code(the first one) to function as the second code, without using a potentiometer.

What can't you use the second code? All you have to do is declare the stepper with two pins instead of four.

In the first code the speed of the stepper is controlled by the value of the pd variable, as set by the pot, in the delayMicroseconds() functions. In the second code you are using the Stepper library setSpeed() function to control speed. Are you supposed to use the Stepper library or not?

In the first code, to vary the speed, vary the value of the pd variable.

The same message as @groundFungus

You can control speed (as opposed to position) using my Stepper Speed Control Library for Arduino which is a modification of the AccelStepper library

My Multi-tasking in Arduino tutorial has a complete stepper example as well showing you how to keep stepper running fast.

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