speed,time,cycle and direction stepper motor

Hello friends
I have a two-phase stepper motor that I want the user to be able to adjust the direction, speed, and timing, and also to adjust this step repeatedly, how many motors to stop and repeat the next cycle after each work cycle.
Time must be entered in hours, minutes and seconds.

Please help me I need this code a lot
No matter how much I searched I couldn't solve it while I'm a novice ...

Moved your topic to it's current location as it is more suitable.

Could you take a few moments to Learn How To Use The Forum.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

fatehan:
Please help me I need this code a lot
No matter how much I searched I couldn't solve it while I'm a novice ...

If you just want someone to write a program for you please ask in the Gigs and Collaborations section of the Forum and be prepared to pay.

If you want help with a program you have written then you obviously need to post the program and tell us in detail what happens when you run it and what you want it to do that is different.

Also post a link to the datasheet for your stepper motors and tell us what stepper motor drivers you are using and what stepper motor power supply you have (volts and amps).

...R
Stepper Motor Basics
Simple Stepper Code

thank you
my driver is TB6600
my motor is 2 phase and 200 step 1 amp
my code is this:

#include <Stepper.h>
#include <LiquidCrystal.h>//Library for LCD
LiquidCrystal lcd(9, 10, 11, 2, 3, 4, 5);
const int stepsPerRevolution = 1600; // change this to fit the number of steps per revolution
// for your motor
const int stepPin = 6; //PUL -Pulse
const int dirPin = 7; //DIR -Direction
const int enPin = 8; //ENA -Enable
int c = 3;
float timing = 2 ;//minute

float rpm = 300;
Stepper myStepper(stepsPerRevolution, 6,7);

void setup() {
// initialize the serial port:
Serial.begin(9600);
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(enPin,OUTPUT);
digitalWrite(enPin,LOW);
pinMode(dirPin,LOW);
lcd.begin(16, 2);//LCD type

lcd.setCursor(0,0);//setting LCD cursor and printing on it
lcd.print(“SNS”);
delay(3000);

lcd.clear ();//Clearing the LCD screen
}

void loop() {

Cycle();

digitalWrite(enPin,HIGH);
}

void Cycle(){

int cycle = 1;
for(cycle ;cycle <= c; cycle++){

float motorSpeed = 4.25531914893617rpm;
//set the motor speed:
int i = 0;
float t= rpm
timing;
for(i;i<t;i++){
if (motorSpeed > 0) {
myStepper.setSpeed(motorSpeed);
// step revolution:
myStepper.step(6400);
if(rpm >= 282){
rpm = 300;
}

if(rpm <= 0.5){
rpm = 0.5;
}
lcd.setCursor(0,0);
lcd.print(“RPM”);
lcd.setCursor(0,1);
lcd.print(rpm);
lcd.setCursor(5,0);
lcd.print(“TIME”);
lcd.setCursor(5,1);
lcd.print(timing);
lcd.setCursor(8,1);
lcd.print(“min”);
lcd.setCursor(10,0);
lcd.print(“CYCLE”);
lcd.setCursor(12,1);
lcd.print(cycle);

}

}
delay(10);
digitalWrite(enPin,HIGH);
delay(10000);
digitalWrite(enPin,LOW);
}
}

To make it easy for people to help you please modify your post and use the code button </>
codeButton.png

so your code 
looks like this

and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to my text editor.

It will also be a big help if you tell us in detail what your program actually does and what you want it to do that is different. It will make it much easier to focus on the parts you need help with rather than wasting time on things that you can do.

...R

Firstly I'd immediately advise you to learn the AccelStepper library which provides speed ramping
(usually essential for all but smallest motors), and also does the work in the background
(well, pseudo-background), freeing up your code to concentrate on the business logic.

You simply have to call the run() method frequently (every time through loop at least), and
the library manages all the timing for you.

