0

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 steppper motor rotates permanently.

#include <Stepper.h>
const int stepPin = 6;  //PUL -Pulse
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() {
  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);
  }

}
eventHandler
  • 1,088
  • 12
  • 20
fatehan
  • 3
  • 1

1 Answers1

0
int one = 30000;//user input

If you're going to use an int as your timing variable, then you need to look up what is the maximum value that an int can hold on your particular board. If you're using something like an UNO or a Mega then that's 32767 and that will probably explain why it doesn't work with anything over about 32 seconds.

Delta_G
  • 2,927
  • 2
  • 9
  • 15
  • this variable is oooookkkkkk....thanks alot `unsigned long one = 300000;` – fatehan Mar 25 '20 at 20:02
  • I want the engine to stop turning when the cycle is over, but that won't happen and the engine will continue to stop and cycle again.` void Cycle() { int cycle = 1; for (cycle ; cycle <= c; cycle++) { if (cycle > c) { digitalWrite(enPin, LOW); } t = millis(); while ((millis() - t) < one) { myStepper.step(stepsPerRevolution);//counter clockwise rotation } digitalWrite(enPin, HIGH); delay(3000); digitalWrite(enPin, LOW); } }` – fatehan Mar 25 '20 at 20:32
  • Right, because the loop function repeats, so your Cycle function gets called again. Move that to setup if you want it to only happen once. – Delta_G Mar 26 '20 at 01:26