Well worth the effort, it will probably make your application easier to code.

  #include <Stepper.h>
 
  const int stepsPerRevolution = 1600;
  const int stepPin = 6;  //PUL -Pulse
  const int dirPin = 7; //DIR -Direction
  const int enPin = 8;  //ENA -Enable
  int one = 30000;//input by user
  int c = 2;//input by user
  int rpm = 1200;//input by user
  unsigned long t = 0;
  Stepper myStepper(stepsPerRevolution, 6, 7);
  void setup() {
    // initialize the serial port:
    Serial.begin(9600);
    pinMode(stepPin, OUTPUT);
    pinMode(dirPin, OUTPUT);
    pinMode(enPin, OUTPUT);
    digitalWrite(enPin, LOW);
    myStepper.setSpeed(rpm);
  }
 
  void loop() {
 
    Cycle();
    digitalWrite(enPin, HIGH);
  }
 
  void Cycle() {
 
    int cycle = 1;
    for (cycle ; cycle <= c; cycle++) {
 
 
      t = millis();
      while ((millis() - t) < one) {
        myStepper.step(stepsPerRevolution);//counter clockwise rotation
 
      }
      delay(3000);
    }
  }

Thank you for posting the program using the code button. However you have not told us what the program actually does and what you want it to do that is different.

...R

I want my stepper motor to run at a specified speed for a specified time and stop for a set time and then repeat the same cycle.
In fact, the number of times the user has to do this will determine the user
I have the last code that failed, but this is not working properly for more than 30 seconds and the spammer rotates permanently.

#include <Stepper.h>
  
  
  const int stepPin = 6;  //PUL -Pulse
  #include <Stepper.h>     //Library for Stepper motor
  
  const int stepsPerRevolution = 1600;
  const int dirPin = 7; //DIR -Direction
  const int enPin = 8;  //ENA -Enable
  int one = 30000;//user input
  int c = 2;// user input
  int rpm = 1200;//user input
  unsigned long t = 0;
  Stepper myStepper(stepsPerRevolution, 6, 7);
  void setup() {
    // initialize the serial port:
    Serial.begin(9600);
    pinMode(stepPin, OUTPUT);
    pinMode(dirPin, OUTPUT);
    pinMode(enPin, OUTPUT);
    digitalWrite(enPin, LOW);
    myStepper.setSpeed(rpm);
  }
  
  void loop() {
  
    Cycle();
    digitalWrite(enPin, HIGH);
  }
  
  void Cycle() {
  
    int cycle = 1;
    for (cycle ; cycle <= c; cycle++) {
  
  
      t = millis();
      while ((millis() - t) < one) {
        myStepper.step(stepsPerRevolution);//counter clockwise rotation
  
      }
      delay(3000);
    }
  }

The code in Reply #8 is very different from the code in Reply #6. Which one do you want help with?

If it is the code in Reply #6 then please use the AutoFormat tool to indent your program consistently and re-post it. At the moment I cannot figure out where different blocks of code are intended to start and end or whether a closing } might be in the wrong place.

...R

this code is last version and it is my code

  #include <Stepper.h>
  
  const int stepsPerRevolution = 1600;
  const int stepPin = 6;  //PUL -Pulse
  const int dirPin = 7; //DIR -Direction
  const int enPin = 8;  //ENA -Enable
  int one = 30000;//input by user
  int c = 2;//input by user
  int rpm = 1200;//input by user
  unsigned long t = 0;
  Stepper myStepper(stepsPerRevolution, 6, 7);
  void setup() {
    // initialize the serial port:
    Serial.begin(9600);
    pinMode(stepPin, OUTPUT);
    pinMode(dirPin, OUTPUT);
    pinMode(enPin, OUTPUT);
    digitalWrite(enPin, LOW);
    myStepper.setSpeed(rpm);
  }
  
  void loop() {
  
    Cycle();
    digitalWrite(enPin, HIGH);
  }
  
  void Cycle() {
  
    int cycle = 1;
    for (cycle ; cycle <= c; cycle++) {
  
  
      t = millis();
      while ((millis() - t) < one) {
        myStepper.step(stepsPerRevolution);//counter clockwise rotation
  
      }
      delay(3000);
    }
  }

fatehan:
this code is last version and it is my code

Thank you for clarifying.

However I’m still a bit confused. In Reply #8 you say

In fact, the number of times the user has to do this will determine the user
I have the last code that failed, but this is not working properly for more than 30 seconds and the spammer rotates permanently.

but your program makes no attempt to get input from the user and I don’t know what you mean by “spammer” - I presume it is typing error but I can’t think what the correct word should be.

This line in your program

myStepper.step(stepsPerRevolution);

will make the motor move for 1600 steps without pausing.

However I don’t know what stepper motor you are using that has 1600 steps per revolution. 200 is the usual number.

Your program seems to want to repeat that until the 30,000 millisecs has expired. (I can’t imagine why you use the variable name one for that number). And then it seems to want to repeat that 2 times. And then loop() will start it all over again. That seems to me all mixed up.

Please describe in English (not code) in as much detail as possible what you want the program to do.

…R

Oh my god, that word is a mistake from me,ecxuse me
I’m using my TB6600 driver and have set the driver to 1600 microsteps
In my code that I put here I want the engine to rotate for 30 seconds and stop for 3 seconds and rotate again for 30 seconds.
Since c = 2 then the motor only has to do it twice but it does not and the motor continues with its own operating cycles.

fatehan:
In my code that I put here I want the engine to rotate for 30 seconds and stop for 3 seconds and rotate again for 30 seconds.

To my mind that would be a lot easier without using the stepper library as it is designed for numbers of steps, rather than time.

Have a look at the second example in this Simple Stepper Code. You could use its singleStep() function like this

void loop() {
    if (moveDone == false) {
       if (millis() - motorStartTime < runDuration ) {
          singleStep();
       }
       else {
          moveDone = true;
          waitStartTime = millis();
       }
     }

    if (moveDone == true) {
       if (millis() - waitStartTime >= waitDuration ) {
           moveDone = false;
           motorStartTime = millis();
       }
    }
}

That should make it run for whatever time you set in runDuration and wait for the time in waitDuration and repeat indefinitely.

I leave it to you to add a count variable to check when it has completed the number of cycles you want. I suggest your do NOT use a FOR loop - just use IF

…R

thanks
but how to set speed of motor??

fatehan:
but how to set speed of motor??

That is determined by the value in the variable millisBetweenSteps

I suggest you play around with the code from my demo first to get familiar with it before adapting it to your own needs.

...R

hi
my stepper motor has 200 steps but i set it on 1600 microsteps by TB6600 driver.
my question is this:
if i set rpm = 1and m=1 Shouldn’t the motor go around in one go?
while i set rpm=1 and m=1 but motor Not a full round.

#include <Stepper.h>

const int stepPin = 6;  //PUL -Pulse
const int dirPin = 7; //DIR -Direction
const int enPin = 8;  //ENA -Enable
const int stepsPerRevolution = 1600;
Stepper myStepper(stepsPerRevolution, 6, 7);

int h = 0;
int m = 1;
unsigned long s;
int delays = 5000;
int c = 1;
int rpm = 1;
unsigned long t = 0;

void setup() {
  // initialize the serial port:
  Serial.begin(9600);
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(enPin, OUTPUT);
  digitalWrite(enPin, LOW);
  myStepper.setSpeed(rpm);
  s = ( (h * 60) + m ) * 60000;
  Cycle();
  digitalWrite(enPin, HIGH);
}


void loop() {

}


void Cycle() {

  int cycle = 1;
  for (cycle ; cycle <= c; cycle++) {
    if (cycle > c) {
      digitalWrite(enPin, LOW);
    }

    t = millis();
    while ((millis() - t) < s) {
      myStepper.step(stepsPerRevolution);

    }
    digitalWrite(enPin, HIGH);
    delay(delays);
    digitalWrite(enPin, LOW);
  }

}

The Arduino stepper library does not work well (or at all) with step / direction type drivers. Try @Robin2's simple stepper code.

@fatehan This is the same problem as in your other Thread where I have already posted several Replies trying to help you

Do not double Post - it just wastes everyone's time. I am suggesting to the Moderator to merge the two Threads.

...R

Topics merged

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a suspension from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.